This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
/
copy-modules.js
173 lines (137 loc) 路 4.63 KB
/
copy-modules.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
const babel = require("@babel/core");
const glob = require("glob");
const fs = require("fs");
const path = require("path");
var shell = require("shelljs");
const minimist = require("minimist");
var chokidar = require('chokidar');
const feature = require("devtools-config");
const getConfig = require("./getConfig");
// Path to the mozilla-central clone is either passed via the --mc argument
// or read from the configuration.
const envConfig = getConfig();
feature.setConfig(envConfig);
function ignoreFile(file) {
// We exclude worker files because they are bundled and we include
// worker/index.js files because are required by the debugger app in order
// to communicate with the worker.
if (file.match(/\/workers/) && !file.match(/workers.js/) && !file.match(/index.js/)) {
return true;
}
return file.match(/(\/fixtures|__mocks__|\/test|vendors\.js|types\.js|types\/)/);
}
function getFiles() {
return glob.sync("./src/**/*.js", {}).filter((file) => !ignoreFile(file));
}
function copyFile(file) {
try {
if (ignoreFile(file)) {
return;
}
const filePath = path.join(__dirname, "..", file);
const code = fs.readFileSync(filePath, "utf8");
shell.mkdir("-p", path.join(mcDebuggerPath, path.dirname(file)));
fs.writeFileSync(path.join(mcDebuggerPath, file), code);
} catch (e) {
console.log(`Failed to copy: ${file}`)
console.error(e);
}
}
function copyFiles() {
getFiles().forEach(copyFile);
}
const MOZ_BUILD_TEMPLATE = `# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DIRS += [
__DIRS__
]
DebuggerModules(
__FILES__
)
`;
/**
* Create the mandatory manifest file that should exist in each folder to
* list files and subfolders that should be packaged in Firefox.
*/
function createMozBuildFiles() {
const builds = {};
getFiles().forEach(file => {
let dir = path.dirname(file);
builds[dir] = builds[dir] || { files: [], dirs: [] };
// Add the current file to its parent dir moz.build
builds[dir].files.push(path.basename(file));
// There should be a moz.build in every folder between the root and this
// file. Climb up the folder hierarchy and make sure a each folder of the
// chain is listing in its parent dir moz.build.
while (path.dirname(dir) != ".") {
const parentDir = path.dirname(dir);
const dirName = path.basename(dir);
builds[parentDir] = builds[parentDir] || { files: [], dirs: [] };
if (!builds[parentDir].dirs.includes(dirName)) {
builds[parentDir].dirs.push(dirName);
}
dir = parentDir;
}
});
Object.keys(builds).forEach(build => {
const { files, dirs } = builds[build];
const buildPath = path.join(mcDebuggerPath, build);
shell.mkdir("-p", buildPath);
// Files and folders should be alphabetically sorted in moz.build
const fileStr = files
.sort((a, b) => (a.toLowerCase() < b.toLowerCase() ? -1 : 1))
.map(file => ` '${file}',`)
.join("\n");
const dirStr = dirs
.sort((a, b) => (a.toLowerCase() < b.toLowerCase() ? -1 : 1))
.map(dir => ` '${dir}',`)
.join("\n");
const src = MOZ_BUILD_TEMPLATE
.replace("__DIRS__", dirStr)
.replace("__FILES__", fileStr);
fs.writeFileSync(path.join(buildPath, "moz.build"), src);
});
}
function watch() {
console.log("[copy-modules] start watching");
var watcher = chokidar.watch('./src').on('all', (event, path) => {});
watcher
.on('change', path => {
console.log(`Updating ${path}`)
copyFile(path)
})
}
function start() {
console.log("[copy-modules] start");
console.log("[copy-modules] copying debugger modules");
copyFiles();
console.log("[copy-modules] creating moz.build files");
createMozBuildFiles();
console.log("[copy-modules] done");
if (shouldWatch) {
watch();
}
}
const args = minimist(process.argv.slice(1), {
string: ["mc"],
boolean: ["watch"]
});
const projectPath = path.resolve(__dirname, "..");
let mcPath = args.mc || feature.getValue("firefox.mcPath");
let mcDebuggerPath = path.join(mcPath, "devtools/client/debugger/new");
let shouldWatch = args.watch;
function run({watch, mc}) {
shouldWatch = watch
mcPath = path.join(mc, "devtools/client/debugger/new");
start();
}
if (process.argv[1] == __filename) {
start();
} else {
module.exports = { run }
}