-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·127 lines (108 loc) · 3.01 KB
/
Copy pathcli.js
File metadata and controls
executable file
·127 lines (108 loc) · 3.01 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
const execSync = require('child_process').execSync;
const fs = require('fs');
const omelette = require('omelette');
// Recursively get all JavaScript files inside a directory.
const getAllFiles = path => {
let files = fs
.readdirSync(path)
.map(file => {
let result = `${path}/${file}`;
if (file.match(/^(\.|node_modules$)/)) {
result = null;
} else if (fs.statSync(`${path}/${file}`).isDirectory()) {
result = getAllFiles(`${path}/${file}`);
}
return result;
})
.flat()
.filter(file => !!file && file.match(/(?<!\.min)\.js$/))
.map(file => file.replace(/^\.\//, ''));
return files;
};
// Auto-completion.
const completion = omelette(`peepso <action> <path>`);
completion.on('action', ({ reply }) => {
reply(['build']);
});
completion.on('path', ({ reply }) => {
reply(getAllFiles('.'));
});
completion.init();
if (~process.argv.indexOf('--setup')) {
completion.setupShellInitFile();
} else if (~process.argv.indexOf('--cleanup')) {
completion.cleanupShellInitFile();
}
// Parameters.
const argv = process.argv.slice(2);
const cmd = argv[0];
const args = argv.slice(1);
switch (cmd) {
case 'build':
build(args);
break;
default:
break;
}
/**
* Build and uglify files.
*
* @param {Array} args
*/
function build(args = []) {
// An empty path will implicitly build the default `assets/js/index.js` file.
if (!args.length) {
args = ['assets/js/index.js'];
}
args.forEach(source => {
// Skip if file is not exist.
if (!fs.existsSync(source)) {
source = `assets/js/${source}`;
if (!fs.existsSync(source)) {
return;
}
}
// Get the non-minified file path if necessary.
if (source.match(/\.min\.js$/)) {
// Special case, `assets/js/bundle.min.js` file is compiled from `assets/js/index.js`
if ('assets/js/bundle.min.js' === source) {
source = 'assets/js/index.js';
} else {
source = source.replace(/\.min\.js$/, '.js');
}
if (!fs.existsSync(source)) {
source = source.replace(/\.js$/, '');
if (!fs.existsSync(source)) {
return;
}
}
}
// Resolve file in case if source is a directory.
if (!source.match(/\.js$/)) {
source = source.replace(/\/+$/, '');
source = `${source}/index.js`;
if (!fs.existsSync(source)) {
return;
}
}
// Resolve the target file path.
if ('assets/js/index.js' === source) {
// Special case, `assets/js/index.js` will be compiled into `assets/js/bundle.min.js`
target = 'assets/js/bundle.min.js';
} else {
target = source.replace(/(\/index)?\.js$/, '') + '.min.js';
}
let cmd = `npx browserify -e ${source} | npx uglifyjs -c -m -o ${target}`;
// Do not use browserify if the output was previously built without browserify to avoid issues.
if (fs.existsSync(target)) {
let content = fs.readFileSync(target).toString().substr(0, 300);
if (content.indexOf('MODULE_NOT_FOUND') === -1) {
cmd = `npx uglifyjs -c -m -o ${target} ${source}`;
}
}
console.log(`Compiling ${source} into ${target}...`);
console.log(`> ${cmd}`);
execSync(cmd);
});
}