Skip to content

Commit 50854f9

Browse files
Adds --batch-suffix option.
It specifies what gets added to output filename when processing in batch mode.
1 parent 9fc6e5e commit 50854f9

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[5.0.0 / 2021-xx-xx](https://github.com/jakubpawlowicz/clean-css-cli/compare/4.3...HEAD)
22
==================
33

4+
* Adds `--batch-suffix` option to specify what gets appended to output filename in batch mode.
45
* Bumps clean-css dependency to 5.0.
56
* Bumps commander dependency to 7.0.
67
* Fixed issue [#18](https://github.com/jakubpawlowicz/clean-css-cli/issues/18) - allows batch processing of input files.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ cleancss -o one.min.css one.css
6565
clean-css-cli 5.0 introduces the following changes / features:
6666

6767
* adds `--batch` option (off by default) which processes input files one by one without joining them together;
68+
* adds `--batch-suffix` option to specify what gets appended to output filename in batch mode;
6869
* clean-css 5.0 with loads of bugfixes;
6970
* drops official support for Node.js 4, 6, and 8;
7071
* `--skip-rebase` option has been removed as rebasing URLs is disabled by default now

index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function cli(process, beforeMinifyCallback) {
3131
.option('-f, --format <options>', 'Controls output formatting, see examples below')
3232
.option('-o, --output [output-file]', 'Use [output-file] as output instead of STDOUT')
3333
.option('-O <n> [optimizations]', 'Turn on level <n> optimizations; optionally accepts a list of fine-grained options, defaults to `1`, see examples below, IMPORTANT: the prefix is O (a capital o letter), NOT a 0 (zero, a number)', function (val) { return Math.abs(parseInt(val)); })
34+
.option('--batch-suffix <suffix>', 'A suffix (without extension) appended to input file name when processing in batch mode (`-min` is the default)', '-min')
3435
.option('--inline [rules]', 'Enables inlining for listed sources (defaults to `local`)')
3536
.option('--inline-timeout [seconds]', 'Per connection timeout when fetching remote stylesheets (defaults to 5 seconds)', parseFloat)
3637
.option('--remove-inlined-files', 'Remove files inlined in <source-file ...> or via `@import` statements')
@@ -172,6 +173,7 @@ function cli(process, beforeMinifyCallback) {
172173
}
173174

174175
var configurations = {
176+
batchSuffix: inputOptions.batchSuffix,
175177
beforeMinifyCallback: beforeMinifyCallback,
176178
debugMode: debugMode,
177179
removeInlinedFiles: removeInlinedFiles,
@@ -238,16 +240,18 @@ function minify(process, options, configurations, data) {
238240

239241
if (options.batch && !('styles' in minified)) {
240242
for (inputPath in minified) {
241-
processMinified(process, configurations, minified[inputPath], inputPath, toOutputPath(inputPath));
243+
processMinified(process, configurations, minified[inputPath], inputPath, toOutputPath(inputPath, configurations.batchSuffix));
242244
}
243245
} else {
244246
processMinified(process, configurations, minified, null, options.output);
245247
}
246248
});
247249
}
248250

249-
function toOutputPath(inputPath) {
250-
return inputPath.replace(/\.css$/, '-min.css');
251+
function toOutputPath(inputPath, batchSuffix) {
252+
var extensionName = path.extname(inputPath);
253+
254+
return inputPath.replace(new RegExp(extensionName + '$'), batchSuffix + extensionName);
251255
}
252256

253257
function processMinified(process, configurations, minified, inputPath, outputPath) {

test/binary-test.js

+17
Original file line numberDiff line numberDiff line change
@@ -777,4 +777,21 @@ vows.describe('cleancss')
777777
}
778778
})
779779
})
780+
.addBatch({
781+
'batch processing with custom suffix': binaryContext('--batch --batch-suffix \'.min\' ./test/fixtures/partials/one.css ./test/fixtures/partials/five.css', {
782+
'creates two separate minified files': function () {
783+
assert.isFalse(fs.existsSync('test/fixtures/partials/one-min.css'));
784+
assert.isFalse(fs.existsSync('test/fixtures/partials/two-min.css'));
785+
assert.isFalse(fs.existsSync('test/fixtures/partials/five-min.css'));
786+
787+
assert.isTrue(fs.existsSync('test/fixtures/partials/one.min.css'));
788+
assert.isFalse(fs.existsSync('test/fixtures/partials/two.min.css'));
789+
assert.isTrue(fs.existsSync('test/fixtures/partials/five.min.css'));
790+
},
791+
'teardown': function () {
792+
deleteFile('test/fixtures/partials/one.min.css');
793+
deleteFile('test/fixtures/partials/five.min.css');
794+
}
795+
})
796+
})
780797
.export(module);

0 commit comments

Comments
 (0)