Skip to content

Commit 019c031

Browse files
Add feature --watch by including chokidar (#71)
* Add feature --watch by including chokidar Check if the watch flag has been passed Construct an array of expanded file paths in case it is a batched command Load chokidar, pass input paths to its watch method Pass minify(...) as a callback to chokidars onchange method
1 parent 3b4dd20 commit 019c031

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ function cli(process, beforeMinifyCallback) {
3737
.option('--remove-inlined-files', 'Remove files inlined in <source-file ...> or via `@import` statements')
3838
.option('--source-map', 'Enables building input\'s source map')
3939
.option('--source-map-inline-sources', 'Enables inlining sources inside source maps')
40-
.option('--with-rebase', 'Enable URLs rebasing');
40+
.option('--with-rebase', 'Enable URLs rebasing')
41+
.option('--watch', 'Runs CLI in watch mode');
4142

4243
program.on('--help', function () {
4344
console.log('');
@@ -144,7 +145,15 @@ function cli(process, beforeMinifyCallback) {
144145

145146
// ... and do the magic!
146147
if (program.args.length > 0) {
147-
minify(process, options, configurations, expandGlobs(program.args));
148+
var expandedGlobs = expandGlobs(program.args);
149+
if (inputOptions.watch) {
150+
var inputPaths = expandedGlobs.map(function (path) { return path.expanded; });
151+
require('chokidar').watch(inputPaths).on('change', function () {
152+
minify(process, options, configurations, expandedGlobs);
153+
});
154+
} else {
155+
minify(process, options, configurations, expandedGlobs);
156+
}
148157
} else {
149158
stdin = process.openStdin();
150159
stdin.setEncoding('utf-8');

package-lock.json

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
],
3535
"homepage": "https://github.com/clean-css/clean-css-cli#readme",
3636
"dependencies": {
37+
"chokidar": "^3.5.2",
3738
"clean-css": "^5.2.2",
3839
"commander": "7.x",
3940
"glob": "^7.1.6"

0 commit comments

Comments
 (0)