Skip to content

Commit 98c9aa4

Browse files
committed
Implement the --inplace flag.
With a rudimentary test that isn't invoked by default.
1 parent 01a7ef7 commit 98c9aa4

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

cli.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ mainOptionKeys.forEach(function(key) {
148148
program.option(key, option);
149149
}
150150
});
151+
var inplace = false;
152+
program.option('--inplace', 'Specify that the files should be overwritten.', function(newInplace) {
153+
inplace = true; });
151154
program.option('-o --output <file>', 'Specify output file (if not specified STDOUT will be used for output)', function(outputPath) {
152155
return fs.createWriteStream(outputPath).on('error', function(e) {
153156
fatal('Cannot write ' + outputPath + '\n' + e.message);
@@ -191,8 +194,9 @@ program.option('--input-dir <dir>', 'Specify an input directory');
191194
program.option('--output-dir <dir>', 'Specify an output directory');
192195
program.option('--file-ext <text>', 'Specify an extension to be read, ex: html');
193196
var content;
194-
program.arguments('[files...]').action(function(files) {
195-
content = files.map(readFile).join('');
197+
var files;
198+
program.arguments('[files...]').action(function(newFiles) {
199+
files = newFiles;
196200
}).parse(process.argv);
197201

198202
function createOptions() {
@@ -286,24 +290,31 @@ function writeMinify() {
286290
var inputDir = program.inputDir;
287291
var outputDir = program.outputDir;
288292
var fileExt = program.fileExt;
289-
if (inputDir || outputDir) {
290-
if (!inputDir) {
291-
fatal('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.');
293+
if (inplace) {
294+
files.forEach(function (file) { processFile(file, file); });
295+
} else {
296+
if (files) {
297+
content = files.map(readFile).join('');
292298
}
293-
else if (!outputDir) {
294-
fatal('You need to specify where to write the output files with the option --output-dir');
299+
if (inputDir || outputDir) {
300+
if (!inputDir) {
301+
fatal('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.');
302+
}
303+
else if (!outputDir) {
304+
fatal('You need to specify where to write the output files with the option --output-dir');
305+
}
306+
processDirectory(inputDir, outputDir, fileExt);
307+
}
308+
// Minifying one or more files specified on the CMD line
309+
else if (typeof content === 'string') {
310+
writeMinify();
311+
}
312+
// Minifying input coming from STDIN
313+
else {
314+
content = '';
315+
process.stdin.setEncoding('utf8');
316+
process.stdin.on('data', function(data) {
317+
content += data;
318+
}).on('end', writeMinify);
295319
}
296-
processDirectory(inputDir, outputDir, fileExt);
297-
}
298-
// Minifying one or more files specified on the CMD line
299-
else if (typeof content === 'string') {
300-
writeMinify();
301-
}
302-
// Minifying input coming from STDIN
303-
else {
304-
content = '';
305-
process.stdin.setEncoding('utf8');
306-
process.stdin.on('data', function(data) {
307-
content += data;
308-
}).on('end', writeMinify);
309320
}

test-cli.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env node
2+
/**
3+
* tests for the html-minifier CLI tool
4+
*
5+
* The MIT/Expat License (MIT)
6+
*
7+
* Copyright (c) 2017 Shlomi Fish
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
10+
* this software and associated documentation files (the "Software"), to deal in
11+
* the Software without restriction, including without limitation the rights to
12+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
13+
* the Software, and to permit persons to whom the Software is furnished to do so,
14+
* subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in all
17+
* copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
21+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
22+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25+
*
26+
*/
27+
28+
'use strict';
29+
30+
QUnit.module("CLI Tests");
31+
32+
QUnit.test("inplace test", function(assert) {
33+
assert.expect(2);
34+
35+
const fs = require('fs');
36+
37+
const temp_dir = fs.mkdtempSync('html-minifier-tests');
38+
const fn1 = temp_dir + "/" + "foo.html";
39+
const fn2 = temp_dir + "/" + "bar.html";
40+
fs.writeFileSync(fn1, "<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>Test file</title>\n</head>\n<body>\n <p>Text</p>\n</body>\n</html>\n");
41+
fs.writeFileSync(fn2, "<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n<title>Bar file</title>\n</head>\n<body>\n <p>BarText</p>\n</body>\n</html>\n");
42+
43+
const execFileSync = require('child_process').execFileSync;
44+
execFileSync('node', ['cli.js', '-c', 'sample-cli-config-file.conf', '--inplace', fn1, fn2]);
45+
46+
const contents1 = fs.readFileSync(fn1, {encoding: 'utf-8'});
47+
const contents2 = fs.readFileSync(fn2, {encoding: 'utf-8'});
48+
49+
assert.equal (contents1, "<!DOCTYPE html><meta charset=utf-8><title>Test file</title><p>Text",
50+
"file was modified in-place");
51+
assert.equal (contents2, "<!DOCTYPE html><meta charset=utf-8><title>Bar file</title><p>BarText",
52+
"second file was modified in-place");
53+
});

0 commit comments

Comments
 (0)