1. #!/usr/bin/env node
    
  2. 
    
  3. 'use strict';
    
  4. 
    
  5. const archiver = require('archiver');
    
  6. const {execSync} = require('child_process');
    
  7. const {readFileSync, writeFileSync, createWriteStream} = require('fs');
    
  8. const {copy, ensureDir, move, remove, pathExistsSync} = require('fs-extra');
    
  9. const {join, resolve} = require('path');
    
  10. const {getGitCommit} = require('./utils');
    
  11. 
    
  12. // These files are copied along with Webpack-bundled files
    
  13. // to produce the final web extension
    
  14. const STATIC_FILES = ['icons', 'popups', 'main.html', 'panel.html'];
    
  15. 
    
  16. /**
    
  17.  * Ensures that a local build of the dependencies exist either by downloading
    
  18.  * or running a local build via one of the `react-build-fordevtools*` scripts.
    
  19.  */
    
  20. const ensureLocalBuild = async () => {
    
  21.   const buildDir = resolve(__dirname, '..', '..', 'build');
    
  22.   const nodeModulesDir = join(buildDir, 'node_modules');
    
  23. 
    
  24.   // TODO: remove this check whenever the CI pipeline is complete.
    
  25.   // See build-all-release-channels.js
    
  26.   const currentBuildDir = resolve(
    
  27.     __dirname,
    
  28.     '..',
    
  29.     '..',
    
  30.     'build',
    
  31.     'oss-experimental',
    
  32.   );
    
  33. 
    
  34.   if (pathExistsSync(buildDir)) {
    
  35.     return; // all good.
    
  36.   }
    
  37. 
    
  38.   if (pathExistsSync(currentBuildDir)) {
    
  39.     await ensureDir(buildDir);
    
  40.     await copy(currentBuildDir, nodeModulesDir);
    
  41.     return; // all good.
    
  42.   }
    
  43. 
    
  44.   throw Error(
    
  45.     'Could not find build artifacts in repo root. See README for prerequisites.',
    
  46.   );
    
  47. };
    
  48. 
    
  49. const preProcess = async (destinationPath, tempPath) => {
    
  50.   await remove(destinationPath); // Clean up from previously completed builds
    
  51.   await remove(tempPath); // Clean up from any previously failed builds
    
  52.   await ensureDir(tempPath); // Create temp dir for this new build
    
  53. };
    
  54. 
    
  55. const build = async (tempPath, manifestPath, envExtension = {}) => {
    
  56.   const binPath = join(tempPath, 'bin');
    
  57.   const zipPath = join(tempPath, 'zip');
    
  58.   const mergedEnv = {...process.env, ...envExtension};
    
  59. 
    
  60.   const webpackPath = join(__dirname, 'node_modules', '.bin', 'webpack');
    
  61.   execSync(
    
  62.     `${webpackPath} --config webpack.config.js --output-path ${binPath}`,
    
  63.     {
    
  64.       cwd: __dirname,
    
  65.       env: mergedEnv,
    
  66.       stdio: 'inherit',
    
  67.     },
    
  68.   );
    
  69.   execSync(
    
  70.     `${webpackPath} --config webpack.backend.js --output-path ${binPath}`,
    
  71.     {
    
  72.       cwd: __dirname,
    
  73.       env: mergedEnv,
    
  74.       stdio: 'inherit',
    
  75.     },
    
  76.   );
    
  77. 
    
  78.   // Make temp dir
    
  79.   await ensureDir(zipPath);
    
  80. 
    
  81.   const copiedManifestPath = join(zipPath, 'manifest.json');
    
  82. 
    
  83.   // Copy unbuilt source files to zip dir to be packaged:
    
  84.   await copy(binPath, join(zipPath, 'build'));
    
  85.   await copy(manifestPath, copiedManifestPath);
    
  86.   await Promise.all(
    
  87.     STATIC_FILES.map(file => copy(join(__dirname, file), join(zipPath, file))),
    
  88.   );
    
  89. 
    
  90.   const commit = getGitCommit();
    
  91.   const dateString = new Date().toLocaleDateString();
    
  92.   const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());
    
  93.   const versionDateString = `${manifest.version} (${dateString})`;
    
  94.   if (manifest.version_name) {
    
  95.     manifest.version_name = versionDateString;
    
  96.   }
    
  97.   manifest.description += `\n\nCreated from revision ${commit} on ${dateString}.`;
    
  98. 
    
  99.   if (process.env.NODE_ENV === 'development') {
    
  100.     // When building the local development version of the
    
  101.     // extension we want to be able to have a stable extension ID
    
  102.     // for the local build (in order to be able to reliably detect
    
  103.     // duplicate installations of DevTools).
    
  104.     // By specifying a key in the built manifest.json file,
    
  105.     // we can make it so the generated extension ID is stable.
    
  106.     // For more details see the docs here: https://developer.chrome.com/docs/extensions/mv2/manifest/key/
    
  107.     manifest.key = 'reactdevtoolslocalbuilduniquekey';
    
  108.   }
    
  109. 
    
  110.   writeFileSync(copiedManifestPath, JSON.stringify(manifest, null, 2));
    
  111. 
    
  112.   // Pack the extension
    
  113.   const archive = archiver('zip', {zlib: {level: 9}});
    
  114.   const zipStream = createWriteStream(join(tempPath, 'ReactDevTools.zip'));
    
  115.   await new Promise((resolvePromise, rejectPromise) => {
    
  116.     archive
    
  117.       .directory(zipPath, false)
    
  118.       .on('error', err => rejectPromise(err))
    
  119.       .pipe(zipStream);
    
  120.     archive.finalize();
    
  121.     zipStream.on('close', () => resolvePromise());
    
  122.   });
    
  123. };
    
  124. 
    
  125. const postProcess = async (tempPath, destinationPath) => {
    
  126.   const unpackedSourcePath = join(tempPath, 'zip');
    
  127.   const packedSourcePath = join(tempPath, 'ReactDevTools.zip');
    
  128.   const packedDestPath = join(destinationPath, 'ReactDevTools.zip');
    
  129.   const unpackedDestPath = join(destinationPath, 'unpacked');
    
  130. 
    
  131.   await move(unpackedSourcePath, unpackedDestPath); // Copy built files to destination
    
  132.   await move(packedSourcePath, packedDestPath); // Copy built files to destination
    
  133.   await remove(tempPath); // Clean up temp directory and files
    
  134. };
    
  135. 
    
  136. const SUPPORTED_BUILDS = ['chrome', 'firefox', 'edge'];
    
  137. 
    
  138. const main = async buildId => {
    
  139.   if (!SUPPORTED_BUILDS.includes(buildId)) {
    
  140.     throw new Error(
    
  141.       `Unexpected build id - "${buildId}". Use one of ${JSON.stringify(
    
  142.         SUPPORTED_BUILDS,
    
  143.       )}.`,
    
  144.     );
    
  145.   }
    
  146. 
    
  147.   const root = join(__dirname, buildId);
    
  148.   const manifestPath = join(root, 'manifest.json');
    
  149.   const destinationPath = join(root, 'build');
    
  150. 
    
  151.   const envExtension = {
    
  152.     IS_CHROME: buildId === 'chrome',
    
  153.     IS_FIREFOX: buildId === 'firefox',
    
  154.     IS_EDGE: buildId === 'edge',
    
  155.   };
    
  156. 
    
  157.   try {
    
  158.     const tempPath = join(__dirname, 'build', buildId);
    
  159.     await ensureLocalBuild();
    
  160.     await preProcess(destinationPath, tempPath);
    
  161.     await build(tempPath, manifestPath, envExtension);
    
  162. 
    
  163.     const builtUnpackedPath = join(destinationPath, 'unpacked');
    
  164.     await postProcess(tempPath, destinationPath);
    
  165. 
    
  166.     return builtUnpackedPath;
    
  167.   } catch (error) {
    
  168.     console.error(error);
    
  169.     process.exit(1);
    
  170.   }
    
  171. 
    
  172.   return null;
    
  173. };
    
  174. 
    
  175. module.exports = main;