|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {readFileSync, writeFileSync} from 'fs'; |
| 10 | +import path from 'path'; |
| 11 | +// @ts-ignore This compiles fine, but Webstorm doesn't like the ESM import in a CJS context. |
| 12 | +import { |
| 13 | + ClassEntry, |
| 14 | + CompilerOptions, |
| 15 | + createCompilerHost, |
| 16 | + DocEntry, |
| 17 | + EntryCollection, |
| 18 | + InterfaceEntry, |
| 19 | + NgtscProgram, |
| 20 | +} from '@angular/compiler-cli'; |
| 21 | +import ts from 'typescript'; |
| 22 | +import {EXAMPLES_PATH, interpolateCodeExamples} from './interpolate_code_examples'; |
| 23 | + |
| 24 | +function main() { |
| 25 | + const [paramFilePath] = process.argv.slice(2); |
| 26 | + const rawParamLines = readFileSync(paramFilePath, {encoding: 'utf8'}).split('\n'); |
| 27 | + |
| 28 | + const [ |
| 29 | + moduleName, |
| 30 | + moduleLabel, |
| 31 | + serializedPrivateModules, |
| 32 | + entryPointExecRootRelativePath, |
| 33 | + srcs, |
| 34 | + outputFilenameExecRootRelativePath, |
| 35 | + serializedPathMapWithExecRootRelativePaths, |
| 36 | + extraEntriesSrcs, |
| 37 | + ] = rawParamLines; |
| 38 | + |
| 39 | + const privateModules = new Set(serializedPrivateModules.split(',')); |
| 40 | + |
| 41 | + // The path map is a serialized JSON map of import path to index.ts file. |
| 42 | + // For example, {'@angular/core': 'path/to/some/index.ts'} |
| 43 | + const pathMap = JSON.parse(serializedPathMapWithExecRootRelativePaths) as Record<string, string>; |
| 44 | + |
| 45 | + // The tsconfig expects the path map in the form of path -> array of actual locations. |
| 46 | + // We also resolve the exec root relative paths to absolute paths to disambiguate. |
| 47 | + const resolvedPathMap: {[key: string]: string[]} = {}; |
| 48 | + for (const [importPath, filePath] of Object.entries(pathMap)) { |
| 49 | + resolvedPathMap[importPath] = [path.resolve(filePath)]; |
| 50 | + |
| 51 | + // In addition to the exact import path, |
| 52 | + // also add wildcard mappings for subdirectories. |
| 53 | + const importPathWithWildcard = path.join(importPath, '*'); |
| 54 | + resolvedPathMap[importPathWithWildcard] = [ |
| 55 | + path.join(path.resolve(path.dirname(filePath)), '*'), |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + const compilerOptions: CompilerOptions = { |
| 60 | + paths: resolvedPathMap, |
| 61 | + rootDir: '.', |
| 62 | + skipLibCheck: true, |
| 63 | + target: ts.ScriptTarget.ES2022, |
| 64 | + // This is necessary because otherwise types that include `| null` are not included in the documentation. |
| 65 | + strictNullChecks: true, |
| 66 | + moduleResolution: ts.ModuleResolutionKind.NodeNext, |
| 67 | + experimentalDecorators: true, |
| 68 | + }; |
| 69 | + |
| 70 | + // Code examples should not be fed to the compiler. |
| 71 | + const filesWithoutExamples = srcs.split(',').filter(src => !src.startsWith(EXAMPLES_PATH)); |
| 72 | + const compilerHost = createCompilerHost({options: compilerOptions}); |
| 73 | + const program: NgtscProgram = new NgtscProgram( |
| 74 | + filesWithoutExamples, |
| 75 | + compilerOptions, |
| 76 | + compilerHost, |
| 77 | + ); |
| 78 | + |
| 79 | + const extraEntries: DocEntry[] = (extraEntriesSrcs ?? '') |
| 80 | + .split(',') |
| 81 | + .filter(path => !!path) |
| 82 | + .reduce((result: DocEntry[], path) => { |
| 83 | + return result.concat(JSON.parse(readFileSync(path, {encoding: 'utf8'})) as DocEntry[]); |
| 84 | + }, []); |
| 85 | + |
| 86 | + const apiDoc = program.getApiDocumentation(entryPointExecRootRelativePath, privateModules); |
| 87 | + const extractedEntries = apiDoc.entries; |
| 88 | + const combinedEntries = extractedEntries.concat(extraEntries); |
| 89 | + |
| 90 | + interpolateCodeExamples(combinedEntries); |
| 91 | + |
| 92 | + const normalized = moduleName.replace('@', '').replace(/[\/]/g, '_'); |
| 93 | + |
| 94 | + const output = JSON.stringify({ |
| 95 | + moduleLabel: moduleLabel || moduleName, |
| 96 | + moduleName: moduleName, |
| 97 | + normalizedModuleName: normalized, |
| 98 | + entries: combinedEntries, |
| 99 | + symbols: [ |
| 100 | + // Symbols referenced, originating from other packages |
| 101 | + ...apiDoc.symbols.entries(), |
| 102 | + |
| 103 | + // Exported symbols from the current package |
| 104 | + ...apiDoc.entries.map(entry => [entry.name, moduleName]), |
| 105 | + |
| 106 | + // Also doing it for every member of classes/interfaces |
| 107 | + ...apiDoc.entries.flatMap(entry => [ |
| 108 | + [entry.name, moduleName], |
| 109 | + ...getEntriesFromMembers(entry).map(member => [member, moduleName]), |
| 110 | + ]), |
| 111 | + ], |
| 112 | + } as EntryCollection); |
| 113 | + |
| 114 | + writeFileSync(outputFilenameExecRootRelativePath, output, {encoding: 'utf8'}); |
| 115 | + console.error('!!!!', output); |
| 116 | +} |
| 117 | + |
| 118 | +function getEntriesFromMembers(entry: DocEntry): string[] { |
| 119 | + if (!hasMembers(entry)) { |
| 120 | + return []; |
| 121 | + } |
| 122 | + |
| 123 | + return entry.members.map(member => `${entry.name}.${member.name}`); |
| 124 | +} |
| 125 | + |
| 126 | +function hasMembers(entry: DocEntry): entry is InterfaceEntry | ClassEntry { |
| 127 | + return 'members' in entry; |
| 128 | +} |
| 129 | + |
| 130 | +main(); |
0 commit comments