|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +// shamelessly copied from react devtools as it's doing exactly what i need. |
| 4 | + |
| 5 | +const { execSync } = require("child_process"); |
| 6 | +const { readFileSync, writeFileSync, createWriteStream } = require("fs"); |
| 7 | +const { join } = require("path"); |
| 8 | +const { copy, ensureDir, move, remove } = require("fs-extra"); |
| 9 | +const archiver = require("archiver"); |
| 10 | +const { getGitCommit } = require("./git"); |
| 11 | + |
| 12 | +// These files are copied along with Webpack-bundled files |
| 13 | +// to produce the final web extension |
| 14 | +const STATIC_FILES = ["icons", "pages", "lib"]; |
| 15 | + |
| 16 | +const preProcess = async (destinationPath, tempPath) => { |
| 17 | + await remove(destinationPath); // Clean up from previously completed builds |
| 18 | + await remove(tempPath); // Clean up from any previously failed builds |
| 19 | + await ensureDir(tempPath); // Create temp dir for this new build |
| 20 | +}; |
| 21 | + |
| 22 | +const build = async (tempPath, manifestPath) => { |
| 23 | + const binPath = join(tempPath, "bin"); |
| 24 | + const zipPath = join(tempPath, "zip"); |
| 25 | + |
| 26 | + const webpackPath = join(__dirname, "node_modules", ".bin", "webpack"); |
| 27 | + execSync( |
| 28 | + `${webpackPath} --config webpack.config.js --output-path ${binPath}`, |
| 29 | + { |
| 30 | + cwd: __dirname, |
| 31 | + env: process.env, |
| 32 | + stdio: "inherit", |
| 33 | + } |
| 34 | + ); |
| 35 | + // execSync( |
| 36 | + // `${webpackPath} --config webpack.backend.js --output-path ${binPath}`, |
| 37 | + // { |
| 38 | + // cwd: __dirname, |
| 39 | + // env: process.env, |
| 40 | + // stdio: 'inherit', |
| 41 | + // }, |
| 42 | + // ); |
| 43 | + |
| 44 | + // Make temp dir |
| 45 | + await ensureDir(zipPath); |
| 46 | + |
| 47 | + const copiedManifestPath = join(zipPath, "manifest.json"); |
| 48 | + |
| 49 | + // Copy unbuilt source files to zip dir to be packaged: |
| 50 | + await copy(binPath, join(zipPath, "dist")); |
| 51 | + await copy(manifestPath, copiedManifestPath); |
| 52 | + await Promise.all( |
| 53 | + STATIC_FILES.map((file) => copy(join(__dirname, file), join(zipPath, file))) |
| 54 | + ); |
| 55 | + |
| 56 | + const commit = getGitCommit(); |
| 57 | + const dateString = new Date().toLocaleDateString(); |
| 58 | + const manifest = JSON.parse(readFileSync(copiedManifestPath).toString()); |
| 59 | + const versionDateString = `${manifest.version} (${dateString})`; |
| 60 | + if (manifest.version_name) { |
| 61 | + // eslint-disable-next-line babel/camelcase |
| 62 | + manifest.version_name = versionDateString; |
| 63 | + } |
| 64 | + manifest.description += `\n\nCreated from revision ${commit} on ${dateString}.`; |
| 65 | + |
| 66 | + writeFileSync(copiedManifestPath, JSON.stringify(manifest, null, 2)); |
| 67 | + |
| 68 | + // Pack the extension |
| 69 | + const archive = archiver("zip", { zlib: { level: 9 } }); |
| 70 | + const zipStream = createWriteStream( |
| 71 | + join(tempPath, "TestingLibraryDevTools.zip") |
| 72 | + ); |
| 73 | + await new Promise((resolve, reject) => { |
| 74 | + archive |
| 75 | + .directory(zipPath, false) |
| 76 | + .on("error", (err) => reject(err)) |
| 77 | + .pipe(zipStream); |
| 78 | + archive.finalize(); |
| 79 | + zipStream.on("close", () => resolve()); |
| 80 | + }); |
| 81 | +}; |
| 82 | + |
| 83 | +const postProcess = async (tempPath, destinationPath) => { |
| 84 | + const unpackedSourcePath = join(tempPath, "zip"); |
| 85 | + const packedSourcePath = join(tempPath, "TestingLibraryDevTools.zip"); |
| 86 | + const packedDestPath = join(destinationPath, "TestingLibraryDevTools.zip"); |
| 87 | + const unpackedDestPath = join(destinationPath, "unpacked"); |
| 88 | + |
| 89 | + await move(unpackedSourcePath, unpackedDestPath); // Copy built files to destination |
| 90 | + await move(packedSourcePath, packedDestPath); // Copy built files to destination |
| 91 | + await remove(tempPath); // Clean up temp directory and files |
| 92 | +}; |
| 93 | + |
| 94 | +const main = async (buildId) => { |
| 95 | + const root = join(__dirname, buildId); |
| 96 | + const manifestPath = join(root, "manifest.json"); |
| 97 | + const destinationPath = join(root, "build"); |
| 98 | + |
| 99 | + try { |
| 100 | + const tempPath = join(__dirname, "build", buildId); |
| 101 | + await preProcess(destinationPath, tempPath); |
| 102 | + await build(tempPath, manifestPath); |
| 103 | + |
| 104 | + const builtUnpackedPath = join(destinationPath, "unpacked"); |
| 105 | + await postProcess(tempPath, destinationPath); |
| 106 | + |
| 107 | + return builtUnpackedPath; |
| 108 | + } catch (error) { |
| 109 | + // eslint-disable-next-line no-console |
| 110 | + console.error(error); |
| 111 | + // eslint-disable-next-line no-process-exit |
| 112 | + process.exit(1); |
| 113 | + } |
| 114 | + |
| 115 | + return null; |
| 116 | +}; |
| 117 | + |
| 118 | +module.exports = main; |
0 commit comments