Skip to content

Commit 54218cd

Browse files
authored
build: add support for shorthand names in API golden approval script (#20394)
Currently we have to write the full path for the API golden approval script, whereas the `test` script supports shorthands. These changes move the logic that guesses the name of a package out into a separate file so that it can be reused in the `approve-api` script.
1 parent 23b4c88 commit 54218cd

File tree

3 files changed

+71
-37
lines changed

3 files changed

+71
-37
lines changed

scripts/approve-api-golden.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ const shelljs = require('shelljs');
44
const chalk = require('chalk');
55
const path = require('path');
66
const packageName = process.argv[2];
7+
const {guessPackageName} = require('./util');
78

89
if (!packageName) {
910
console.error(chalk.red('No package name has been passed in for API golden approval.'));
1011
process.exit(1);
1112
}
1213

14+
const projectDir = path.join(__dirname, '../');
15+
const packageNameGuess = guessPackageName(packageName, path.join(projectDir, 'src'));
16+
17+
if (!packageNameGuess.result) {
18+
console.error(chalk.red(`Could not find package for API golden approval called ` +
19+
`${chalk.yellow(packageName)}. Looked in packages: \n${packageNameGuess.attempts.join('\n')}`));
20+
process.exit(1);
21+
}
22+
1323
// ShellJS should exit if any command fails.
1424
shelljs.set('-e');
15-
shelljs.cd(path.join(__dirname, '../'));
16-
shelljs.exec(`yarn bazel run //tools/public_api_guard:${packageName}.d.ts_api.accept`);
25+
shelljs.cd(projectDir);
26+
shelljs.exec(`yarn bazel run //tools/public_api_guard:${packageNameGuess.result}.d.ts_api.accept`);

scripts/run-component-tests.js

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,14 @@ const shelljs = require('shelljs');
2222
const chalk = require('chalk');
2323
const path = require('path');
2424
const args = process.argv.slice(2);
25+
const {guessPackageName, convertPathToPosix} = require('./util');
2526

2627
// Path to the project directory.
2728
const projectDir = path.join(__dirname, '../');
2829

2930
// Path to the directory that contains all packages.
3031
const packagesDir = path.join(projectDir, 'src/');
3132

32-
// List of packages where the specified component could be defined in. The script uses the
33-
// first package that contains the component (if no package is specified explicitly).
34-
// e.g. "button" will become "material/button", and "overlay" becomes "cdk/overlay".
35-
const orderedGuessPackages = ['material', 'cdk', 'material-experimental', 'cdk-experimental'];
36-
37-
/** Map of common typos in target names. The key is the typo, the value is the correct form. */
38-
const commonTypos = new Map([
39-
['snackbar', 'snack-bar'],
40-
]);
41-
4233
// ShellJS should exit if any command fails.
4334
shelljs.set('-e');
4435
shelljs.cd(projectDir);
@@ -95,7 +86,6 @@ if (!components.length) {
9586

9687
const bazelAction = local ? 'run' : 'test';
9788
const testLabels = components
98-
.map(t => correctTypos(t))
9989
.map(t => `${getBazelPackageOfComponentName(t)}:${getTargetName(t)}`);
10090

10191
// Runs Bazel for the determined test labels.
@@ -113,17 +103,17 @@ function getBazelPackageOfComponentName(name) {
113103
if (targetName !== null) {
114104
return targetName;
115105
}
116-
// If the name does not contain an explicit package name, we try guessing the
117-
// package name by walking through an ordered list of possible packages and checking
118-
// if a package contains a component with the given name. The first match will be used.
119-
for (let guessPackage of orderedGuessPackages) {
120-
const guessTargetName = convertPathToBazelLabel(path.join(packagesDir, guessPackage, name));
121-
if (guessTargetName !== null) {
122-
return guessTargetName;
123-
}
106+
// If the name does not contain an explicit package name, try to guess it.
107+
const guess = guessPackageName(name, packagesDir);
108+
const guessLabel =
109+
guess.result ? convertPathToBazelLabel(path.join(packagesDir, guess.result)) : null;
110+
111+
if (guessLabel) {
112+
return guessLabel;
124113
}
114+
125115
console.error(chalk.red(`Could not find test target for specified component: ` +
126-
`${chalk.yellow(name)}. Looked in packages: ${orderedGuessPackages.join(', ')}`));
116+
`${chalk.yellow(name)}. Looked in packages: \n${guess.attempts.join('\n')}`));
127117
process.exit(1);
128118
}
129119

@@ -135,21 +125,6 @@ function convertPathToBazelLabel(name) {
135125
return null;
136126
}
137127

138-
/** Correct common typos in a target name */
139-
function correctTypos(target) {
140-
let correctedTarget = target;
141-
for (const [typo, correction] of commonTypos) {
142-
correctedTarget = correctedTarget.replace(typo, correction);
143-
}
144-
145-
return correctedTarget;
146-
}
147-
148-
/** Converts an arbitrary path to a Posix path. */
149-
function convertPathToPosix(pathName) {
150-
return pathName.replace(/\\/g, '/');
151-
}
152-
153128
/** Gets the name of the target that should be run. */
154129
function getTargetName(packageName) {
155130
// Schematics don't have _local and browser targets.

scripts/util.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const path = require('path');
2+
const shelljs = require('shelljs');
3+
4+
/** Map of common typos in target names. The key is the typo, the value is the correct form. */
5+
const commonTypos = new Map([
6+
['snackbar', 'snack-bar'],
7+
]);
8+
9+
// List of packages where the specified component could be defined in. The script uses the
10+
// first package that contains the component (if no package is specified explicitly).
11+
// e.g. "button" will become "material/button", and "overlay" becomes "cdk/overlay".
12+
const orderedGuessPackages = ['material', 'cdk', 'material-experimental', 'cdk-experimental'];
13+
14+
/**
15+
* Tries to guess the full name of a package, based on a shorthand name.
16+
* Returns an object with the result of the guess and the names that were attempted.
17+
*/
18+
function guessPackageName(name, packagesDir) {
19+
name = correctTypos(name);
20+
21+
// Build up a list of packages that we're going to try.
22+
const attempts = [name, ...orderedGuessPackages.map(package => path.join(package, name))];
23+
const result = attempts.find(guessName => shelljs.test('-d', path.join(packagesDir, guessName)));
24+
25+
return {
26+
result: result ? convertPathToPosix(result) : null,
27+
attempts
28+
};
29+
}
30+
31+
/** Converts an arbitrary path to a Posix path. */
32+
function convertPathToPosix(pathName) {
33+
return pathName.replace(/\\/g, '/');
34+
}
35+
36+
/** Correct common typos in a target name */
37+
function correctTypos(target) {
38+
let correctedTarget = target;
39+
for (const [typo, correction] of commonTypos) {
40+
correctedTarget = correctedTarget.replace(typo, correction);
41+
}
42+
43+
return correctedTarget;
44+
}
45+
46+
module.exports = {
47+
guessPackageName,
48+
convertPathToPosix
49+
};

0 commit comments

Comments
 (0)