-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
109 lines (89 loc) · 2.64 KB
/
index.ts
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
#!/usr/bin/env node
import fs from "fs";
import path from "path";
import arg from "arg";
import invariant from "tiny-invariant";
import console from "console";
const urlRegex =
/((?<!\+)https?:\/\/(?:www\.)?(?:[-\p{Letter}.]+?[.@][a-zA-Z\d]{2,}|localhost)(?:[-\w\p{Letter}.:%+~#*$!?&/=@]*?(?:,(?!\s))*?)*)/gu;
const helpText = `
Description:
a cli to find and remove links that might annoy the chrome web store team
often these are firebase links for some reason
Usage:
badlinks <file_or_folder> [options]
Options:
-r, --remove Remove the found JS links
-h, --help Display this help message
`;
function showHelp() {
console.log(helpText);
process.exit(0);
}
export function extractJsLinks(text: string) {
return text.match(urlRegex)?.filter((link) => link.includes(".js")) || [];
}
function processFile(filePath: string, shouldRemove: boolean) {
let fileContent = fs.readFileSync(filePath, "utf-8");
const jsLinks = extractJsLinks(fileContent);
if (jsLinks.length === 0) {
console.log(`No JS link(s) found in ${filePath}`);
return;
}
console.log(`JS link(s) found in ${filePath}:`);
console.log(jsLinks);
if (shouldRemove) {
jsLinks.forEach((link) => (fileContent = fileContent.replace(link, "")));
fs.writeFileSync(filePath, fileContent);
console.log(`JS link(s) removed from ${filePath}`);
} else {
console.error("Links found (use --remove to remove them).");
}
}
function processPath(inputPath: string, shouldRemove: boolean) {
const fileStats = fs.statSync(inputPath);
if (fileStats.isDirectory()) {
const fileNames = fs.readdirSync(inputPath);
for (const fileName of fileNames) {
processPath(path.join(inputPath, fileName), shouldRemove);
}
} else if (fileStats.isFile() && path.extname(inputPath) === ".js") {
processFile(inputPath, shouldRemove);
}
}
function parseArgs() {
const argSpec = {
"--help": Boolean,
"--remove": Boolean,
"-h": "--help",
"-r": "--remove",
};
try {
const args = arg(argSpec);
if (args["--help"]) {
showHelp();
}
const inputPath = args._.length > 0 ? args._[0] : null;
if (!inputPath) {
console.error("** Error: A file or folder path is required \n --- ");
showHelp();
}
return {
inputPath,
remove: args["--remove"] || false,
};
} catch (error) {
// @ts-ignore
console.error(`Error: ${error?.message}`);
showHelp();
}
}
try {
const options = parseArgs();
invariant(options?.inputPath, "No input path");
processPath(options.inputPath, options.remove);
} catch (error) {
// @ts-ignore
console.error("Error:", error.message);
process.exit(1);
}