Source: test/egg/eggc.js

#!/usr/bin/env node
/**
 * @description A executable to be able to compile eggc lang files
 * @author AdayCuestaCorrea <alu0101483887@ull.edu.es>
 * @since 02/04/2024
 */

 'use strict';

 const fs = require('fs');
 const { parseFromFile } = require("./index.js");

/**
 * Compiles the given origin file into an abstract syntax tree (AST) and writes it to the destination file.
 * If no destination file is provided, it will use the same name as the origin file with a '.json' extension.
 *
 * @param {string} origin - The path of the origin file to compile.
 * @param {string} [destination] - The path of the destination file to write the compiled AST. If not provided, a default destination file will be used.
 * @returns {void}
 */
const compile = (origin, destination = undefined) => {
  if (destination == undefined) {
    destination = origin.match(/^[^\.]*/)[0] + '.json';
  }

  const ast = parseFromFile(origin);

  //console.log(ast);
  const astString = JSON.stringify(ast, null, 2);

  fs.writeFileSync(destination, astString);
};

compile(process.argv[2] || 'array.egg');