Skip to content

Commit ae5fa72

Browse files
lekhmanruscrisbeto
authored andcommitted
build: convert approve-api-golden and run-component-tests scripts to .mts to support updated chalk (#31966)
(cherry picked from commit 3cbe5a0)
1 parent 07e9f0a commit ae5fa72

File tree

4 files changed

+67
-71
lines changed

4 files changed

+67
-71
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"build-and-check-release-output": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only scripts/build-and-check-release-output.mts",
2323
"dev-app": "ibazel run //src/dev-app:devserver",
2424
"universal-app": "bazel run //src/universal-app:server",
25-
"test": "node ./scripts/run-component-tests.js",
25+
"test": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only ./scripts/run-component-tests.mts",
2626
"test-local": "pnpm -s test --local",
2727
"test-firefox": "pnpm -s test --firefox",
2828
"test-tsec": "pnpm bazelisk test //... --build_tag_filters=tsec --test_tag_filters=tsec",
@@ -42,7 +42,7 @@
4242
"ts-circular-deps:check": "pnpm -s ng-dev ts-circular-deps check --config ./src/circular-deps-test.conf.cjs",
4343
"ts-circular-deps:approve": "pnpm -s ng-dev ts-circular-deps approve --config ./src/circular-deps-test.conf.cjs",
4444
"merge": "pnpm -s ng-dev pr merge",
45-
"approve-api": "node ./scripts/approve-api-golden.js",
45+
"approve-api": "node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only ./scripts/approve-api-golden.mts",
4646
"integration-tests": "bazel test --test_tag_filters=-linker-integration-test --build_tests_only -- //integration/...",
4747
"test-linker-aot": "bazel test --partial_compilation --test_tag_filters=partial-compilation-integration,-firefox --build_tests_only -- //integration/... //src/...",
4848
"test-linker-jit": "bazel test --partial_compilation --test_tag_filters=partial-compilation-integration,-firefox --build_tests_only --//tools:force_partial_jit_compilation=True -- //integration/... //src/...",
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
#!/usr/bin/env node
22

3-
const shelljs = require('shelljs');
4-
const chalk = require('chalk');
5-
const path = require('path');
6-
const {guessPackageName} = require('./util');
7-
const projectDir = path.join(__dirname, '../');
3+
import chalk from 'chalk';
4+
import {join} from 'path';
5+
import sh from 'shelljs';
6+
import {guessPackageName} from './util.mjs';
7+
88
const bazel = process.env['BAZEL'] || 'pnpm -s bazel';
99

1010
if (process.argv.length < 3) {
1111
console.error(chalk.red('No package name has been passed in for API golden approval.'));
1212
process.exit(1);
1313
}
1414

15-
// ShellJS should exit if any command fails.
16-
shelljs.set('-e');
17-
shelljs.cd(projectDir);
15+
sh.set('-e');
1816

1917
for (const searchPackageName of process.argv.slice(2)) {
20-
const packageNameGuess = guessPackageName(searchPackageName, path.join(projectDir, 'src'));
18+
const packageNameGuess = guessPackageName(searchPackageName, join(process.cwd(), 'src'));
2119

2220
if (!packageNameGuess.result) {
2321
console.error(
@@ -30,8 +28,8 @@ for (const searchPackageName of process.argv.slice(2)) {
3028
process.exit(1);
3129
}
3230

33-
const [packageName, ..._entryPointTail] = packageNameGuess.result.split('/');
31+
const [packageName] = packageNameGuess.result.split('/');
3432
const apiGoldenTargetName = `//goldens:${packageName}_api.accept`.replace(/-/g, '_');
3533

36-
shelljs.exec(`${bazel} run ${apiGoldenTargetName}`);
34+
sh.exec(`${bazel} run ${apiGoldenTargetName}`);
3735
}

scripts/run-component-tests.js renamed to scripts/run-component-tests.mts

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,31 @@
1717
* --no-watch | Watch mode is enabled by default. This flag opts-out to standard Bazel.
1818
*/
1919

20-
const yargs = require('yargs');
21-
const shelljs = require('shelljs');
22-
const chalk = require('chalk');
23-
const path = require('path');
24-
const args = process.argv.slice(2);
25-
const {guessPackageName, convertPathToPosix} = require('./util');
20+
import chalk from 'chalk';
21+
import {join, relative} from 'path';
22+
import sh from 'shelljs';
23+
import yargs from 'yargs';
24+
import {guessPackageName, convertPathToPosix} from './util.mjs';
2625

27-
// Path to the project directory.
28-
const projectDir = path.join(__dirname, '../');
26+
const args = process.argv.slice(2);
2927

3028
// Path to the directory that contains all packages.
31-
const packagesDir = path.join(projectDir, 'src/');
29+
const packagesDir = join(process.cwd(), 'src/');
3230

3331
// ShellJS should exit if any command fails.
34-
shelljs.set('-e');
35-
shelljs.cd(projectDir);
32+
sh.set('-e');
33+
34+
interface CliArgs {
35+
components: string[];
36+
debug: boolean;
37+
firefox: boolean;
38+
watch: boolean;
39+
}
3640

3741
// Extracts the supported command line options.
38-
const {components, debug, firefox, watch} = yargs(args)
42+
const parser = yargs(args)
3943
.command('* <components..>', 'Run tests for specified components', args =>
40-
args.positional('components', {type: 'array'}),
44+
args.positional('components', {type: 'string', array: true}),
4145
)
4246
.option('debug', {
4347
alias: 'local',
@@ -53,8 +57,9 @@ const {components, debug, firefox, watch} = yargs(args)
5357
default: true,
5458
description: 'Whether tests should be re-run automatically upon changes.',
5559
})
56-
.strict()
57-
.parseSync();
60+
.strict();
61+
62+
const {components, debug, firefox, watch} = parser.parseSync() as unknown as CliArgs;
5863

5964
// Whether tests for all components should be run.
6065
const all = components.length === 1 && components[0] === 'all';
@@ -84,51 +89,49 @@ if (all) {
8489
console.warn(chalk.yellow('Unable to run all component tests in watch mode.'));
8590
console.warn(chalk.yellow('Tests will be run in non-watch mode..'));
8691
}
87-
shelljs.exec(
92+
sh.exec(
8893
`pnpm -s bazel test --test_tag_filters=-e2e,browser:${browserName} ` +
8994
`--build_tag_filters=browser:${browserName} --build_tests_only //src/...`,
9095
);
91-
return;
92-
}
96+
} else {
97+
// Exit if no component has been specified.
98+
if (!components.length) {
99+
console.error(
100+
chalk.red(
101+
'No component specified. Please either specify individual components, or pass "all" ' +
102+
'in order to run tests for all components.',
103+
),
104+
);
105+
console.info(chalk.yellow('Below are a few examples of how the script can be run:'));
106+
console.info(chalk.yellow(` - pnpm test all`));
107+
console.info(chalk.yellow(` - pnpm test cdk/overlay material/stepper`));
108+
console.info(chalk.yellow(` - pnpm test button toolbar`));
109+
process.exit(1);
110+
}
93111

94-
// Exit if no component has been specified.
95-
if (!components.length) {
96-
console.error(
97-
chalk.red(
98-
'No component specified. Please either specify individual components, or pass "all" ' +
99-
'in order to run tests for all components.',
100-
),
112+
const bazelAction = debug ? 'run' : 'test';
113+
const testLabels = components.map(
114+
t => `${getBazelPackageOfComponentName(t)}:${getTargetName(t)}`,
101115
);
102-
console.info(chalk.yellow('Below are a few examples of how the script can be run:'));
103-
console.info(chalk.yellow(` - pnpm test all`));
104-
console.info(chalk.yellow(` - pnpm test cdk/overlay material/stepper`));
105-
console.info(chalk.yellow(` - pnpm test button toolbar`));
106-
process.exit(1);
107-
}
108-
109-
const bazelAction = debug ? 'run' : 'test';
110-
const testLabels = components.map(t => `${getBazelPackageOfComponentName(t)}:${getTargetName(t)}`);
111-
112-
// Runs Bazel for the determined test labels.
113-
shelljs.exec(`${bazelBinary} ${bazelAction} ${testLabels.join(' ')}`);
114116

117+
// Runs Bazel for the determined test labels.
118+
sh.exec(`${bazelBinary} ${bazelAction} ${testLabels.join(' ')}`);
119+
}
115120
/**
116121
* Gets the Bazel package label for the specified component name. Throws if
117122
* the component could not be resolved to a Bazel package.
118123
*/
119-
function getBazelPackageOfComponentName(name) {
124+
function getBazelPackageOfComponentName(name: string) {
120125
// Before guessing any Bazel package, we test if the name contains the
121126
// package name already. If so, we just use that for Bazel package.
122127
const targetName =
123-
convertPathToBazelLabel(name) || convertPathToBazelLabel(path.join(packagesDir, name));
128+
convertPathToBazelLabel(name) || convertPathToBazelLabel(join(packagesDir, name));
124129
if (targetName !== null) {
125130
return targetName;
126131
}
127132
// If the name does not contain an explicit package name, try to guess it.
128133
const guess = guessPackageName(name, packagesDir);
129-
const guessLabel = guess.result
130-
? convertPathToBazelLabel(path.join(packagesDir, guess.result))
131-
: null;
134+
const guessLabel = guess.result ? convertPathToBazelLabel(join(packagesDir, guess.result)) : null;
132135

133136
if (guessLabel) {
134137
return guessLabel;
@@ -144,15 +147,15 @@ function getBazelPackageOfComponentName(name) {
144147
}
145148

146149
/** Converts a path to a Bazel label. */
147-
function convertPathToBazelLabel(name) {
148-
if (shelljs.test('-d', name)) {
149-
return `//${convertPathToPosix(path.relative(projectDir, name))}`;
150+
function convertPathToBazelLabel(name: string) {
151+
if (sh.test('-d', name)) {
152+
return `//${convertPathToPosix(relative(process.cwd(), name))}`;
150153
}
151154
return null;
152155
}
153156

154157
/** Gets the name of the target that should be run. */
155-
function getTargetName(packageName) {
158+
function getTargetName(packageName: string) {
156159
// Schematics don't have _debug and browser targets.
157160
if (packageName && packageName.endsWith('schematics')) {
158161
return 'unit_tests';
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const path = require('path');
2-
const shelljs = require('shelljs');
1+
import {join} from 'path';
2+
import sh from 'shelljs';
33

44
/** Map of common typos in target names. The key is the typo, the value is the correct form. */
55
const commonTypos = new Map([['snackbar', 'snack-bar']]);
@@ -13,12 +13,12 @@ const orderedGuessPackages = ['material', 'cdk', 'material-experimental', 'cdk-e
1313
* Tries to guess the full name of a package, based on a shorthand name.
1414
* Returns an object with the result of the guess and the names that were attempted.
1515
*/
16-
function guessPackageName(name, packagesDir) {
16+
export function guessPackageName(name: string, packagesDir: string) {
1717
name = correctTypos(name);
1818

1919
// Build up a list of packages that we're going to try.
20-
const attempts = [name, ...orderedGuessPackages.map(package => path.join(package, name))];
21-
const result = attempts.find(guessName => shelljs.test('-d', path.join(packagesDir, guessName)));
20+
const attempts = [name, ...orderedGuessPackages.map(guessPackage => join(guessPackage, name))];
21+
const result = attempts.find(guessName => sh.test('-d', join(packagesDir, guessName)));
2222

2323
return {
2424
result: result ? convertPathToPosix(result) : null,
@@ -27,21 +27,16 @@ function guessPackageName(name, packagesDir) {
2727
}
2828

2929
/** Converts an arbitrary path to a Posix path. */
30-
function convertPathToPosix(pathName) {
30+
export function convertPathToPosix(pathName: string) {
3131
return pathName.replace(/\\/g, '/');
3232
}
3333

3434
/** Correct common typos in a target name */
35-
function correctTypos(target) {
35+
function correctTypos(target: string) {
3636
let correctedTarget = target;
3737
for (const [typo, correction] of commonTypos) {
3838
correctedTarget = correctedTarget.replace(typo, correction);
3939
}
4040

4141
return correctedTarget;
4242
}
43-
44-
module.exports = {
45-
guessPackageName,
46-
convertPathToPosix,
47-
};

0 commit comments

Comments
 (0)