angular/scripts/benchmarks/utils.mts
Paul Gschwendtner 899cb4ab49 refactor: add explicit types for exports relying on inferred call return type (#61316)
As part of the Bazel toolchain migration we noticed that implicit types
generated by the TypeScript compiler sometimes end up referencing types
from other packages (i.e. cross-package imports).

These imports currently work just because the Bazel `ts_library` and
`ng_module` rules automatically inserted a `<amd-module
name="@angular/x" />` into `.d.ts` of packages. This helped TS figure
out how to import a given file. Notably this is custom logic that is not
occuring in vanilla TS or Angular compilations—so we will drop this
magic as part of the toolchain cleanup!

To improve code quality and keep the existing behavior working, we are
doing the following:

- adding a lint rule that reduces the risk of such imports breaking. The
  failure scenario without the rule is that API goldens show unexpected
  diffs, and types might be duplicated in a different package!

- keeping the `<amd-module` headers, but we manually insert them into
  the package entry-points. This should ensure we don't regress
  anywhere; while we also improved general safety around this above.

Long-term, isolated declarations or a lint rule from eslint-typescript
can make this even more robust.

PR Close #61316
2025-05-13 22:46:00 +00:00

51 lines
1.4 KiB
TypeScript

/**
* @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.dev/license
*/
import {Log} from '@angular/ng-dev';
import childProcess from 'child_process';
import path from 'path';
import url from 'url';
const scriptDir = path.dirname(url.fileURLToPath(import.meta.url));
/** Absolute disk path to the project directory. */
export const projectDir: string = path.join(scriptDir, '../..');
/**
* Executes the given command, forwarding stdin, stdout and stderr while
* still capturing stdout in order to return it.
*/
export function exec(cmd: string, args: string[] = []): Promise<string> {
return new Promise((resolve, reject) => {
Log.info('Running command:', cmd, args.join(' '));
const proc = childProcess.spawn(cmd, args, {
shell: true,
cwd: projectDir,
// Only capture `stdout`. Forward the rest to the parent TTY.
stdio: ['inherit', 'pipe', 'inherit'],
});
let stdout = '';
proc.stdout.on('data', (chunk) => {
stdout += chunk.toString('utf8');
process.stdout.write(chunk);
});
proc.on('close', (status, signal) => {
if (status !== 0 || signal !== null) {
reject(`Command failed. Status code: ${status}. Signal: ${signal}`);
}
resolve(stdout);
});
proc.on('error', (err) => {
reject(`Command failed: ${err}`);
});
});
}