-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·112 lines (100 loc) · 2.88 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
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
#!/usr/bin/env node
/* eslint @typescript-eslint/no-var-requires: 0 */
const fs = require("fs");
const packageJson = require("./package.json");
const fakeDiff = require("fake-diff");
function help() {
console.error(
`
${packageJson.description} from a file.
By default, output is redirected to STDOUT.
Usage:
${packageJson.name} [-h] [-v] [-k 'light' | 'dark'] [-d] [-w] [-s] file.md
Options:
-h, --help Display this help text and exit.
-v, --version Show this program version number (${packageJson.version}).
-k, --keep Theme image links to keep. Either 'light or 'dark'.
-d, --diff Show the difference before and after links stripping.
-w, --write Write changes in file. Not compatible with --diff.
-s, --strict Exit with code 1 if any content is stripped.
`
);
process.exit(1);
}
function version() {
console.error(packageJson.version);
process.exit(1);
}
function processArgs(args) {
if (args.length === 0) {
help();
}
let keep = "light",
file,
_insideKeep = false,
diff = false,
write = false,
strict = false;
for (const arg of args) {
if (["-h", "--help"].includes(arg)) {
help();
} else if (["-v", "--version"].includes(arg)) {
version();
} else if (["-d", "--diff"].includes(arg)) {
diff = true;
} else if (["-w", "--write"].includes(arg)) {
write = true;
} else if (["-s", "--strict"].includes(arg)) {
strict = true;
} else if (["-k", "--keep"].includes(arg)) {
_insideKeep = true;
} else if (_insideKeep) {
if (!["light", "dark"].includes(arg)) {
console.error(
`ERROR: -k/--keep argument value must be either 'light' or 'dark'`
);
help();
}
keep = arg;
} else {
if (!fs.existsSync(arg)) {
console.error(`ERROR: File ${arg} does not exists`);
help();
}
file = arg;
}
}
if (write && diff) {
console.error(
`ERROR: The option --write is not compatible along with --diff`
);
help();
}
return { file, keep, diff, write, strict };
}
if (require.main === module) {
let sliceN = 1;
if (
process.argv.indexOf(module.filename) > -1 ||
require("path").basename(process.argv[1]) === "strip-gh-theme-links"
) {
sliceN = 2;
}
const { file, keep, diff, write, strict } = processArgs(
process.argv.slice(sliceN)
);
const stripGhThemeLinks = require("./dist/cjs").default;
const content = fs.readFileSync(file, "utf-8");
const strippedContent = stripGhThemeLinks(content, keep);
if (strict && content.length === strippedContent.length) {
console.error(`ERROR: Any content stripped from file '${file}'`);
process.exit(1);
}
if (diff) {
console.log(fakeDiff(content, strippedContent));
} else if (write) {
fs.writeFileSync(file, strippedContent);
} else {
console.log(strippedContent);
}
}