forked from RobinBuschmann/usemin-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·84 lines (79 loc) · 2.09 KB
/
index.js
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
'use strict';
var argv = require('yargs')
.usage('Usage: usemin [input.html] [--dest|-d dir] [--output|-o output.html] [options]')
.example('usemin src/index.html -d dist -o dist/index.html')
.example('usemin src/index.html -d dist > dist/index.html')
.options({
'd': {
alias: 'dest',
demand: 'Please specify the output directory',
describe: 'Output directory for compressed output files',
type: 'string'
},
'o': {
alias: 'output',
describe: 'HTML output file',
type: 'string'
},
'htmlmin': {
default: false,
describe: 'Also minifies the input HTML file',
type: 'boolean'
},
'rmlr': {
alias: 'removeLivereload',
default: false,
describe: 'Remove livereload script',
type: 'boolean'
},
'noprocess': {
default: false,
describe: 'Do not process files, just replace references',
type: 'boolean'
},
'bust': {
default: true,
describe: 'Defines whether files should be cache busted or not',
type: 'boolean'
},
'bustMaps': {
default: false,
describe: 'Defines whether file maps should be cache busted or not',
type: 'boolean'
},
'c': {
alias: 'config',
describe: 'Config file for UglifyJS, CleanCSS and htmlmin',
type: 'string'
}
})
.demand(1)
.argv;
var fs = require('fs');
var getBlocks = require('./lib/getBlocks');
var getConfig = require('./lib/getConfig');
var processBlocks = require('./lib/processBlocks');
var getHTML = require('./lib/getHTML');
var processCacheBusting = require('./lib/processCacheBusting');
var filePath = argv._[0];
var content = fs.readFileSync(filePath).toString();
var blocks = getBlocks(argv._[0], content, argv.removeLivereload);
var config = getConfig(argv.c);
var process = (argv.noprocess === true) ? true : processBlocks(blocks, argv.dest, config);
if(argv.bust) processCacheBusting(blocks, argv.dest, argv.bustMaps);
var output = getHTML(content, blocks, argv.htmlmin, config);
if (process) {
if (argv.o) {
fs.writeFile(argv.o, output, function(err) {
if (err) {
throw Error(err);
}
});
}
else {
console.log(output);
}
}
else {
throw Error('Unexpected error.');
}