UNPKG

uascript

Version:
59 lines (58 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const file_manager_1 = require("./file-manager/file-manager"); const runner_1 = require("./interpreter/runner"); const path = require('path'); const fs = require('fs/promises'); async function main() { const args = process.argv.slice(2); const MAX_SOURCE_SIZE = 10 * 1024 * 1024; if (!args.length || !args[0] || !args[1]) { console.error('Usage: node index.js <mode> <input> [output]'); console.error(' mode: "true" for file compiler, "false" for source compiler'); console.error(' input: path to file/directory or source code'); console.error(' output: (optional) output path for file compiler'); process.exit(1); } const isFileCompiler = Boolean(args[0]); try { if (isFileCompiler && args[2]) { const fromPath = args[1]; const outPath = args[2]; const resolvedFrom = path.resolve(fromPath); const resolvedOut = path.resolve(outPath); const cwd = process.cwd(); if (!resolvedFrom.startsWith(cwd)) { throw new Error(`Input path must be within current directory: ${fromPath}`); } if (!resolvedOut.startsWith(cwd)) { throw new Error(`Output path must be within current directory: ${outPath}`); } try { await fs.access(resolvedFrom); } catch { throw new Error(`Input path does not exist: ${fromPath}`); } await file_manager_1.compileByPath(resolvedFrom, resolvedOut); } else if (!isFileCompiler) { const source = args[1]; if (source.length > MAX_SOURCE_SIZE) { throw new Error(`Source code exceeds maximum size of ${MAX_SOURCE_SIZE} bytes`); } if (source.includes('\0')) { throw new Error('Source code contains null bytes'); } await runner_1.compileAndRunSource(source); } else { throw new Error('Invalid arguments. For file compiler, output path is required.'); } } catch (error) { console.error('Error:', error.message); process.exit(1); } } main();