最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - How to run a simple js debugger with typescript (in nx mono-repo) - Stack Overflow

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论