|
| 1 | +import {sync as glob} from 'glob'; |
| 2 | +import {readFileSync, writeFileSync} from 'fs'; |
| 3 | +import {join, basename} from 'path'; |
| 4 | +import * as ts from 'typescript'; |
| 5 | + |
| 6 | +// Script that generates mappings from our publicly-exported symbols to their entry points. The |
| 7 | +// mappings are intended to be used by the secondary entry points schematic and should be committed |
| 8 | +// next to the relevant schematic file. |
| 9 | +// Can be run using `ts-node --project scripts scripts/generate-schematic-imports-map.ts`. |
| 10 | +const mappings: {[symbolName: string]: string} = {}; |
| 11 | +const outputPath = join(__dirname, '../temp-entry-points-mapping.json'); |
| 12 | + |
| 13 | +glob('**/*.d.ts', { |
| 14 | + absolute: true, |
| 15 | + cwd: join(__dirname, '../tools/public_api_guard/material') |
| 16 | +}).forEach(fileName => { |
| 17 | + const content = readFileSync(fileName, 'utf8'); |
| 18 | + const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.ES5); |
| 19 | + const moduleName = basename(fileName, '.d.ts'); |
| 20 | + |
| 21 | + // We only care about the top-level symbols. |
| 22 | + sourceFile.forEachChild((node: ts.Node & {name?: ts.Identifier}) => { |
| 23 | + // Most of the exports are named nodes (e.g. classes, types, interfaces) so we can use the |
| 24 | + // `name` property to extract the name. The one exception are variable declarations for |
| 25 | + // which we need to loop through the list of declarations. |
| 26 | + if (node.name) { |
| 27 | + addMapping(moduleName, node.name.text); |
| 28 | + } else if (ts.isVariableStatement(node)) { |
| 29 | + node.declarationList.declarations.forEach(declaration => { |
| 30 | + if (ts.isIdentifier(declaration.name)) { |
| 31 | + addMapping(moduleName, declaration.name.text); |
| 32 | + } else { |
| 33 | + throw Error('Unsupported variable kind.'); |
| 34 | + } |
| 35 | + }); |
| 36 | + } else if (node.kind !== ts.SyntaxKind.EndOfFileToken) { |
| 37 | + throw Error(`Unhandled node kind ${node.kind} in ${fileName}.`); |
| 38 | + } |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +/** Adds a symbol to the mappings. */ |
| 43 | +function addMapping(moduleName: string, symbolName: string) { |
| 44 | + if (mappings[symbolName] && mappings[symbolName] !== moduleName) { |
| 45 | + throw Error(`Duplicate symbol name ${symbolName}.`); |
| 46 | + } |
| 47 | + |
| 48 | + mappings[symbolName] = moduleName; |
| 49 | +} |
| 50 | + |
| 51 | +writeFileSync(outputPath, JSON.stringify(mappings, null, 2)); |
| 52 | +console.log(`Generated mappings to ${outputPath}. You should move the file to the ` + |
| 53 | + `proper place yourself.`); |
0 commit comments