-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
94 lines (80 loc) · 2.58 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
#! /usr/bin/env node
import { sep } from "path";
const [node, source, addonId] = process.argv;
import readline from 'readline'
const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const question = (question) =>
new Promise((resolve) => readlineInterface.question(question, resolve));
import { existsSync, readdirSync, mkdirSync, writeFileSync, readFileSync } from "fs";
(async () => {
let name = await question("Name: ");
let id = (await question(`ID${addonId ? ` (${addonId})` : ""}:`)) || addonId;
let description = await question("Description: ");
let username = await question("Username: ");
let tags = await question("Tags (separate with commas): ");
let userscriptMatch = await question(
"Userscript Matches (separate with commas, leave blank for no userscript): "
);
let userstyleMatch = await question(
"Userstyle Matches (separate with commas, leave blank for no userstyle): "
);
readlineInterface.close();
let dir = process.cwd() + sep + "addons" + sep + id;
if (existsSync(dir)) {
if (readdirSync(dir).length) {
console.log(
"\x1b[31m%s",
"[Error] A folder with this addon ID already exists!",
"\x1b[0m"
);
return;
}
} else mkdirSync(dir);
const addonJSON = {
$schema:
"https://raw.githubusercontent.com/ScratchAddons/manifest-schema/dist/schema.json",
name,
description,
credits: [
{
name: username,
},
],
tags: tags.split(/,\s*/),
};
if (userscriptMatch) {
addonJSON.userscripts = [
{
url: "userscript.js",
matches: userscriptMatch.split(/,\s*/),
},
];
const userscriptJS = `export default async function ({ addon, msg, console }) {\n\n}\n`;
writeFileSync(dir + sep + "userscript.js", userscriptJS);
}
if (userstyleMatch) {
addonJSON.userstyles = [
{
url: "userstyle.css",
matches: userstyleMatch.split(/,\s*/),
},
];
writeFileSync(dir + sep + "userstyle.css", "");
}
writeFileSync(
dir + sep + "addon.json",
JSON.stringify(addonJSON, null, " ") + "\n"
);
const addonsDir = process.cwd() + sep + "addons" + sep + "addons.json";
let addonsJSON = readFileSync(addonsDir, "utf8");
const substringIndex = addonsJSON.indexOf("// NEW ADDONS ABOVE THIS ↑↑") - 5;
addonsJSON =
addonsJSON.substring(0, substringIndex) +
` "${id}",\n` +
addonsJSON.substring(substringIndex, addonsJSON.length);
writeFileSync(addonsDir, addonsJSON);
console.log("\x1b[34m%s", "Addon created:", dir, "\x1b[0m");
})();