|
| 1 | +import * as fs from 'fs'; |
| 2 | +import {dirname, join, relative, resolve} from 'path'; |
| 3 | +import * as ts from 'typescript'; |
| 4 | + |
| 5 | + |
| 6 | +function _createSource(path: string): ts.SourceFile { |
| 7 | + return ts.createSourceFile(path, fs.readFileSync(path, 'utf-8'), ts.ScriptTarget.Latest); |
| 8 | +} |
| 9 | + |
| 10 | +function _findNodes(sourceFile: ts.SourceFile, node: ts.Node, kind: ts.SyntaxKind, |
| 11 | + keepGoing = false): ts.Node[] { |
| 12 | + if (node.kind == kind && !keepGoing) { |
| 13 | + return [node]; |
| 14 | + } |
| 15 | + |
| 16 | + return node.getChildren(sourceFile).reduce((result, n) => { |
| 17 | + return result.concat(_findNodes(sourceFile, n, kind, keepGoing)); |
| 18 | + }, node.kind == kind ? [node] : []); |
| 19 | +} |
| 20 | + |
| 21 | +function _recursiveSymbolExportLookup(sourcePath: string, |
| 22 | + sourceFile: ts.SourceFile, |
| 23 | + symbolName: string): string | null { |
| 24 | + // Check this file. |
| 25 | + const hasSymbol = _findNodes(sourceFile, sourceFile, ts.SyntaxKind.ClassDeclaration) |
| 26 | + .some((cd: ts.ClassDeclaration) => { |
| 27 | + return cd.name && cd.name.text == symbolName; |
| 28 | + }); |
| 29 | + if (hasSymbol) { |
| 30 | + return sourcePath; |
| 31 | + } |
| 32 | + |
| 33 | + // We found the bootstrap variable, now we just need to get where it's imported. |
| 34 | + const exports = _findNodes(sourceFile, sourceFile, ts.SyntaxKind.ExportDeclaration, false) |
| 35 | + .map(node => node as ts.ExportDeclaration); |
| 36 | + |
| 37 | + for (const decl of exports) { |
| 38 | + if (!decl.moduleSpecifier || decl.moduleSpecifier.kind !== ts.SyntaxKind.StringLiteral) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + const module = resolve(dirname(sourcePath), (decl.moduleSpecifier as ts.StringLiteral).text); |
| 43 | + if (!decl.exportClause) { |
| 44 | + const moduleTs = module + '.ts'; |
| 45 | + if (fs.existsSync(moduleTs)) { |
| 46 | + const moduleSource = _createSource(moduleTs); |
| 47 | + const maybeModule = _recursiveSymbolExportLookup(module, moduleSource, symbolName); |
| 48 | + if (maybeModule) { |
| 49 | + return maybeModule; |
| 50 | + } |
| 51 | + } |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + if (decl.exportClause.namedBindings.kind == ts.SyntaxKind.NamespaceImport) { |
| 56 | + const binding = decl.exportClause.namedBindings as ts.NamespaceImport; |
| 57 | + if (binding.name.text == symbolName) { |
| 58 | + // This is a default export. |
| 59 | + return module; |
| 60 | + } |
| 61 | + } else if (decl.exportClause.namedBindings.kind == ts.SyntaxKind.NamedImports) { |
| 62 | + const binding = decl.exportClause.namedBindings as ts.NamedImports; |
| 63 | + for (const specifier of binding.elements) { |
| 64 | + if (specifier.name.text == symbolName) { |
| 65 | + // If it's a directory, load its index and recursively lookup. |
| 66 | + if (fs.statSync(module).isDirectory()) { |
| 67 | + const indexModule = join(module, 'index.ts'); |
| 68 | + if (fs.existsSync(indexModule)) { |
| 69 | + const maybeModule = _recursiveSymbolExportLookup( |
| 70 | + indexModule, _createSource(indexModule), symbolName); |
| 71 | + if (maybeModule) { |
| 72 | + return maybeModule; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Create the source and verify that the symbol is at least a class. |
| 78 | + const source = _createSource(module); |
| 79 | + const hasSymbol = _findNodes(source, source, ts.SyntaxKind.ClassDeclaration) |
| 80 | + .some((cd: ts.ClassDeclaration) => { |
| 81 | + return cd.name && cd.name.text == symbolName; |
| 82 | + }); |
| 83 | + |
| 84 | + if (hasSymbol) { |
| 85 | + return module; |
| 86 | + } else { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return null; |
| 95 | +} |
| 96 | + |
| 97 | +function _symbolImportLookup(sourcePath: string, |
| 98 | + sourceFile: ts.SourceFile, |
| 99 | + symbolName: string): string | null { |
| 100 | + // We found the bootstrap variable, now we just need to get where it's imported. |
| 101 | + const imports = _findNodes(sourceFile, sourceFile, ts.SyntaxKind.ImportDeclaration, false) |
| 102 | + .map(node => node as ts.ImportDeclaration); |
| 103 | + |
| 104 | + for (const decl of imports) { |
| 105 | + if (!decl.importClause || !decl.moduleSpecifier) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + if (decl.moduleSpecifier.kind !== ts.SyntaxKind.StringLiteral) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + const module = resolve(dirname(sourcePath), (decl.moduleSpecifier as ts.StringLiteral).text); |
| 113 | + |
| 114 | + if (decl.importClause.namedBindings.kind == ts.SyntaxKind.NamespaceImport) { |
| 115 | + const binding = decl.importClause.namedBindings as ts.NamespaceImport; |
| 116 | + if (binding.name.text == symbolName) { |
| 117 | + // This is a default export. |
| 118 | + return module; |
| 119 | + } |
| 120 | + } else if (decl.importClause.namedBindings.kind == ts.SyntaxKind.NamedImports) { |
| 121 | + const binding = decl.importClause.namedBindings as ts.NamedImports; |
| 122 | + for (const specifier of binding.elements) { |
| 123 | + if (specifier.name.text == symbolName) { |
| 124 | + // If it's a directory, load its index and recursively lookup. |
| 125 | + if (fs.statSync(module).isDirectory()) { |
| 126 | + const indexModule = join(module, 'index.ts'); |
| 127 | + if (fs.existsSync(indexModule)) { |
| 128 | + const maybeModule = _recursiveSymbolExportLookup( |
| 129 | + indexModule, _createSource(indexModule), symbolName); |
| 130 | + if (maybeModule) { |
| 131 | + return maybeModule; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Create the source and verify that the symbol is at least a class. |
| 137 | + const source = _createSource(module); |
| 138 | + const hasSymbol = _findNodes(source, source, ts.SyntaxKind.ClassDeclaration) |
| 139 | + .some((cd: ts.ClassDeclaration) => { |
| 140 | + return cd.name && cd.name.text == symbolName; |
| 141 | + }); |
| 142 | + |
| 143 | + if (hasSymbol) { |
| 144 | + return module; |
| 145 | + } else { |
| 146 | + return null; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + return null; |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +export function resolveEntryModuleFromMain(mainPath: string) { |
| 157 | + const source = _createSource(mainPath); |
| 158 | + |
| 159 | + const bootstrap = _findNodes(source, source, ts.SyntaxKind.CallExpression, false) |
| 160 | + .map(node => node as ts.CallExpression) |
| 161 | + .filter(call => { |
| 162 | + const access = call.expression as ts.PropertyAccessExpression; |
| 163 | + return access.kind == ts.SyntaxKind.PropertyAccessExpression |
| 164 | + && access.name.kind == ts.SyntaxKind.Identifier |
| 165 | + && (access.name.text == 'bootstrapModule' |
| 166 | + || access.name.text == 'bootstrapModuleFactory'); |
| 167 | + }); |
| 168 | + |
| 169 | + if (bootstrap.length != 1 |
| 170 | + || bootstrap[0].arguments[0].kind !== ts.SyntaxKind.Identifier) { |
| 171 | + throw new Error('Tried to find bootstrap code, but could not. Specify either ' |
| 172 | + + 'statically analyzable bootstrap code or pass in an entryModule ' |
| 173 | + + 'to the plugins options.'); |
| 174 | + } |
| 175 | + |
| 176 | + const bootstrapSymbolName = (bootstrap[0].arguments[0] as ts.Identifier).text; |
| 177 | + const module = _symbolImportLookup(mainPath, source, bootstrapSymbolName); |
| 178 | + if (module) { |
| 179 | + return `${resolve(dirname(mainPath), module)}#${bootstrapSymbolName}`; |
| 180 | + } |
| 181 | + |
| 182 | + // shrug... something bad happened and we couldn't find the import statement. |
| 183 | + throw new Error('Tried to find bootstrap code, but could not. Specify either ' |
| 184 | + + 'statically analyzable bootstrap code or pass in an entryModule ' |
| 185 | + + 'to the plugins options.'); |
| 186 | +} |
0 commit comments