Actor picture

JavaScript Code to Flowchart

drobnikj/js-code-2-flowchart

Use this to convert JavaScript code to a flowchart. The actor uses https://www.npmjs.com/package/js2flowchart npm package to convert the code to a flowchart. The output is an SVG file.

No credit card required

Author's avatarJakub Drobník
  • Modified
  • Users67
  • Runs119
Actor picture

JavaScript Code to Flowchart

Based on the apify/actor-node-basic:v0.21.10 Docker image (see docs).

const Apify = require('apify');
const js2flowchart = require('js2flowchart');

Apify.main(async () => {
    // Get input of your act
    const input = await Apify.getValue('INPUT');
    let inputCode;
    if (Buffer.isBuffer(input)) {
        inputCode = input.toString();
    } else if(input.inputCode) {
        inputCode = input.inputCode;
    } else if(input.crawlerId) {
        const crawlerDetail = await Apify.client.crawlers.getCrawlerSettings({ crawlerId: input.crawlerId });
        if (!crawlerDetail) throw new Error(`Crawler with id ${input.crawlerId} not found`);
        inputCode = crawlerDetail.pageFunction;
    }
    console.log('Input code:');
    console.log(inputCode);
    
    console.log('Generating SVG image...')
    const svg = js2flowchart.convertCodeToSvg(inputCode);

    console.log('Saving svg image...');
    await Apify.setValue('OUTPUT', svg, { contentType: 'image/svg+xml' });
    console.log(`https://api.apify.com/v2/key-value-stores/${Apify.getEnv().defaultKeyValueStoreId}/records/OUTPUT?disableRedirect=1`);
    console.log('Done');
});