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