https://github.com/angular/angular
Raw File
Tip revision: 55d412c5b1b0ba9b03174f7ad9907961fcafa970 authored by Dylan Hunn on 13 July 2023, 17:58:22 UTC
release: cut the v16.2.0-next.2 release
Tip revision: 55d412c
cli.ts
/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import {runfiles} from '@bazel/runfiles';
import * as fs from 'fs';

import {SymbolExtractor} from './symbol_extractor';

const args = process.argv.slice(2) as [string, string];
process.exitCode = main(args) ? 0 : 1;

/**
 * CLI main method.
 *
 * ```
 *   cli javascriptFilePath.js goldenFilePath.json
 * ```
 */
function main(argv: [string, string, string]|[string, string]): boolean {
  const javascriptFilePath = runfiles.resolveWorkspaceRelative(argv[0]);
  const goldenFilePath = runfiles.resolveWorkspaceRelative(argv[1]);
  const doUpdate = argv[2] == '--accept';

  console.info('Input javascript file:', javascriptFilePath);

  const javascriptContent = fs.readFileSync(javascriptFilePath).toString();
  const goldenContent = fs.readFileSync(goldenFilePath).toString();

  const symbolExtractor = new SymbolExtractor(javascriptFilePath, javascriptContent);

  let passed: boolean = false;
  if (doUpdate) {
    fs.writeFileSync(goldenFilePath, JSON.stringify(symbolExtractor.actual, undefined, 2));
    console.error('Updated gold file:', goldenFilePath);
    passed = true;
  } else {
    passed = symbolExtractor.compareAndPrintError(goldenFilePath, goldenContent);
    if (!passed) {
      console.error(`TEST FAILED!`);
      console.error(`  To update the golden file run: `);
      console.error(`    yarn bazel run ${process.env['TEST_TARGET']}.accept`);
    }
  }
  return passed;
}
back to top