Very basic question. I'm not sure if this is a node / typescript question, a vs code question, or an nx question.
In an nx monorepo, say you have a simple .js script located in an nx library project that is used to convert some files from one format to another. How do you set up nx to a) run the script, b) to allow you to debug the script.
If the source file is called monkey-schemas.ts and contains:
import * as openApiSchema from './open-api-schemas';
export class monkeySchemas {
static nameSchema : openApiSchema.SchemaObject = {
type: "string",
description: "Name field"
}
static testModel : interfaces.SchemaObject = {
type: "object",
properties: {
body: {
type: "object",
properties: {
firstName: MonkeyModels.nameSchema,
secondName: MonkeyModels.nameSchema
}
}
}
}
}
and the expectation is to output a file containing the simple ts interface:
export interface testModel { firstName?: string; secondName?: string; }
In my case the script is at \libs\monkey-defs\src\scripts\generate-ts-interfaces.js. What I've got for the generate script is this so far:
// Define input and output paths
const inputPath = path.resolve(__dirname, '../lib/models/monkey-schemas.ts');
const outputPath = path.resolve(__dirname, '../out/ts-interfaces/');
// Utility to convert a schema object to TypeScript interface
function schemaToInterface(schemaName, schemaObject) {
const properties = schemaObject.properties || {};
const fields = Object.entries(properties)
.map(([key, value]) => {
const type = value.type || 'any';
return ` ${key}: ${type};`;
})
.join('\n');
return `export interface ${schemaName} {\n${fields}\n}`;
}
// Load schemas
const schemas = require(inputPath);
// Process and write selected schemas to TypeScript interfaces
function generateInterfaces() {
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
Object.entries(schemas.monkeySchemas)
.forEach(([name, schema]) => {
if (schema.type === 'object') {
const tsInterface = schemaToInterface(name, schema);
const filePath = path.join(outputPath, `${name}.ts`);
fs.writeFileSync(filePath, tsInterface, 'utf8');
console.log(`Generated: ${filePath}`);
}
});
}
generateInterfaces();
I'm consious but barely that the .ts needs to be compiled into .js.
I'm at a loss how to join this together in the configurations and so on.