diff --git a/packages/ts-simplify/bin/ts-simplify.ts b/packages/ts-simplify/bin/ts-simplify.ts index e767682..c5cc864 100644 --- a/packages/ts-simplify/bin/ts-simplify.ts +++ b/packages/ts-simplify/bin/ts-simplify.ts @@ -1,5 +1,6 @@ import { simplifyTypes } from "../src/simplify-types"; declare const process: { argv: string[] }; -const [_, __, source, output] = process.argv; -simplifyTypes({ source, output }); +const [_, __, sourceFile, outputFile] = process.argv; +const outputText = simplifyTypes({ sourceFile }); +console.log(outputText); diff --git a/packages/ts-simplify/package.json b/packages/ts-simplify/package.json index d3dac42..a253e96 100644 --- a/packages/ts-simplify/package.json +++ b/packages/ts-simplify/package.json @@ -9,8 +9,12 @@ "keywords": [], "author": "", "license": "MIT", + "dependencies": { + "debug": "^4.3.4", + "ts-morph": "^20.0.0" + }, "devDependencies": { - "ts-morph": "^20.0.0", + "@types/debug": "^4.1.9", "ts-node": "^10.9.1", "typescript": "^5.2.2" } diff --git a/packages/ts-simplify/src/simplify-types.ts b/packages/ts-simplify/src/simplify-types.ts index 3c11b49..5614a62 100644 --- a/packages/ts-simplify/src/simplify-types.ts +++ b/packages/ts-simplify/src/simplify-types.ts @@ -1,24 +1,30 @@ +import debug from "debug"; import { Project, TypeFormatFlags } from "ts-morph"; -const logger = { - info(...messages: any[]): void { - console.log(...messages); - }, -}; +namespace logger { + const base = debug("ts-simplify"); + export const info = base.extend("info"); + export const warn = base.extend("warn"); + export const error = base.extend("error"); +} -export function simplifyTypes({ source = "", output = "" }) { +export function simplifyTypes(config: { sourceFile: string }) { const project = new Project({ tsConfigFilePath: "tsconfig.json", skipAddingFilesFromTsConfig: true, }); - const sourceFile = project.addSourceFileAtPath(source); + logger.info(`Loading source file at ${config.sourceFile}`); + + const sourceFile = project.addSourceFileAtPath(config.sourceFile); const sourceTypes = sourceFile.getTypeAliases().filter((type) => type.isExported()); + logger.info(`Source file exports ${sourceTypes.length} types: ${sourceTypes.map((t) => t.getName()).join(", ")}`); + const simplifiedFile = project.createSourceFile( "./virtual/simplified.ts", unindent(` - import * as SOURCES from '../${source}'; + import * as SOURCES from '../${config.sourceFile}'; type SimplifyDeep = T extends object ? T extends infer O @@ -38,7 +44,7 @@ export function simplifyTypes({ source = "", output = "" }) { ); const outputFile = project.createSourceFile( - output || "./virtual/output.ts", + "./virtual/output.ts", ` // Generated by ts-simplify `, @@ -59,12 +65,18 @@ export function simplifyTypes({ source = "", output = "" }) { type: expandedType, }); } - console.log(simplifiedFile.getText({ includeJsDocComments: true })); - console.log(outputFile.getText({ includeJsDocComments: true })); + + const outputText = unindent(` + // Generated by ts-simplify + // Original source: ${config.sourceFile} + ${outputFile.getText()} + `); + + return outputText; } function unindent(str: string) { const lines = str.split("\n"); - const indent = lines[0].match(/^\s*/)![0]; + const indent = lines[1].match(/^\s*/)![0]; return lines.map((line) => line.replace(indent, "")).join("\n"); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca0f74b..3f69911 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -233,6 +233,12 @@ importers: packages/ts-simplify: devDependencies: + '@types/debug': + specifier: ^4.1.9 + version: 4.1.9 + debug: + specifier: ^4.3.4 + version: 4.3.4(supports-color@8.1.1) ts-morph: specifier: ^20.0.0 version: 20.0.0 @@ -1457,7 +1463,7 @@ packages: engines: {node: '>=14'} dependencies: '@open-draft/until': 1.0.3 - '@types/debug': 4.1.7 + '@types/debug': 4.1.9 '@xmldom/xmldom': 0.7.5 debug: 4.3.4(supports-color@8.1.1) headers-polyfill: 3.1.0 @@ -2238,8 +2244,8 @@ packages: resolution: {integrity: sha512-vt+kDhq/M2ayberEtJcIN/hxXy1Pk+59g2FV/ZQceeaTyCtCucjL2Q7FXlFjtWn4n15KCr1NE2lNNFhp0lEThw==} dev: true - /@types/debug@4.1.7: - resolution: {integrity: sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==} + /@types/debug@4.1.9: + resolution: {integrity: sha512-8Hz50m2eoS56ldRlepxSBa6PWEVCtzUo/92HgLc2qTMnotJNIm7xP+UZhyWoYsyOdd5dxZ+NZLb24rsKyFs2ow==} dependencies: '@types/ms': 0.7.31 dev: true