forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-assets.js
More file actions
256 lines (216 loc) · 6.99 KB
/
copy-assets.js
File metadata and controls
256 lines (216 loc) · 6.99 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const {
tools: { makeBundle, symlinkTests, copyFile }
} = require("devtools-launchpad/index");
const path = require("path");
const minimist = require("minimist");
var fs = require("fs");
var fsExtra = require("fs-extra");
const rimraf = require("rimraf");
const feature = require("devtools-config");
const getConfig = require("./getConfig");
const writeReadme = require("./writeReadme");
const envConfig = getConfig();
feature.setConfig(envConfig);
const args = minimist(process.argv.slice(2), {
boolean: ["watch", "symlink", "assets"],
string: ["mc"]
});
const shouldSymLink = args.symlink;
const updateAssets = args.assets;
const watch = args.watch;
function moveFile(src, dest, opts) {
copyFile(src, dest, opts);
rimraf.sync(src);
}
function updateFile(filename, cbk) {
var text = fs.readFileSync(filename, "utf-8");
fs.writeFileSync(filename, cbk(text), "utf-8");
}
function searchText(text, regexp) {
let matches = [];
let match;
do {
match = regexp.exec(text);
if (match) {
matches.push(match[1]);
}
} while (match);
return matches;
}
const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
? walkSync(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file));
});
return filelist;
};
function copySVGs({ projectPath, mcPath }) {
/*
* Copying SVGs
* We want to copy the SVGs that we include in our CSS into the
* MC devtools/client/themes directory. To do this, we look for the
* SVGs that are inlcuded in our CSS files and then copy the files
* and include them in the jar.mn file
*/
const projectImagesPath = path.join(projectPath, "assets/images/");
const mcImagesPath = path.join(
mcPath,
"devtools/client/themes/images/debugger"
);
let usedSvgs = [];
const svgTest = new RegExp(/url\(\/images\/(.*)\)/, "g");
const cssFiles = walkSync(path.join(projectPath, "src/components"))
.filter(file => file.match(/css$/))
.forEach(file =>
usedSvgs.push(...searchText(fs.readFileSync(file, "utf-8"), svgTest))
);
const files = fs
.readdirSync(projectImagesPath)
.filter(file => file.match(/svg$/))
.filter(file => usedSvgs.includes(file));
rimraf.sync(mcImagesPath);
files.forEach(file =>
fsExtra.copySync(
path.join(projectImagesPath, file),
path.join(mcImagesPath, `${file}`)
)
);
const newText =
files
.map(
file =>
` skin/images/debugger/${file} (themes/images/debugger/${file})`
)
.join("\n") + "\n";
const mcJarPath = path.join(mcPath, "devtools/client/jar.mn");
updateFile(mcJarPath, text => {
const newJar = text.replace(
/(.*skin\/images\/debugger\/.*$\n)+/gm,
newText
);
return newJar;
});
}
function copyTests({ mcPath, projectPath, mcModulePath, shouldSymLink }) {
const projectTestPath = path.join(projectPath, "src/test/mochitest");
const mcTestPath = path.join(mcPath, mcModulePath, "test/mochitest");
if (shouldSymLink) {
symlinkTests({ projectPath, mcTestPath, projectTestPath });
} else {
// we should rm the test dir first
rimraf.sync(mcTestPath);
copyFile(projectTestPath, mcTestPath, { cwd: projectPath });
}
}
function copyWithReplace(source, target, { cwd }, what, replacement) {
if (cwd) {
source = path.resolve(cwd, source);
target = path.resolve(cwd, target);
}
const content = fs.readFileSync(source).toString();
const replaced = content.replace(what, replacement);
fs.writeFileSync(target, replaced);
}
function copyWasmParser({ mcPath, projectPath }) {
copyWithReplace(
require.resolve("wasmparser/dist/WasmParser.js"),
path.join(mcPath, "devtools/client/shared/vendor/WasmParser.js"),
{ cwd: projectPath },
/^\/\/# sourceMappingURL=[^\n]*/m,
""
);
copyWithReplace(
require.resolve("wasmparser/dist/WasmDis.js"),
path.join(mcPath, "devtools/client/shared/vendor/WasmDis.js"),
{ cwd: projectPath },
/^\/\/# sourceMappingURL=[^\n]*/m,
""
);
const wasmparserPackageLocation = require.resolve("wasmparser/package.json");
const wasmparserVersion = JSON.parse(
fs.readFileSync(wasmparserPackageLocation).toString()
).version;
copyWithReplace(
path.join(projectPath, "./assets/panel/WASMPARSER_UPGRADING"),
path.join(mcPath, "devtools/client/shared/vendor/WASMPARSER_UPGRADING"),
{ cwd: projectPath },
/\$\(WASMPARSER_VERSION\)/g,
wasmparserVersion
);
}
function start() {
console.log("start: copy assets");
const projectPath = path.resolve(__dirname, "..");
const mcModulePath = "devtools/client/debugger/new";
let mcPath = args.mc ? args.mc : feature.getValue("firefox.mcPath");
process.env.NODE_ENV = "production";
// resolving against the project path in case it's relative. If it's absolute
// it will override whatever is in projectPath.
mcPath = path.resolve(projectPath, mcPath);
const config = { shouldSymLink, mcPath, projectPath, mcModulePath };
copyFile(
path.join(projectPath, "./assets/panel/debugger.properties"),
path.join(mcPath, "devtools/client/locales/en-US/debugger.properties"),
{ cwd: projectPath }
);
copyFile(
path.join(projectPath, "./assets/panel/prefs.js"),
path.join(mcPath, "devtools/client/preferences/debugger.js"),
{ cwd: projectPath }
);
copyFile(
path.join(projectPath, "./assets/panel/index.html"),
path.join(mcPath, "devtools/client/debugger/new/index.html"),
{ cwd: projectPath }
);
copyFile(
path.join(projectPath, "./assets/panel/panel.js"),
path.join(mcPath, "devtools/client/debugger/new/panel.js"),
{ cwd: projectPath }
);
copyFile(
path.join(projectPath, "./assets/panel/moz.build"),
path.join(mcPath, "devtools/client/debugger/new/moz.build"),
{ cwd: projectPath }
);
copySVGs(config);
copyTests(config);
copyWasmParser(config);
writeReadme(path.join(mcPath, "devtools/client/debugger/new/README.mozilla"));
const debuggerPath = "devtools/client/debugger/new"
if (!mcPath.startsWith(projectPath)) {
rimraf.sync(path.join(
mcPath,
debuggerPath,
"test/mochitest/examples/babel/source-maps-semantics.md"
));
}
makeBundle({
outputPath: path.join(mcPath, mcModulePath),
projectPath,
watch,
updateAssets
})
.then(() => onBundleFinish({mcPath, debuggerPath, projectPath}))
.catch(err => {
console.log(
"Uhoh, something went wrong. The error was written to assets-error.log"
);
fs.writeFileSync("assets-error.log", JSON.stringify(err, null, 2));
});
}
function onBundleFinish({mcPath, debuggerPath, projectPath}) {
console.log("done: copy assets");
moveFile(
path.join(mcPath, debuggerPath, "source-map-worker.js"),
path.join(mcPath, "devtools/client/shared/source-map/worker.js"),
{cwd: projectPath}
);
moveFile(
path.join(mcPath, debuggerPath, "source-map-index.js"),
path.join(mcPath, "devtools/client/shared/source-map/index.js"),
{cwd: projectPath}
);
}
start();