asyncapi / generator

Use your AsyncAPI definition to generate literally anything. Markdown documentation, Node.js code, HTML documentation, anything!
https://asyncapi.com/docs/tools/generator
Apache License 2.0
759 stars 218 forks source link

Figure out what development flow will be without cli.js #1226

Open lmgyuan opened 3 months ago

lmgyuan commented 3 months ago

research what would be the best developer experience for local development of generator to see if new features (but not only) work with templates.

I still want to be able to locally run easily generator, modify some dummy asyncapi document that in is generator, add something to react-template that is generator, and see if after generation the feature I added works - and last thing I would do is add tests

2 options:

lmgyuan commented 3 months ago

According to our internal meeting, we want to first deprecae the use of cli.js in our "test:cli": "node cli.js ./test/docs/dummy.yml ./test/test-templates/react-template -o test/output --force-write --debug && test -e test/output/test-file.md". For example, in package.json, we will replace it with "test:cli": node generate_test_script.js. Then, we will add a script like the following to the code base:

const path = require('path');
const fs = require('fs');
const Generator = require('@asyncapi/generator');

// Define paths from environment variables with fallbacks
const ASYNCAPI_DOCUMENT_PATH = process.env.ASYNCAPI_DOCUMENT_PATH || './test/docs/dummy.yml'; // path to your AsyncAPI document
const TEMPLATE_PATH = process.env.TEMPLATE_PATH || './test/test-templates/react-template'; // path to your template
const OUTPUT_DIR = process.env.OUTPUT_DIR || './test/output'; // output directory

// Initialize the generator
const generator = new Generator(
    TEMPLATE_PATH,
    OUTPUT_DIR,
    {
        entrypoint: 'index.html', // specify the entrypoint if needed
        noOverwriteGlobs: [], // glob patterns to exclude from overwriting
        forceWrite: true, // same as --force-write
        debug: true // same as --debug
    }
);

// Function to generate documents
async function generateDocs() {
    try {
        // Ensure output directory exists or create it
        fs.mkdirSync(OUTPUT_DIR, { recursive: true });

        // Generate the files based on the AsyncAPI document and the template
        await generator.generate(ASYNCAPI_DOCUMENT_PATH);

        // Check if a specific file exists
        const checkFilePath = path.join(OUTPUT_DIR, 'test-file.md');
        if (fs.existsSync(checkFilePath)) {
            console.log('File exists:', checkFilePath);
        } else {
            console.log('File does not exist:', checkFilePath);
        }
        console.log('Generation complete!');
    } catch (error) {
        console.error('Error during generation:', error);
    }
}

// Run the generation function
generateDocs();

This script is not final, so open for discussions! Also, please leave comments on what we should do to inform our users about deprecating the cli.js in generator. We are also interested in hardcoding a message for people when they use to say things like "cli.js is about to be deprecated, please adjust your codebase accordingly" etc.

lmgyuan commented 3 months ago

This issue is open for discussion and thoughts! Please leave comments whenever you see fit and let's get this thing going!

: )

Gmin2 commented 3 months ago
const Generator = require('@asyncapi/generator');

This is wrong we need to import the local generator construct and then use it

derberg commented 2 months ago

Hey @lmgyuan

I can't recall why that was closed. Reopening for now, feel free to close again but with comment why.

We should figure out final shape of the script.

We also have to agree when we will add this script. Should we wait one year, and add it the same time we remove cli.js or maybe we just add it now, as it is and gradually improve over time, and stop using cli.js for development immediately

derberg commented 3 weeks ago

@lmgyuan any progress?