UNPKG

@code-to-json/cli

Version:

52 lines 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var path = require("path"); var fs = require("fs"); var ts = require("typescript"); var debug = require("debug"); var util_1 = require("util"); var core_1 = require("@code-to-json/core"); var DEFAULT_COMPILER_OPTIONS = { allowJs: true }; var NS_PER_SEC = 1e9; var debugLog = debug('code-to-json:cli'); function run(entries, out, configPath) { var beginTime = process.hrtime(); var tsCompilerOptions; if (configPath) { // const cfgFileText = fs // .readFileSync(path.join(process.cwd(), configPath)) // .toString(); var cfgPath = ts.findConfigFile(configPath, function (fn) { return true; }); if (!cfgPath) throw new Error('No config file found'); var cfgFilePath = path.isAbsolute(cfgPath) ? cfgPath : path.join(process.cwd(), cfgPath); var _a = ts.readConfigFile(cfgFilePath, function (fPath) { return fs.readFileSync(fPath).toString(); }), config = _a.config, error = _a.error; if (error) { throw new Error("Problem reading " + cfgFilePath + "\n" + error.messageText.toString()); } tsCompilerOptions = config.compilerOptions; } else { tsCompilerOptions = DEFAULT_COMPILER_OPTIONS; } util_1.debuglog("Walking tree using TS compiler options \n" + JSON.stringify(tsCompilerOptions)); util_1.debuglog("...and entries \n" + JSON.stringify(entries)); var prog = ts.createProgram({ rootNames: entries, options: tsCompilerOptions }); var walkResult = core_1.walkProgram(prog); var timeElapsed = process.hrtime(beginTime); console.log("Code extraction took " + Math.round((timeElapsed[0] * NS_PER_SEC + timeElapsed[1]) / 1e3) / 1e3 + "ms"); console.log(walkResult); var outputPath = path.isAbsolute(out) ? out : path.join(process.cwd(), out); fs.writeFileSync(outputPath, JSON.stringify(walkResult, null, ' ')); } exports.default = run; //# sourceMappingURL=run.js.map