Skip to content

Commit 2b299f7

Browse files
keenanpayneJaiheravi
authored andcommitted
Create simple watching implementation
1 parent 8f13271 commit 2b299f7

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

cli/index.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const mediaMinMax = require('postcss-media-minmax');
1111
const customMedia = require('postcss-custom-media');
1212
const imports = require('postcss-easy-import');
1313

14+
const src = require('./src');
1415
const lh = require('./lib/lh');
1516
const typeScale = require('./lib/type-scale');
1617

@@ -32,16 +33,35 @@ const compile = async src => await postcss()
3233
.use(autoprefixer())
3334
.process(src, { parser: scssSyntax, from: command.input });
3435

36+
const build = () => {
37+
compile(fs.readFileSync(command.input, 'utf8')).then(css => {
38+
fs.writeFile(command.output, css, err => {
39+
if (err) throw err
40+
console.log(`File written: ${command.output}\nFrom: ${command.input}`);
41+
})
42+
});
43+
};
44+
45+
const watch = async path => {
46+
console.log(`Currently watching for changes in: ${path}`);
47+
48+
fs.watch(path, {recursive: true}, (eventType, filename) => {
49+
console.log(`${eventType.charAt(0).toUpperCase() + eventType.slice(1)} in: ${filename}`);
50+
build();
51+
});
52+
};
3553

3654
switch (command.name) {
3755
case 'compile':
38-
compile(fs.readFileSync(command.input, 'utf8')).then(css => {
39-
fs.writeFile(command.output, css, err => {
40-
if (err) throw err
41-
console.log(`File written: ${command.output}\nFrom: ${command.input}`);
42-
})
43-
})
56+
build();
57+
4458
break
59+
case 'watch':
60+
build();
61+
watch(src).catch(console.error);
62+
63+
break
64+
4565
default:
4666
console.log('Unknown command')
4767
break

0 commit comments

Comments
 (0)