This repository was archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.js
118 lines (98 loc) · 3.86 KB
/
build.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
#!/usr/bin/env node
// shamelessly copied from react devtools as it's doing exactly what i need.
const { execSync } = require("child_process");
const { readFileSync, writeFileSync, createWriteStream } = require("fs");
const { join } = require("path");
const { copy, ensureDir, move, remove } = require("fs-extra");
const archiver = require("archiver");
// const { getGitCommit } = require("./git");
// These files are copied along with Webpack-bundled files
// to produce the final web extension
const STATIC_FILES = ["icons", "pages", "lib"];
const preProcess = async (destinationPath, tempPath) => {
await remove(destinationPath); // Clean up from previously completed builds
await remove(tempPath); // Clean up from any previously failed builds
await ensureDir(tempPath); // Create temp dir for this new build
};
const build = async (tempPath, manifestPath) => {
const binPath = join(tempPath, "bin");
const zipPath = join(tempPath, "zip");
const webpackPath = join(__dirname, "node_modules", ".bin", "webpack");
execSync(
`${webpackPath} --config webpack.config.js --output-path ${binPath}`,
{
cwd: __dirname,
env: process.env,
stdio: "inherit",
}
);
// execSync(
// `${webpackPath} --config webpack.backend.js --output-path ${binPath}`,
// {
// cwd: __dirname,
// env: process.env,
// stdio: 'inherit',
// },
// );
// Make temp dir
await ensureDir(zipPath);
const copiedManifestPath = join(zipPath, "manifest.json");
// Copy unbuilt source files to zip dir to be packaged:
await copy(binPath, join(zipPath, "dist"));
await copy(manifestPath, copiedManifestPath);
await Promise.all(
STATIC_FILES.map((file) => copy(join(__dirname, file), join(zipPath, file)))
);
// const commit = getGitCommit();
const dateString = new Date().toLocaleDateString();
const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());
const versionDateString = `${manifest.version} (${dateString})`;
if (manifest.version_name) {
// eslint-disable-next-line babel/camelcase
manifest.version_name = versionDateString;
}
// manifest.description += `\n\nCreated from revision ${commit} on ${dateString}.`;
writeFileSync(copiedManifestPath, JSON.stringify(manifest, null, 2));
// Pack the extension
const archive = archiver("zip", { zlib: { level: 9 } });
const zipStream = createWriteStream(
join(tempPath, "TestingLibraryDevTools.zip")
);
await new Promise((resolve, reject) => {
archive
.directory(zipPath, false)
.on("error", (err) => reject(err))
.pipe(zipStream);
archive.finalize();
zipStream.on("close", () => resolve());
});
};
const postProcess = async (tempPath, destinationPath) => {
const unpackedSourcePath = join(tempPath, "zip");
const packedSourcePath = join(tempPath, "TestingLibraryDevTools.zip");
const packedDestPath = join(destinationPath, "TestingLibraryDevTools.zip");
const unpackedDestPath = join(destinationPath, "unpacked");
await move(unpackedSourcePath, unpackedDestPath); // Copy built files to destination
await move(packedSourcePath, packedDestPath); // Copy built files to destination
await remove(tempPath); // Clean up temp directory and files
};
const main = async (buildId) => {
const root = join(__dirname, buildId);
const manifestPath = join(root, "manifest.json");
const destinationPath = join(root, "build");
try {
const tempPath = join(__dirname, "build", buildId);
await preProcess(destinationPath, tempPath);
await build(tempPath, manifestPath);
const builtUnpackedPath = join(destinationPath, "unpacked");
await postProcess(tempPath, destinationPath);
return builtUnpackedPath;
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
return null;
};
module.exports = main;