-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
66 lines (55 loc) · 1.85 KB
/
cli.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
const path = require('path');
const { performance } = require('perf_hooks');
const { compress, formats: FORMATS } = require('./');
const package = require('./package.json');
const rootDir = process.cwd();
const [ , , ...restArgs] = process.argv;
const params = Object.fromEntries(restArgs.map(param => {
const pair = param.split('=');
return [
pair[0].slice(2),
(pair[1] === undefined || pair[1] === null) ? true : pair[1]
]
}));
const from = params.input || params.from || rootDir;
const to = params.output || params.to || from;
const fromFolder = path.isAbsolute(from) ? from : path.join(rootDir, from);
const toFolder = path.isAbsolute(to) ? to : path.join(rootDir, to);
const formats = params['formats']
? params['formats'].split(',')
: [FORMATS.GZIP, FORMATS.BROTLI];
if (params['ext-white-list']) {
params['ext-white-list'] = params['ext-white-list'].split(',');
}
if (params['file-size']) {
const fileSize = parseFloat(params['file-size']);
if (typeof fileSize === 'number' && !Number.isNaN(fileSize) && Number.isFinite(fileSize)) {
params['file-size'] = fileSize;
} else {
delete params['file-size'];
}
}
if (params['concurrency']) {
const concurrency = parseInt(params['concurrency']);
if (typeof concurrency === 'number' && !Number.isNaN(concurrency) && Number.isFinite(concurrency)) {
params['concurrency'] = concurrency;
} else {
delete params['concurrency'];
}
}
const startTime = performance.now();
console.log(package.name);
console.log(`version: ${package.version}`);
compress({
from: fromFolder,
to: toFolder,
formats,
extWhiteList: params['ext-white-list'],
concurrency: params['concurrency'],
fileSize: params['file-size']
})
.then(() => {
const diffTime = Math.round(performance.now() - startTime);
console.log(`The Compression Process took ${diffTime}ms`)
})
.catch(console.error)