Skip to content

Commit faf9ed0

Browse files
Fixes #65 - batch processing with output path.
When an output path is given (via `-o` or `--output`) while batch processing is turned on (via `-b` or `--batch`) then output files are written to a directory under output path, while the root of input wildcard is used as a base, e.g.: ```bash cleancss --batch --output dist/ source/**/*.css ``` will put `source/one.css` under `dist/one-min.css` and `source/vendor/two.css` under `dist/vendor/two-min.css`. In case of input path given explicitely they are written directly to output path, e.g. ```bash cleancss --batch --output dist/ source/one.css source/vendor/two.css ``` will put `source/one.css` under `dist/one-min.css` and `source/vendor/two.css` under `dist/two-min.css`.
1 parent e87acb7 commit faf9ed0

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

History.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
[5.3.0 / 2021-xx-xx](https://github.com/jakubpawlowicz/clean-css-cli/compare/5.2...HEAD)
2+
==================
3+
4+
* Fixed issue [#65](https://github.com/jakubpawlowicz/clean-css-cli/issues/65) - batch processing with output path.
5+
16
[5.2.2 / 2021-03-19](https://github.com/jakubpawlowicz/clean-css-cli/compare/v5.2.1...v5.2.2)
27
==================
38

index.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ function cli(process, beforeMinifyCallback) {
120120
options.sourceMap = false;
121121
}
122122

123+
if (options.output && options.batch) {
124+
fs.mkdirSync(options.output, {recursive: true});
125+
}
126+
123127
var configurations = {
124128
batchSuffix: inputOptions.batchSuffix,
125129
beforeMinifyCallback: beforeMinifyCallback,
@@ -179,34 +183,56 @@ function expandGlobs(paths) {
179183
.map(function (path) { return path.substring(1); });
180184

181185
return globPatterns.reduce(function (accumulator, path) {
182-
return accumulator.concat(glob.sync(path, { ignore: ignoredGlobPatterns, nodir: true, nonull: true }));
186+
var expandedWithSource =
187+
glob.sync(path, { ignore: ignoredGlobPatterns, nodir: true, nonull: true })
188+
.map(function (expandedPath) { return { expanded: expandedPath, source: path }; });
189+
190+
return accumulator.concat(expandedWithSource);
183191
}, []);
184192
}
185193

186194
function minify(process, options, configurations, data) {
187195
var cleanCss = new CleanCSS(options);
196+
var input = typeof(data) == 'string' ?
197+
data :
198+
data.map(function (o) { return o.expanded; });
188199

189200
applyNonBooleanCompatibilityFlags(cleanCss, options.compatibility);
190201
configurations.beforeMinifyCallback(cleanCss);
191-
cleanCss.minify(data, getSourceMapContent(configurations.inputSourceMap), function (errors, minified) {
202+
cleanCss.minify(input, getSourceMapContent(configurations.inputSourceMap), function (errors, minified) {
192203
var inputPath;
204+
var outputPath;
193205

194206
if (options.batch && !('styles' in minified)) {
195207
for (inputPath in minified) {
196-
processMinified(process, configurations, minified[inputPath], inputPath, toOutputPath(inputPath, configurations.batchSuffix));
208+
outputPath = options.batch && options.output ?
209+
toBatchOutputPath(inputPath, configurations.batchSuffix, options.output, data) :
210+
toSimpleOutputPath(inputPath, configurations.batchSuffix);
211+
212+
processMinified(process, configurations, minified[inputPath], inputPath, outputPath);
197213
}
198214
} else {
199215
processMinified(process, configurations, minified, null, options.output);
200216
}
201217
});
202218
}
203219

204-
function toOutputPath(inputPath, batchSuffix) {
220+
function toSimpleOutputPath(inputPath, batchSuffix) {
205221
var extensionName = path.extname(inputPath);
206222

207223
return inputPath.replace(new RegExp(extensionName + '$'), batchSuffix + extensionName);
208224
}
209225

226+
function toBatchOutputPath(inputPath, batchSuffix, output, expandedWithSource) {
227+
var extensionName = path.extname(inputPath);
228+
var inputSource = expandedWithSource.find(function (ic) { return ic.expanded == inputPath; }).source;
229+
var inputSourceRoot = inputSource.indexOf('*') > -1 ?
230+
inputSource.substring(0, inputSource.indexOf('*')) :
231+
path.dirname(inputSource);
232+
233+
return path.join(output, inputPath.replace(inputSourceRoot, '').replace(new RegExp(extensionName + '$'), batchSuffix + extensionName));
234+
}
235+
210236
function processMinified(process, configurations, minified, inputPath, outputPath) {
211237
var mapOutputPath;
212238

test/binary-test.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -844,20 +844,25 @@ vows.describe('cleancss')
844844
})
845845
})
846846
.addBatch({
847-
'batch processing with output given': binaryContext('-b -o ./test ./test/fixtures-batch-4/partials/one.css ./test/fixtures-batch-4/partials/five.css', {
847+
'batch processing with output given': binaryContext('-b -o ./test/fixtures-batch-4-output ./test/fixtures-batch-4/partials/one.css ./test/fixtures-batch-4/partials/five.css', {
848848
'setup': function () {
849849
execSync('cp -fr test/fixtures test/fixtures-batch-4');
850850
},
851851
'does not produce any errors': function (error) {
852852
assert.equal(error, '');
853853
},
854854
'creates two separate minified files': function () {
855-
assert.isTrue(fs.existsSync('test/fixtures-batch-4/partials/one-min.css'));
855+
assert.isFalse(fs.existsSync('test/fixtures-batch-4/partials/one-min.css'));
856856
assert.isFalse(fs.existsSync('test/fixtures-batch-4/partials/two-min.css'));
857-
assert.isTrue(fs.existsSync('test/fixtures-batch-4/partials/five-min.css'));
857+
assert.isFalse(fs.existsSync('test/fixtures-batch-4/partials/five-min.css'));
858+
859+
assert.isTrue(fs.existsSync('test/fixtures-batch-4-output/one-min.css'));
860+
assert.isFalse(fs.existsSync('test/fixtures-batch-4-output/two-min.css'));
861+
assert.isTrue(fs.existsSync('test/fixtures-batch-4-output/five-min.css'));
858862
},
859863
'teardown': function () {
860864
execSync('rm -fr test/fixtures-batch-4');
865+
execSync('rm -fr test/fixtures-batch-4-output');
861866
}
862867
})
863868
})
@@ -879,4 +884,23 @@ vows.describe('cleancss')
879884
}
880885
})
881886
})
887+
.addBatch({
888+
'batch processing with output as a path': binaryContext('-b ./test/fixtures-batch-6/partials/\\*\\*/*.css -o test/fixtures-batch-6-output', {
889+
'setup': function () {
890+
execSync('cp -fr test/fixtures test/fixtures-batch-6');
891+
},
892+
'creates two separate minified files': function () {
893+
assert.isTrue(fs.existsSync('test/fixtures-batch-6-output/extra/four-min.css'));
894+
assert.isTrue(fs.existsSync('test/fixtures-batch-6-output/extra/three-min.css'));
895+
assert.isTrue(fs.existsSync('test/fixtures-batch-6-output/one-min.css'));
896+
assert.isTrue(fs.existsSync('test/fixtures-batch-6-output/two-min.css'));
897+
assert.isTrue(fs.existsSync('test/fixtures-batch-6-output/quoted-svg-min.css'));
898+
assert.isTrue(fs.existsSync('test/fixtures-batch-6-output/five-min.css'));
899+
},
900+
'teardown': function () {
901+
execSync('rm -fr test/fixtures-batch-6');
902+
execSync('rm -fr test/fixtures-batch-6-output');
903+
}
904+
})
905+
})
882906
.export(module);

0 commit comments

Comments
 (0)