-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·38 lines (31 loc) · 939 Bytes
/
index.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
32
33
34
35
36
37
38
const chokidar = require('chokidar');
const { appendFile, readFile, writeFileSync, existsSync } = require('fs');
const PRINT = '/*CHANGE*/';
const PRINT_REG = /\n?\/\*CHANGE\*\//gm;
module.exports = (options = {}) => {
const opts = {
main: './style.css',
folder: '.',
...options
};
if (!existsSync(opts.folder)) {
throw new TypeError(`postcss-watch-folder: The specified folder (${opts.folder}) could not be located.`);
}
if (!existsSync(opts.main)) {
throw new TypeError(`postcss-watch-folder: The specified main file (${opts.main}) could not be located.`);
}
const watcher = chokidar.watch(opts.folder);
return {
postcssPlugin: 'postcss-watch-folder',
Once() {
watcher.on('add', () => {
appendFile(opts.main, PRINT, () => {
readFile(opts.main, 'utf8', (err, data) => {
writeFileSync(opts.main, data.replace(PRINT_REG, ''));
});
});
});
}
};
};
module.exports.postcss = true;