-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepa.js
executable file
·101 lines (85 loc) · 1.98 KB
/
prepa.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const min = require("./min");
const hash = require("./hash");
const zip = require("./zip");
process.title = "prepa";
let args = process.argv.slice(2);
let cmd = null;
let flag = null;
let dir = null;
function help() {
const lines = [
"Usage: prepa <command> --replace <directory>",
"",
"Flags:",
" --replace Replace files in the directory (required to prevent accidents)",
"",
"Commands:",
" go Run min, hash, and zip in one go",
" min Minify assets",
" hash Fingerprint assets",
" rename Like `hash` but without fixing references",
" zip Precompress assets",
];
for (let line of lines) {
console.error(line);
}
}
if (args.length <= 0) {
help();
process.exit(1);
} else {
cmd = args[0];
}
if (!["go", "min", "hash", "rename", "zip"].includes(cmd)) {
console.error(`Unrecognized command: ${cmd}`);
process.exit(1);
}
if (args.length <= 1 || !args[1].startsWith("-")) {
console.error("Missing required flag: --replace");
process.exit(1);
} else {
flag = args[1];
}
if (flag !== "--replace") {
console.error(`Unrecognized flag: ${flag}`);
process.exit(1);
}
if (args.length <= 2) {
console.error("Missing directory");
process.exit(1);
} else {
dir = args[2];
}
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
console.error(`Invalid directory: ${dir}`);
process.exit(1);
}
if (args.length > 3) {
console.error(`Unexpected arguments after the directory`);
process.exit(1);
}
switch (cmd) {
case "go":
go(dir);
break;
case "min":
min.run(dir);
break;
case "hash":
hash.run(dir, true);
break;
case "rename":
hash.run(dir, false);
break;
case "zip":
zip.run(dir);
break;
}
async function go(dir) {
await min.run(dir);
await hash.run(dir, true);
await zip.run(dir);
}