Skip to content

Commit

Permalink
Implement the --inplace flag.
Browse files Browse the repository at this point in the history
With a rudimentary test that isn't invoked by default.
  • Loading branch information
shlomif committed Sep 2, 2018
1 parent df720b3 commit 6c54f3a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 21 deletions.
57 changes: 36 additions & 21 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ mainOptionKeys.forEach(function(key) {
program.option('--' + paramCase(key), option);
}
});
program.option('-o --output <file>', 'Specify output file (if not specified STDOUT will be used for output)');
var inplace = false;
program.option('--inplace', 'Specify that the files should be overwritten.', function(newInplace) {
inplace = true; });
program.option('-o --output <file>', 'Specify output file (if not specified STDOUT will be used for output)', function(outputPath) {
return fs.createWriteStream(outputPath).on('error', function(e) {
fatal('Cannot write ' + outputPath + '\n' + e.message);
});
}, process.stdout);

function readFile(file) {
try {
Expand Down Expand Up @@ -190,8 +197,9 @@ program.option('--input-dir <dir>', 'Specify an input directory');
program.option('--output-dir <dir>', 'Specify an output directory');
program.option('--file-ext <text>', 'Specify an extension to be read, ex: html');
var content;
program.arguments('[files...]').action(function(files) {
content = files.map(readFile).join('');
var files;
program.arguments('[files...]').action(function(newFiles) {
files = newFiles;
}).parse(process.argv);

function createOptions() {
Expand Down Expand Up @@ -287,24 +295,31 @@ function writeMinify() {
var inputDir = program.inputDir;
var outputDir = program.outputDir;
var fileExt = program.fileExt;
if (inputDir || outputDir) {
if (!inputDir) {
fatal('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.');
if (inplace) {
files.forEach(function (file) { processFile(file, file); });
} else {
if (files) {
content = files.map(readFile).join('');
}
else if (!outputDir) {
fatal('You need to specify where to write the output files with the option --output-dir');
if (inputDir || outputDir) {
if (!inputDir) {
fatal('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.');
}
else if (!outputDir) {
fatal('You need to specify where to write the output files with the option --output-dir');
}
processDirectory(inputDir, outputDir, fileExt);
}
// Minifying one or more files specified on the CMD line
else if (typeof content === 'string') {
writeMinify();
}
// Minifying input coming from STDIN
else {
content = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
content += data;
}).on('end', writeMinify);
}
processDirectory(inputDir, outputDir, fileExt);
}
// Minifying one or more files specified on the CMD line
else if (content) {
writeMinify();
}
// Minifying input coming from STDIN
else {
content = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
content += data;
}).on('end', writeMinify);
}
53 changes: 53 additions & 0 deletions test-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node
/**
* tests for the html-minifier CLI tool
*
* The MIT/Expat License (MIT)
*
* Copyright (c) 2017 Shlomi Fish
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/

'use strict';

QUnit.module("CLI Tests");

QUnit.test("inplace test", function(assert) {
assert.expect(2);

const fs = require('fs');

const temp_dir = fs.mkdtempSync('html-minifier-tests');
const fn1 = temp_dir + "/" + "foo.html";
const fn2 = temp_dir + "/" + "bar.html";
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");
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");

const execFileSync = require('child_process').execFileSync;
execFileSync('node', ['cli.js', '-c', 'sample-cli-config-file.conf', '--inplace', fn1, fn2]);

const contents1 = fs.readFileSync(fn1, {encoding: 'utf-8'});
const contents2 = fs.readFileSync(fn2, {encoding: 'utf-8'});

assert.equal (contents1, "<!DOCTYPE html><meta charset=utf-8><title>Test file</title><p>Text",
"file was modified in-place");
assert.equal (contents2, "<!DOCTYPE html><meta charset=utf-8><title>Bar file</title><p>BarText",
"second file was modified in-place");
});

0 comments on commit 6c54f3a

Please sign in to comment.