-
Notifications
You must be signed in to change notification settings - Fork 111
/
cli.js
executable file
·31 lines (24 loc) · 1.02 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env node
import { watch } from 'chokidar';
import { fileURLToPath } from 'url';
import { buildFile } from './index.js';
/** @typedef {import('sass').Options<'sync'>} SyncSassOptions */
/** @typedef {import('./').BuildOptions} BuildOptions */
const env = process.env.NODE_ENV || process.env.RAILS_ENV || 'development';
const isProduction = env === 'production';
const args = process.argv.slice(2);
const files = args.filter((arg) => !arg.startsWith('-'));
const flags = args.filter((arg) => arg.startsWith('-'));
const isWatching = flags.includes('--watch');
const outDir = flags.find((flag) => flag.startsWith('--out-dir='))?.slice(10);
/** @type {BuildOptions & SyncSassOptions} */
const options = { outDir, style: isProduction ? 'compressed' : 'expanded' };
Promise.all(
files.map(async (file) => {
const { loadedUrls } = await buildFile(file, options);
if (isWatching) {
const loadedPaths = loadedUrls.map(fileURLToPath);
watch(loadedPaths).on('change', () => buildFile(file, options));
}
}),
);