-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
235 lines (217 loc) · 6.03 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import inquirer from "inquirer";
import { lstatSync, readdirSync, writeFileSync, existsSync, readdir } from "fs";
import meow from "meow";
import readJSON from "./helpers/readJson";
import { extname, join, resolve } from "path";
import log from "./helpers/log";
import run from "./run";
import { spawn, execSync } from "child_process";
import { parseArgs } from "util";
import { homedir, platform } from "os";
import getFirefoxPaths from "./helpers/firefoxPaths";
import files from "./asset-bundle";
import modulesList from "./helpers/allModules";
const NAME = `firefox-profile-creator`;
const { APP_PATH, PROFILES_PATH } = getFirefoxPaths();
const args = parseArgs({
args: Bun.argv,
options: {
launch: {
type: "boolean",
},
help: {
type: "boolean",
short: "h",
},
output: {
type: "string",
short: "o",
},
},
allowPositionals: true,
});
const PROFILE_PATH_CLI = args.positionals[2];
const OUTPUT_PATH_CLI = args.values.output ? resolve(args.values.output) : null;
const MODULE_DIR = "modules";
const OPTIONS = Object.fromEntries(
modulesList
.map((i) => ({
id: i,
info: readJSON(join(MODULE_DIR, i, "index.json")),
}))
.map((i) => [i.id, i.info]),
);
const questions = [
...Object.entries(OPTIONS)
.map(([k, v]) => {
const index = readJSON(join(MODULE_DIR, k, "index.json"));
if (!v.modules) {
return {
type: "confirm",
message: "Enable " + v.title,
default: !!index.defaultEnabled,
name: k + ".isEnabled",
};
}
const fmt = (str, pref) => (str ? `${pref}${str}` : "");
if (!Object.keys(v.modules).length) {
return null;
}
return {
type: "checkbox",
name: k + ".enabled",
message: v.title,
loop: false,
choices: Object.entries(v.modules).map(([k2, v2]) => {
const possiblejson = join(MODULE_DIR, k, k2);
const j = {
...index.modules[k2],
...(extname(possiblejson) == ".json" ? readJSON(possiblejson) : {}),
};
return {
value: k2,
checked: !!j.defaultEnabled,
name: `${v2.description}${fmt(
v2.description == j.description ? null : j.description,
" - ",
)}`,
};
}),
};
})
.filter(Boolean),
{
type: "confirm",
name: "extendProfile",
message: "Extend an existing firefox profile?",
default: false,
},
{
type: "list",
name: "extendProfile.path",
when: (a) => a.extendProfile,
message: "Choose a profile to extend",
choices: readdirSync(PROFILES_PATH)
.filter((i) => lstatSync(resolve(PROFILES_PATH, i)).isDirectory())
.map((i) => ({
value: resolve(PROFILES_PATH, i),
name: i,
})),
},
{
type: "list",
name: "extendProfile.merge",
when: (a) => a.extendProfile?.path,
message: "How would you like to merge the profile?",
choices: [
{ name: "All files", value: "all" },
{ name: "Select specific files", value: "select" },
],
},
{
type: "checkbox",
when: (a) => a.extendProfile?.merge === "select",
message: "Select things to merge",
loop: false,
name: "extendProfile.choices",
choices: ["bookmarks", "history", "passwords", "cookies", "extensions"].map(
(i) => ({
value: i,
checked: i !== "cookies",
name: i[0].toUpperCase() + i.slice(1),
}),
),
},
...(OUTPUT_PATH_CLI
? []
: [
{
type: "input",
name: "outputsPath",
default: "outputs/profile",
message: "Profile output path",
validate: (i) =>
!existsSync(resolve(i)) || "Output path already exists",
},
]),
];
const tc = (fn) => {
try {
return fn();
} catch (e) {
return null;
}
};
const confirm = async (q) => {
if (PROFILE_PATH_CLI) {
return true;
}
return await inquirer
.prompt([
{
type: "confirm",
name: "conf",
message: q,
},
])
.then((a) => a.conf);
};
const showHelp = () => {
console.log(`
\x1b[1mUsage:\x1b[0m
${NAME} [options] [config file path] [output file path]
\x1b[33mNote: All arguments are optional, this is an interactive tool mainly.\x1b[0m
\x1b[1mOptions:\x1b[0m
\x1b[32m--help, -h\x1b[0m Show this help message
\x1b[32m--launch\x1b[0m Launch Firefox after profile creation
\x1b[32m--output <path>\x1b[0m Profile output path
`);
process.exit(0);
};
if (args.values.help) {
showHelp();
}
let results;
const conf_path = resolve(PROFILE_PATH_CLI || "./config.json");
if (
existsSync(conf_path) &&
(await confirm('Load profile config from "config.json?"'))
) {
results = readJSON(conf_path, true);
} else {
results = await inquirer.prompt(questions);
}
if (!results.outputsPath) {
results.outputsPath = OUTPUT_PATH_CLI;
}
results.outputsPath = resolve(results.outputsPath);
if (results.extendProfile) {
results.extendProfile = {
path: results.extendProfile.path,
...(results.extendProfile?.choices
? Object.fromEntries(results.extendProfile.choices.map((i) => [i, true]))
: {}),
...(results.extendProfile.merge === "all" ? { merge: "all" } : {}),
};
}
log.info("Writing profile config to " + resolve("./config.json"));
writeFileSync("config.json", JSON.stringify(results, null, 2));
if (await confirm(`Build profile to ${results.outputsPath} now?`)) {
await run(results);
const firefoxPath =
tc(() => execSync("which firefox").toString().trim()) || APP_PATH;
if (
PROFILE_PATH_CLI ? args.values.launch : await confirm("Open firefox now?")
) {
log.debug('Launching profile at "' + results.outputsPath + '"');
setTimeout(() => process.exit(0), 1000);
spawn(firefoxPath, ["--profile", results.outputsPath], {
detached: true,
});
} else {
console.log(
"Run firefox as follows:\n\t" +
`${firefoxPath || "firefox"} --profile ${results.outputsPath}`,
);
}
}