diff options
Diffstat (limited to 'tools/ts_library_builder')
-rw-r--r-- | tools/ts_library_builder/README.md | 6 | ||||
-rw-r--r-- | tools/ts_library_builder/ast_util.ts | 22 | ||||
-rw-r--r-- | tools/ts_library_builder/build_library.ts | 74 | ||||
-rw-r--r-- | tools/ts_library_builder/main.ts | 5 | ||||
-rw-r--r-- | tools/ts_library_builder/test.ts | 4 |
5 files changed, 72 insertions, 39 deletions
diff --git a/tools/ts_library_builder/README.md b/tools/ts_library_builder/README.md index e2701f0fb..187d753e6 100644 --- a/tools/ts_library_builder/README.md +++ b/tools/ts_library_builder/README.md @@ -18,9 +18,9 @@ The tool depends upon a couple libraries: - [`ts-node`](https://www.npmjs.com/package/ts-node) to provide just in time transpiling of TypeScript for the tool itself. -- [`ts-simple-ast`](https://www.npmjs.com/package/ts-simple-ast) which provides - a more rational and functional interface to the TypeScript AST to make - manipulations easier. +- [`ts-morph`](https://www.npmjs.com/package/ts-morph) which provides a more + rational and functional interface to the TypeScript AST to make manipulations + easier. - [`prettier`](https://www.npmjs.com/package/prettier) and [`@types/prettier`](https://www.npmjs.com/package/@types/prettier) to format the output. diff --git a/tools/ts_library_builder/ast_util.ts b/tools/ts_library_builder/ast_util.ts index 58aa7fb9c..f18daa398 100644 --- a/tools/ts_library_builder/ast_util.ts +++ b/tools/ts_library_builder/ast_util.ts @@ -15,7 +15,21 @@ import { TypeGuards, VariableStatement, VariableDeclarationKind -} from "ts-simple-ast"; +} from "ts-morph"; + +let silent = false; + +/** Logs a message to the console. */ +export function log(message: any = "", ...args: any[]) { + if (!silent) { + console.log(message, ...args); + } +} + +/** Sets the silent flag which impacts logging to the console. */ +export function setSilent(value = false): void { + silent = value; +} /** Add a property to an interface */ export function addInterfaceProperty( @@ -169,8 +183,12 @@ export function flattenNamespace({ } sourceFiles.add(currentSourceFile); - const currentSourceFilePath = currentSourceFile.getFilePath(); + const currentSourceFilePath = currentSourceFile + .getFilePath() + .replace(/(\.d)?\.ts$/, ""); + log("Process source file:", currentSourceFilePath); if (customSources && currentSourceFilePath in customSources) { + log(" Using custom source."); output += customSources[currentSourceFilePath]; return; } diff --git a/tools/ts_library_builder/build_library.ts b/tools/ts_library_builder/build_library.ts index 98a51f63f..abfb430d1 100644 --- a/tools/ts_library_builder/build_library.ts +++ b/tools/ts_library_builder/build_library.ts @@ -1,4 +1,5 @@ import { writeFileSync } from "fs"; +import { join } from "path"; import * as prettier from "prettier"; import { ExpressionStatement, @@ -8,10 +9,11 @@ import { ts, Type, TypeGuards -} from "ts-simple-ast"; +} from "ts-morph"; import { addInterfaceProperty, addSourceComment, + addTypeAlias, addVariableDeclaration, checkDiagnostics, flattenNamespace, @@ -19,10 +21,11 @@ import { inlineFiles, loadDtsFiles, loadFiles, + log, logDiagnostics, namespaceSourceFile, normalizeSlashes, - addTypeAlias + setSilent } from "./ast_util"; export interface BuildLibraryOptions { @@ -47,6 +50,10 @@ export interface BuildLibraryOptions { */ inline?: string[]; + /** An array of input files to be provided to the input project, relative to + * the basePath. */ + inputs?: string[]; + /** * The path to the output library */ @@ -320,26 +327,32 @@ export function main({ basePath, buildPath, inline, + inputs, debug, outFile, silent }: BuildLibraryOptions): void { - if (!silent) { - console.log("-----"); - console.log("build_lib"); - console.log(); - console.log(`basePath: "${basePath}"`); - console.log(`buildPath: "${buildPath}"`); - if (inline && inline.length) { - console.log(`inline:`); - for (const filename of inline) { - console.log(` "${filename}"`); - } + setSilent(silent); + log("-----"); + log("build_lib"); + log(); + log(`basePath: "${basePath}"`); + log(`buildPath: "${buildPath}"`); + if (inline && inline.length) { + log("inline:"); + for (const filename of inline) { + log(` "${filename}"`); + } + } + if (inputs && inputs.length) { + log("inputs:"); + for (const input of inputs) { + log(` "${input}"`); } - console.log(`debug: ${!!debug}`); - console.log(`outFile: "${outFile}"`); - console.log(); } + log(`debug: ${!!debug}`); + log(`outFile: "${outFile}"`); + log(); // the inputProject will take in the TypeScript files that are internal // to Deno to be used to generate the library @@ -348,10 +361,9 @@ export function main({ baseUrl: basePath, declaration: true, emitDeclarationOnly: true, - lib: [], - module: ModuleKind.AMD, + module: ModuleKind.ESNext, moduleResolution: ModuleResolutionKind.NodeJs, - noLib: true, + // noLib: true, paths: { "*": ["*", `${buildPath}/*`] }, @@ -365,20 +377,23 @@ export function main({ // Add the input files we will need to generate the declarations, `globals` // plus any modules that are importable in the runtime need to be added here // plus the `lib.esnext` which is used as the base library - inputProject.addExistingSourceFiles([ - `${basePath}/node_modules/typescript/lib/lib.esnext.d.ts`, - `${basePath}/js/deno.ts`, - `${basePath}/js/globals.ts` - ]); + if (inputs) { + inputProject.addExistingSourceFiles( + inputs.map(input => join(basePath, input)) + ); + } // emit the project, which will be only the declaration files const inputEmitResult = inputProject.emitToMemory(); + log("Emitted input project."); + const inputDiagnostics = inputEmitResult .getDiagnostics() .map(d => d.compilerObject); logDiagnostics(inputDiagnostics); if (inputDiagnostics.length) { + console.error("\nDiagnostics present during input project emit.\n"); process.exit(1); } @@ -419,7 +434,6 @@ export function main({ compilerOptions: { baseUrl: buildPath, moduleResolution: ModuleResolutionKind.NodeJs, - noLib: true, strict: true, target: ScriptTarget.ESNext }, @@ -445,7 +459,7 @@ export function main({ // Generate a object hash of substitutions of modules to use when flattening const customSources = { - [msgGeneratedDts.getFilePath()]: `${ + [msgGeneratedDts.getFilePath().replace(/(\.d)?\.ts$/, "")]: `${ debug ? getSourceComment(msgGeneratedDts, basePath) : "" }${msgGeneratedDtsText}\n` }; @@ -462,9 +476,7 @@ export function main({ targetSourceFile: libDTs }); - if (!silent) { - console.log(`Merged "globals" into global scope.`); - } + log(`Merged "globals" into global scope.`); flatten({ basePath, @@ -478,9 +490,7 @@ export function main({ targetSourceFile: libDTs }); - if (!silent) { - console.log(`Created module "deno" and namespace Deno.`); - } + log(`Created module "deno" and namespace Deno.`); // Inline any files that were passed in, to be used to add additional libs // which are not part of TypeScript. diff --git a/tools/ts_library_builder/main.ts b/tools/ts_library_builder/main.ts index c3192a753..1d1cf14cb 100644 --- a/tools/ts_library_builder/main.ts +++ b/tools/ts_library_builder/main.ts @@ -42,6 +42,11 @@ buildRuntimeLib({ buildPath, debug, inline, + inputs: [ + "node_modules/typescript/lib/lib.esnext.d.ts", + "js/deno.ts", + "js/globals.ts" + ], outFile, silent }); diff --git a/tools/ts_library_builder/test.ts b/tools/ts_library_builder/test.ts index 7907e1c53..b1a4ae344 100644 --- a/tools/ts_library_builder/test.ts +++ b/tools/ts_library_builder/test.ts @@ -4,7 +4,7 @@ // ./node_modules/.bin/ts-node --project tools/ts_library_builder/tsconfig.json tools/ts_library_builder/test.ts import * as assert from "assert"; -import { Project, ts } from "ts-simple-ast"; +import { Project, ts } from "ts-morph"; import { flatten, mergeGlobal } from "./build_library"; import { inlineFiles, loadDtsFiles } from "./ast_util"; @@ -20,7 +20,7 @@ function setupFixtures() { baseUrl: basePath, declaration: true, emitDeclarationOnly: true, - module: ModuleKind.AMD, + module: ModuleKind.ESNext, moduleResolution: ModuleResolutionKind.NodeJs, strict: true, stripInternal: true, |