Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the --inplace flag. #828

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ 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 = newInplace;
});
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 +198,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 +296,34 @@ 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.');
}
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 (content) {
writeMinify();
if (inplace) {
files.forEach(function(file) {
processFile(file, file);
});
}
// Minifying input coming from STDIN
else {
content = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
content += data;
}).on('end', writeMinify);
if (files) {
content = files.map(readFile).join('');
}
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);
}
}
13 changes: 6 additions & 7 deletions dist/htmlminifier.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/htmlminifier.min.js

Large diffs are not rendered by default.

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");
});
4 changes: 2 additions & 2 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<head>
<meta charset="utf-8">
<title>HTML Minifier Tests</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.1.css">
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.6.2.css">
</head>
<body>
<div id="qunit">
<button onclick="QUnit.start()">Start</button>
</div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.6.1.js"></script>
<script src="https://code.jquery.com/qunit/qunit-2.6.2.js"></script>
<script src="../dist/htmlminifier.min.js"></script>
<script src="minifier.js"></script>
</body>
Expand Down