function buildTargetPackages()

in scripts/package-builder.js [52:107]


function buildTargetPackages(destDir, description, isRelease = false) {
  console.info('##################################');
  console.info(`${scriptPath}:`);
  console.info('  Building @nguniversal/* npm packages');
  console.info(`  Mode: ${description}`);
  console.info('##################################');

  /** The list of packages which were built. */
  const builtPackages = [];
  // List of targets to build, e.g. core, common, compiler, etc. Note that we want to also remove
  // all carriage return (`\r`) characters form the query output, because otherwise the carriage
  // return is part of the bazel target name and bazel will complain.
  // eslint-disable-next-line max-len
  const getTargetsCmd = `${bazelCmd} query --output=label "attr('tags', '\\[.*release\\]', //modules/...) intersect kind('ng_package|pkg_npm', //modules/...)"`;
  const targets = exec(getTargetsCmd, true).split(/\r?\n/);

  // If we are in release mode, run `bazel clean` to ensure the execroot and action cache
  // are not populated. This is necessary because targets using `npm_package` rely on
  // workspace status variables for the package version. Such NPM package targets are not
  // rebuilt if only the workspace status variables change. This could result in accidental
  // re-use of previously built package output with a different `version` in the `package.json`.
  if (isRelease) {
    console.info('Building in release mode. Resetting the Bazel execroot and action cache..');
    exec(`${bazelCmd} clean`);
  }

  // Use either `--config=snapshot` or `--config=release` so that builds are created with the
  // correct embedded version info.
  exec(`${bazelCmd} build --config=${isRelease ? 'release' : 'snapshot'} ${targets.join(' ')}`);

  // Create the output directory.
  const absDestDir = resolve(baseDir, destDir);
  if (!test('-d', absDestDir)) {
    mkdir('-p', absDestDir);
  }

  targets.forEach((target) => {
    const pkg = target.replace(/\/\/modules\/(.*):npm_package/, '$1');

    // Skip any that don't have an "npm_package" target.
    const srcDir = `${bazelBin}/modules/${pkg}/npm_package`;
    const destDir = `${absDestDir}/${pkg}`;

    if (test('-d', srcDir)) {
      console.info(`# Copy artifacts to ${destDir}`);
      rm('-rf', destDir);
      cp('-R', srcDir, destDir);
      chmod('-R', 'u+w', destDir);
      builtPackages.push({ name: `@nguniversal/${pkg}`, outputPath: destDir });
    }
  });

  console.info('');

  return builtPackages;
}