Skip to content

Commit

Permalink
add dynamic transpiling from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Dec 17, 2023
1 parent 346eb2c commit 42c7d1c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-pianos-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@authdog/hydra-cli": patch
---

add support for config with typescript annotations
6 changes: 3 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node-fetch": "2.6.9",
"typescript": "^5.2.2"
"@types/node-fetch": "2.6.9"
},
"dependencies": {
"node-fetch": "2.6.9",
"commander": "^11.1.0",
"graphql-tag": "^2.12.6",
"zod": "^3.22.4"
"zod": "^3.22.4",
"typescript": "^5.2.2"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/",
Expand Down
63 changes: 42 additions & 21 deletions packages/cli/src/commands/generate-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from "path";
import { buildSchemaIntrospection } from "../utils/introspectSchemas";
import { validateConfig } from "../utils/validateConfig";
import path from "path";
import ts from "typescript";
import fs from "fs"

interface IGenerateSchemaAction {
config?: string;
Expand All @@ -14,29 +16,48 @@ export const generateSchemaAction = async ({
const configPath = path.resolve(rootPath, config); // Construct absolute path for config
const outputPath = path.resolve(rootPath, ".hydra/schemaRaw.ts"); // Construct absolute path for output

let demoConfig: any;

try {
// Use `import` instead of `require` for TypeScript support
const importedConfig = await import(configPath);
demoConfig = importedConfig.default || importedConfig;
} catch (error) {
throw new Error("Error loading or parsing the config file: " + error);
}

console.log(demoConfig);
const validatedConfig = validateConfig(demoConfig);
// Compiler options (optional)
const tsConfig = {
target: ts.ScriptTarget.ESNext,
module: ts.ModuleKind.CommonJS,
};

if (!validatedConfig) {
throw new Error("Invalid config");
}
// Create TypeScript compiler host and program
const compilerHost = ts.createCompilerHost({});
const program = ts.createProgram([configPath], tsConfig, compilerHost);

try {
await buildSchemaIntrospection(validatedConfig.schemas, outputPath);
// Get the source file
const sourceFile = program.getSourceFile(configPath);

if (!sourceFile) {
throw new Error("Source file not found");
}

// Emit the transpiled code
const { outputText } = ts.transpileModule(sourceFile.getText(), { compilerOptions: tsConfig });

// // Evaluate the transpiled code
const module_ = eval(outputText);

const demoConfig = module_.default || module_;

const validatedConfig = validateConfig(demoConfig);

if (!validatedConfig) {
throw new Error("Invalid config");
}

try {
await buildSchemaIntrospection(validatedConfig.schemas, outputPath);
} catch (error) {
console.error(error);
throw new Error("Error generating schema");
}

console.info("Schema generated successfully");
} catch (error) {
console.error(error);
throw new Error("Error generating schema");
throw new Error("Error transpiling or executing TypeScript code: " + error);
}

console.info("Schema generated successfully");
};
};

0 comments on commit 42c7d1c

Please sign in to comment.