forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-modules.js
More file actions
183 lines (147 loc) · 5 KB
/
copy-modules.js
File metadata and controls
183 lines (147 loc) · 5 KB
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
/* 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) {
return file.match(/(mochitest)/) || fs.statSync(file).isDirectory();
}
function getFiles() {
return [...glob.sync("./src/**/*", {}), ...glob.sync("./packages/**/*", {})].filter(file => !ignoreFile(file));
}
function copyFiles() {
getFiles().forEach(file => {
try {
if (ignoreFile(file)) {
console.log("IGNORING File: ", file);
return;
}
console.log("COPYING File: ", file);
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);
}
});
}
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__
]
CompiledModules(
__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()
.filter(file => file.match(/.js$/))
.filter(file => {
if (file.match(/\/workers\.js/)) {
return true;
}
// 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/)) {
return file.match(/workers\/(\w|-)*\/index.js/);
}
return !file.match(/(test|types|packages)/)
})
.forEach(file => {
// console.log(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");
const mcDebuggerPath = path.join(mcPath, "devtools/client/debugger");
let shouldWatch = args.watch;
function run({ watch, mc }) {
shouldWatch = watch;
mcPath = path.join(mc, "devtools/client/debugger");
start();
}
if (process.argv[1] == __filename) {
start();
} else {
module.exports = { run };
}