-
Notifications
You must be signed in to change notification settings - Fork 43
/
tools.mjs
188 lines (152 loc) · 3.76 KB
/
tools.mjs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import fs from "fs";
const args = process.argv.slice(2);
function redLog(msg, noDash = false) {
if (noDash) {
console.log("\x1b[31m%s\x1b[0m", `- ${msg}`);
} else {
console.log("\x1b[31m%s\x1b[0m", msg);
}
}
function greenLog(msg, noDash = false) {
if (noDash) {
console.log("\x1b[32m%s\x1b[0m", `- ${msg}`);
} else {
console.log("\x1b[32m%s\x1b[0m", msg);
}
}
function usageLog({ usage, options }) {
const optionsStr = options.map((option) => {
return ` <${option.name}> ${option.description}`;
});
greenLog(`
Usage: ${usage} [options]
Options:
${optionsStr}
`);
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
try {
Array.prototype.asyncForEach = async function (callback) {
for (let index = 0; index < this.length; index++) {
await callback(this[index], index, this);
}
};
} catch (e) {
// swallow
}
// create map of options
const commandMaps = [
{
cmd: "pull",
usage: "yarn tools pull",
description: "pull the changes based on repo.config.js",
fn: pullFunc,
},
];
function getCmd() {
let cmd;
let errMsg = `\n❌ Command "${args[0]}" not found.\n`;
function printAllCommands() {
greenLog("All available commands: \n");
commandMaps.forEach((cm) => {
greenLog(` ${cm.cmd} - ${cm.description}`);
});
greenLog(``);
}
try {
cmd = commandMaps.find((cm) => cm.cmd === args[0]);
} catch (e) {
redLog(errMsg);
printAllCommands();
process.exit();
}
if (!cmd) {
redLog(errMsg);
printAllCommands();
process.exit();
}
return cmd;
}
async function getReposConfig() {
const fileName = "repos.config.json";
let reposConfig;
try {
reposConfig = JSON.parse(await fs.promises.readFile(fileName, "utf8"));
} catch (e) {
redLog(`❌ Failed to read ${fileName}. Error: ${e}`);
process.exit();
}
if (reposConfig.length <= 0) {
redLog(`❌ No repos found in ${fileName}.`);
process.exit();
}
await reposConfig.asyncForEach((repo) => {
if (!repo.copyFrom) {
redLog(`❌ copyFrom is missing for ${JSON.stringify(repo)}.`);
process.exit();
}
if (!repo.pasteTo) {
redLog(`❌ pasteTo is missing for ${JSON.stringify(repo)}.`);
process.exit();
}
if (!repo.sidebarPosition) {
redLog(`❌ sidebarPosition is missing for ${JSON.stringify(repo)}.`);
process.exit();
}
});
return reposConfig;
}
async function fetchCopyFrom(url) {
let res;
try {
res = await fetch(url).then((res) => res.text());
} catch (e) {
redLog(`❌ ${e} - Failed to fetch ${url}. => Continue...`);
}
return res;
}
async function writePasteTo(path, content) {
greenLog(`📝 Writing to ${path}`);
let res = false;
try {
await fs.promises.writeFile(path, content);
res = true;
} catch (e) {
redLog(`❌ ${e} - Failed to write to ${path}. => Continue...`);
}
return res;
}
function getHeader(sidebarPosition) {
return `---
sidebar_position: ${sidebarPosition}
---
\n`;
}
async function pullFunc() {
greenLog("🚀 Pulling changes...");
const reposConfig = await getReposConfig();
reposConfig.asyncForEach(async (repo) => {
const content = await fetchCopyFrom(repo.copyFrom);
if (content) {
const contentHeader = getHeader(repo.sidebarPosition);
const newContent = contentHeader + content;
await writePasteTo(process.cwd() + repo.pasteTo, newContent);
}
});
}
// -- start script
async function setup() {
// find the cmd based on the first argument
const cmd = getCmd();
try {
cmd.fn();
} catch (e) {
redLog(`❌ Failed to run function ${cmd.cmd}. Error: ${e}`);
process.exit();
}
}
setup();