|
| 1 | +import {join} from 'path'; |
| 2 | +import {readdirSync, existsSync} from 'fs'; |
| 3 | +import * as ts from 'typescript'; |
| 4 | +import chalk from 'chalk'; |
| 5 | + |
| 6 | +const excludedPackages = new Set(['sidenav']); |
| 7 | +let hasFailed = false; |
| 8 | + |
| 9 | +readdirSync(join(__dirname, '../src/material'), {withFileTypes: true}) |
| 10 | + .filter(entity => entity.isDirectory()) |
| 11 | + .map(entity => entity.name) |
| 12 | + .filter(name => !excludedPackages.has(name)) |
| 13 | + .filter(name => existsSync(join(__dirname, '../src/material-experimental', 'mdc-' + name))) |
| 14 | + .forEach(name => { |
| 15 | + const missingSymbols = getMissingSymbols(name); |
| 16 | + |
| 17 | + if (missingSymbols.length > 0) { |
| 18 | + console.log(chalk.redBright(`\nMissing symbols from mdc-${name}:`)); |
| 19 | + console.log(missingSymbols.join('\n')); |
| 20 | + hasFailed = true; |
| 21 | + } |
| 22 | + }); |
| 23 | + |
| 24 | +if (hasFailed) { |
| 25 | + console.log(chalk.redBright( |
| 26 | + '\nDetected one or more MDC packages do not export the same set of symbols from\n' + |
| 27 | + 'public-api.ts as their non-MDC counterpart.\nEither implement the missing symbols or ' + |
| 28 | + 're-export them from the Material package.' |
| 29 | + )); |
| 30 | + process.exit(1); |
| 31 | +} else { |
| 32 | + console.log(chalk.green( |
| 33 | + 'All MDC packages export the same public API symbols as their non-MDC counterparts.')); |
| 34 | + process.exit(0); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets the names of symbols that are present in a Material package, |
| 39 | + * but not its MDC counterpart. |
| 40 | + */ |
| 41 | +function getMissingSymbols(name: string): string[] { |
| 42 | + const mdcExports = getExports(`material-experimental/mdc-${name}`); |
| 43 | + const materialExports = getExports(`material/${name}`); |
| 44 | + |
| 45 | + if (!mdcExports.length) { |
| 46 | + throw Error(`Could not resolve exports in mdc-${name}`); |
| 47 | + } |
| 48 | + |
| 49 | + if (!materialExports.length) { |
| 50 | + throw Error(`Could not resolve exports in ${name}`); |
| 51 | + } |
| 52 | + |
| 53 | + return materialExports.filter(exportName => !mdcExports.includes(exportName)); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Gets the name of the exported symbols from a particular package. |
| 58 | + * Based on https://github.com/angular/angular/blob/master/tools/ts-api-guardian/lib/serializer.ts |
| 59 | + */ |
| 60 | +function getExports(name: string): string[] { |
| 61 | + const entryPoint = join(__dirname, '../src', name, 'public-api.ts'); |
| 62 | + const program = ts.createProgram([entryPoint], { |
| 63 | + // This is a bit faster than the default and seems to produce identical results. |
| 64 | + moduleResolution: ts.ModuleResolutionKind.Classic |
| 65 | + }); |
| 66 | + const sourceFile = program.getSourceFiles().find(f => f.fileName.endsWith('public-api.ts'))!; |
| 67 | + const typeChecker = program.getTypeChecker(); |
| 68 | + const mainSymbol = typeChecker.getSymbolAtLocation(sourceFile); |
| 69 | + |
| 70 | + return (mainSymbol ? (typeChecker.getExportsOfModule(mainSymbol) || []) : []).map(symbol => { |
| 71 | + // tslint:disable-next-line:no-bitwise |
| 72 | + if (symbol.flags & ts.SymbolFlags.Alias) { |
| 73 | + const resolvedSymbol = typeChecker.getAliasedSymbol(symbol); |
| 74 | + return (!resolvedSymbol.valueDeclaration && !resolvedSymbol.declarations) ? |
| 75 | + symbol : resolvedSymbol; |
| 76 | + } else { |
| 77 | + return symbol; |
| 78 | + } |
| 79 | + }).map(symbol => symbol.name); |
| 80 | +} |
0 commit comments