Skip to content

Commit

Permalink
`types(groq-builder): improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrippey committed Oct 5, 2023
1 parent 13a57ec commit 934e5b1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
5 changes: 3 additions & 2 deletions packages/ts-simplify/bin/ts-simplify.ts
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 5 additions & 1 deletion packages/ts-simplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
36 changes: 24 additions & 12 deletions packages/ts-simplify/src/simplify-types.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends object
? T extends infer O
Expand All @@ -38,7 +44,7 @@ export function simplifyTypes({ source = "", output = "" }) {
);

const outputFile = project.createSourceFile(
output || "./virtual/output.ts",
"./virtual/output.ts",
`
// Generated by ts-simplify
`,
Expand All @@ -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");
}
12 changes: 9 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 934e5b1

Please sign in to comment.