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