-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path84.js
More file actions
113 lines (100 loc) · 2.91 KB
/
Copy path84.js
File metadata and controls
113 lines (100 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
var commandLine = require('commander');
var Promise = require('bluebird');
var path = require('path');
var fs = Promise.promisifyAll(require('fs'));
var pkg = require('../package.json');
var standalone = require('../index');
var colors = require('colors');
var minify = Promise.promisifyAll(require('html-minifier').minify);
var arg = process.argv;
// options
commandLine
.version('standalone-html v' + pkg.version)
.usage('[options] <full path to source html> --output <path>')
.option('-o, --output <path>', 'full path to output file')
.option('-e, --escape "[ regex ]"', 'ignore the given regular expression when minify')
.option('-m, --minifyall', 'minify the html file, include all scripts, css & image')
.option('-j, --justminify', 'minify the html')
.parse(arg)
var html = commandLine.args[0];
var output = (commandLine.output === undefined) ? 'index_standalone.html' : commandLine.output;
var escapeChar = (commandLine.escape === undefined) ? '[]' : commandLine.escape;
var opt = {
removeAttributeQuotes: false,
minifyCSS: true,
minifyJS: true,
collapseWhitespace: true,
removeComments: true,
ignoreCustomFragments: eval(escapeChar)
};
if (!html) {
commandLine.help();
} else {
// ensure file exist and proceed or exit
fs.stat(html, function (err, stat) {
if (err === null) {
console.log('');
console.log('Target file : ' + html);
console.log('');
startApp();
} else if (err.code == 'ENOENT') {
console.log('File does not exist.'.red + ' Exit.');
process.exit;
} else {
console.log('File error: ' + err.code + '. Exit.');
process.exit;
}
});
}
function startApp() {
var inputPath = path.dirname(html);
var inputFile = fs.readFileSync(html, 'utf-8');
var outputPath = output;
var currentDir = process.cwd();
if (output && html && commandLine.justminify) {
minifyFile(inputFile, outputPath);
} else if (output && html) {
standalone.cli(inputPath, inputFile, outputPath, getOpt);
} else {
commandLine.help();
}
}
//parse options
function getOpt(resHtml, outputPath) {
if (commandLine.minifyall) {
console.log('');
console.log('minify all. Process may take a few minutes with large file.');
console.log('');
minifyFile(resHtml, outputPath);
} else {
console.log('');
console.log('Output file name : ' + outputPath);
console.log('');
writeFile(resHtml, outputPath);
}
}
//minify the result
function minifyFile(resHtml, outputPath) {
var resHtml = minify(resHtml, opt, function (err) {
if (err) {
console.error('error will processing file.');
}
});
console.log('');
console.log('Output file name : ' + outputPath);
console.log('');
writeFile(resHtml, outputPath);
}
//write result to file
function writeFile(resHtml, outputPath) {
fs.writeFile(outputPath, resHtml, function (err) {
if (err) {
console.log('');
console.log('File error: ' + err + '. Exit.');
} else {
console.log('');
console.log('All done. Exit.'.green);
}
});
}