-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun.js
226 lines (213 loc) · 6.57 KB
/
run.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
import { cpSync, lstatSync, readdirSync, existsSync } from "fs";
import { join, resolve } from "path";
import readJSON from "./helpers/readJson";
import { ensureFolder } from "./helpers/ensure";
import log from "./helpers/log";
import copyFilesToProfile from "./helpers/copyFilesToProfile";
import { profile } from "console";
import { homedir, platform } from "os";
import contentcsshandle from "./handlers/contentcss";
import extensionshandle from "./handlers/extensions";
import repohandle from "./handlers/repo";
import userhandle from "./handlers/user";
import usercss from "./handlers/usercss";
import customjshandle from "./modules/custom-js/index";
import firefoxPaths from "./helpers/firefoxPaths";
import getFirefoxPaths from "./helpers/firefoxPaths";
import modulesList from "./helpers/allModules";
const HANDLERS = {
"handlers/contentcss.js": contentcsshandle,
"handlers/extensions.js": extensionshandle,
"handlers/repo.js": repohandle,
"handlers/user.js": userhandle,
"handlers/usercss.js": usercss,
"modules/custom-js/index.js": customjshandle,
};
const exec = require("util").promisify(require("child_process").exec);
const directories = {
modules: "modules",
};
/**
* @param {Object} config - Configuration object
* @returns {Promise<void>}
*/
export default async function run(config) {
console.assert(
(await exec('echo "hello world"').then((r) => r.stdout?.trim())) ==
"hello world",
);
config = {
outputsPath: "outputs/profile",
...config,
};
if (existsSync(config.outputsPath)) {
console.error("Outputs path already exists");
process.exit(1);
}
const profilePath = resolve(config.outputsPath);
if (config.extendProfile) {
config.extendProfile.path = config.extendProfile.path.replace(
/^\~/,
homedir(),
);
const path = resolve(config.extendProfile.path);
if (config.extendProfile.bookmarks && config.extendProfile.history) {
copyFilesToProfile(profilePath, [resolve(path, "places.sqlite")]);
}
if (config.extendProfile.passwords) {
copyFilesToProfile(profilePath, [resolve(path, "logins.json")]);
}
if (config.extendProfile.cookies) {
copyFilesToProfile(profilePath, [
resolve(path, "cookies.sqlite"),
resolve(path, "cookies.sqlite-wal"),
]);
}
if (config.extendProfile.extensions) {
copyFilesToProfile(profilePath, [
resolve(path, "extension-preferences.json"),
resolve(path, "extension-settings.json"),
]);
cpSync(resolve(path, "extensions"), resolve(profilePath, "extensions"), {
recursive: true,
});
cpSync(
resolve(path, "extension-store"),
resolve(profilePath, "extension-store"),
{
recursive: true,
},
);
cpSync(
resolve(path, "extension-store-menus"),
resolve(profilePath, "extension-store-menus"),
{
recursive: true,
},
);
for (let ext of readdirSync(resolve(path, "storage", "default")).filter(
(i) => i.startsWith("moz-extension++"),
)) {
ensureFolder(resolve(profilePath, "storage", "default"));
cpSync(
resolve(path, "storage", "default", ext),
resolve(profilePath, "storage", "default", ext),
{ recursive: true },
);
}
}
if (config.extendProfile.merge === "select") {
delete config.extendProfile.merge;
}
if (config.extendProfile.merge) {
const filesToMerge =
config.extendProfile.merge === "all"
? readdirSync(path)
: config.extendProfile.merge;
if (!Array.isArray(filesToMerge)) {
log.error("Invalid merge option: ", filesToMerge);
process.exit(1);
}
filesToMerge.forEach((file) => {
cpSync(resolve(path, file), resolve(profilePath, file), {
recursive: true,
});
});
}
}
const allModules = modulesList
.map((i) => ({
directory: join(directories.modules, i),
config: readJSON(join(directories.modules, i, "index.json")),
id: i,
}))
.sort((a, b) => a.config.buildOrder - b.config.buildOrder);
for (let module of allModules) {
let { config: moduleConfig } = module;
if (Array.isArray(config[module.id])) {
config[module.id] = { enabled: [...config[module.id]] };
}
let single = !!moduleConfig.single;
config[module.id] = config[module.id] || {};
config[module.id] = {
...(single
? {
isEnabled: moduleConfig.defaultEnabled,
}
: {
enabled: Object.entries(moduleConfig.modules)
.filter((i) => i[1].defaultEnabled == true)
.map((i) => i[0]),
options: {},
}),
...(moduleConfig.defaultConfig || {}),
...config[module.id],
};
if (config[module.id]?.extend) {
config[module.id].enabled = [
...config[module.id].enabled,
...config[module.id].extend,
];
}
if (config[module.id].isEnabled === false) {
continue;
}
if (single) {
config[module.id].enabled = ["index.js"];
console.log(module.directory);
moduleConfig.handler = join(directories.modules, module.id, "index.js");
}
config[module.id].enabled = config[module.id].enabled.map((i) => {
console.log(i, typeof i);
if (Array.isArray(i)) {
config[module.id].options[i[0]] = {
...(config[module.id].options[i[0]] || {}),
...i[1],
};
return i[0];
}
return i;
});
log.debug(`Running module`, module.id, "with config", config[module.id]);
const handle = HANDLERS[moduleConfig.handler];
let appPath = getFirefoxPaths().APP_PATH;
await handle({
...config[module.id],
profilePath,
appPath,
modulesPath: join(directories.modules, module.id),
options: config[module.id].options,
});
}
if (config.prefs) {
for (let [k, v] of Object.entries(config.prefs)) {
copyFilesToProfile(profilePath, [
{
name: "user.js",
append: true,
content:
"/// USER_CONFIG_PREF\n" +
`user_pref(${JSON.stringify(k)}, ${JSON.stringify(v)});`,
},
]);
}
}
if (config.userChrome) {
copyFilesToProfile(profilePath, [
{
name: "userChrome.css",
append: true,
content: "/// USER_CONFIG_CSS\n" + config.userChrome,
},
]);
}
if (config.userContent) {
copyFilesToProfile(profilePath, [
{
name: "userContent.css",
append: true,
content: "/// USER_CONFIG_CSS\n" + config.userContent,
},
]);
}
}