forked from philc/vimium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
executable file
·187 lines (165 loc) · 5.52 KB
/
make.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
#!/usr/bin/env node
// Usage: ./make.js command. Use -h for help.
// This is a set of tasks for building and testing Vimium in development.
fs = require("fs");
child_process = require("child_process");
// Spawns a new process and returns it.
function spawn(procName, optArray, silent = false, sync = true) {
if (process.platform == "win32") {
// if win32, prefix arguments with "/c {original command}"
// e.g. "mkdir c:\git\vimium" becomes "cmd.exe /c mkdir c:\git\vimium"
optArray.unshift("/c", procName)
procName = "cmd.exe"
}
proc = null
if (sync) {
proc = child_process.spawnSync(procName, optArray, {
stdio: [undefined, process.stdout, process.stderr]
});
} else {
proc = child_process.spawn(procName, optArray)
if (!silent) {
proc.stdout.on('data', (data) => process.stdout.write(data));
proc.stderr.on('data', (data) => process.stderr.write(data));
}
}
return proc;
}
// Builds a zip file for submission to the Chrome store. The output is in dist/.
function buildStorePackage() {
const excludeList = [
"*.md",
".*",
"CREDITS",
"MIT-LICENSE.txt",
"dist",
"make.js",
"node_modules",
"package-lock.json",
"test_harnesses",
"tests",
];
const manifestContents = require("./dist/vimium/manifest.json");
const rsyncOptions = ["-r", ".", "dist/vimium"].concat(
...excludeList.map((item) => ["--exclude", item])
);
const vimiumVersion = require("./manifest.json").version;
const writeDistManifest = (manifestObject) => {
fs.writeFileSync("dist/vimium/manifest.json", JSON.stringify(manifestObject, null, 2));
};
// cd into "dist/vimium" before building the zip, so that the files in the zip don't each have the
// path prefix "dist/vimium".
// --filesync ensures that files in the archive which are no longer on disk are deleted. It's equivalent to
// removing the zip file before the build.
const zipCommand = "cd dist/vimium && zip -r --filesync ";
spawn("rm", ["-rf", "dist/vimium"]);
spawn("mkdir", ["--parents", "dist/vimium", "dist/chrome-canary", "dist/chrome-store", "dist/firefox"]);
spawn("rsync", rsyncOptions);
writeDistManifest(Object.assign({}, manifestContents, {
// Chrome considers this key invalid in manifest.json, so we add it during the build phase.
browser_specific_settings: {
gecko: {
strict_min_version: "62.0",
},
},
}));
spawn("bash", ["-c", `${zipCommand} ../firefox/vimium-firefox-${vimiumVersion}.zip .`]);
// Build the Chrome Store package. Chrome does not require the clipboardWrite permission.
const permissions = manifestContents.permissions.filter((p) => p != "clipboardWrite");
writeDistManifest(Object.assign({}, manifestContents, {
permissions,
}));
spawn("bash", ["-c", `${zipCommand} ../chrome-store/vimium-chrome-store-${vimiumVersion}.zip .`]);
// Build the Chrome Store dev package.
writeDistManifest(Object.assign({}, manifestContents, {
name: "Vimium Canary",
description: "This is the development branch of Vimium (it is beta software).",
permissions,
}));
spawn("bash", ["-c", `${zipCommand} ../chrome-canary/vimium-canary-${vimiumVersion}.zip .`]);
}
// Returns how many tests failed.
function runUnitTests() {
console.log("Running unit tests...")
const basedir = __dirname + "/tests/unit_tests/";
fs.readdirSync(basedir).forEach((filename) => {
if (filename.endsWith("_test.js")) {
require(basedir + filename);
}
});
return Tests.run();
}
// Returns how many tests fail.
function runDomTests() {
const puppeteer = require("puppeteer");
const testFile = __dirname + "/tests/dom_tests/dom_tests.html";
(async () => {
const browser = await puppeteer.launch({
// NOTE(philc): "Disabling web security" is required for vomnibar_test.js, because we have a file://
// page accessing an iframe, and Chrome prevents this because it's a cross-origin request.
args: ['--disable-web-security']
});
const page = await browser.newPage();
page.on("console", msg => console.log(msg.text()));
page.on("error", (err) => console.log(err));
page.on("pageerror", (err) => console.log(err));
await page.goto("file://" + testFile);
const testsFailed = await page.evaluate(() => {
Tests.run();
return Tests.testsFailed;
});
await browser.close();
return testsFailed;
})();
}
// Prints the list of valid commands.
function printHelpString() {
console.log("Usage: ./make.js command\n\nValid commands:");
const keys = Object.keys(commands).sort();
for (let k of keys)
console.log(k, ":", commands[k].help);
}
const commands = []
// Defines a new command.
function command(name, helpString, fn) {
commands[name] = { help: helpString, fn: fn };
}
command(
"test",
"Run all tests",
() => {
const failed = runUnitTests() + runDomTests();
if (failed > 0)
Process.exit(failed);
});
command(
"test-unit",
"Run unit tests",
() => {
const failed = runUnitTests();
if (failed > 0)
Process.exit(failed);
});
command(
"test-dom",
"Run DOM tests",
() => {
const failed = runDomTests();
if (failed > 0)
Process.exit(failed);
});
command(
"package",
"Builds a zip file for submission to the Chrome store. The output is in dist/",
buildStorePackage);
if (process.argv.includes("-h") || process.argv.includes("--help") || process.argv.length == 2) {
printHelpString();
return;
}
commandArg = process.argv[2]
if (commands[commandArg]) {
commands[commandArg].fn();
} else {
printHelpString();
process.exit(1);
}