|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be
|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 |
| -import {resolve} from 'path'; |
| 8 | +import {existsSync, lstatSync, readFileSync, readdirSync} from 'fs'; |
| 9 | +import {posix as path} from 'path'; |
| 10 | + |
9 | 11 | import {PackageTransformer} from './transform/package_transformer';
|
10 | 12 |
|
11 | 13 | export function mainNgcc(args: string[]): number {
|
12 |
| - const packagePaths = args[0] ? [resolve(args[0])] : []; |
13 |
| - const formats = args[1] ? [args[1]] : ['fesm2015', 'esm2015', 'fesm5', 'esm5']; |
14 |
| - |
15 |
| - // TODO: find all the package types to transform |
16 |
| - // TODO: error/warning logging/handling etc |
| 14 | + const formats = args[0] ? args[0].split(',') : ['fesm2015', 'esm2015', 'fesm5', 'esm5']; |
| 15 | + const packagePaths = args[1] ? [path.resolve(args[1])] : findPackagesToCompile(); |
| 16 | + const targetPath = args[2] ? args[2] : 'node_modules'; |
17 | 17 |
|
18 | 18 | const transformer = new PackageTransformer();
|
19 | 19 | packagePaths.forEach(packagePath => {
|
20 | 20 | formats.forEach(format => {
|
21 |
| - console.warn(`Compiling ${packagePath}:${format}`); |
22 |
| - transformer.transform(packagePath, format); |
| 21 | + // TODO: remove before flight |
| 22 | + console.warn(`Compiling ${packagePath} : ${format}`); |
| 23 | + transformer.transform(packagePath, format, targetPath); |
23 | 24 | });
|
24 | 25 | });
|
25 | 26 |
|
26 | 27 | return 0;
|
27 | 28 | }
|
| 29 | + |
| 30 | +// TODO - consider nested node_modules |
| 31 | + |
| 32 | +/** |
| 33 | + * Check whether the given folder needs to be included in the ngcc compilation. |
| 34 | + * We do not care about folders that are: |
| 35 | + * |
| 36 | + * - symlinks |
| 37 | + * - node_modules |
| 38 | + * - do not contain a package.json |
| 39 | + * - do not have a typings property in package.json |
| 40 | + * - do not have an appropriate metadata.json file |
| 41 | + * |
| 42 | + * @param folderPath The absolute path to the folder. |
| 43 | + */ |
| 44 | +function hasMetadataFile(folderPath: string): boolean { |
| 45 | + const folderName = path.basename(folderPath); |
| 46 | + if (folderName === 'node_modules' || lstatSync(folderPath).isSymbolicLink()) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + const packageJsonPath = path.join(folderPath, 'package.json'); |
| 50 | + if (!existsSync(packageJsonPath)) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')); |
| 54 | + if (!packageJson.typings) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + // TODO: avoid if packageJson contains built marker |
| 58 | + const metadataPath = |
| 59 | + path.join(folderPath, packageJson.typings.replace(/\.d\.ts$/, '.metadata.json')); |
| 60 | + return existsSync(metadataPath); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Look for packages that need to be compiled. |
| 65 | + * The function will recurse into folders that start with `@...`, e.g. `@angular/...`. |
| 66 | + * Without an argument it starts at `node_modules`. |
| 67 | + */ |
| 68 | +function findPackagesToCompile(folder: string = 'node_modules'): string[] { |
| 69 | + const fullPath = path.resolve(folder); |
| 70 | + const packagesToCompile: string[] = []; |
| 71 | + readdirSync(fullPath) |
| 72 | + .filter(p => !p.startsWith('.')) |
| 73 | + .filter(p => lstatSync(path.join(fullPath, p)).isDirectory()) |
| 74 | + .forEach(p => { |
| 75 | + const packagePath = path.join(fullPath, p); |
| 76 | + if (p.startsWith('@')) { |
| 77 | + packagesToCompile.push(...findPackagesToCompile(packagePath)); |
| 78 | + } else { |
| 79 | + packagesToCompile.push(packagePath); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + return packagesToCompile.filter(path => recursiveDirTest(path, hasMetadataFile)); |
| 84 | +} |
| 85 | + |
| 86 | +function recursiveDirTest(dir: string, test: (dir: string) => boolean): boolean { |
| 87 | + return test(dir) || readdirSync(dir).some(segment => { |
| 88 | + const fullPath = path.join(dir, segment); |
| 89 | + return lstatSync(fullPath).isDirectory() && recursiveDirTest(fullPath, test); |
| 90 | + }); |
| 91 | +} |
0 commit comments