UNPKG

json-processing

Version:

JSON Processing Tool

86 lines (85 loc) 3.48 kB
'use strict'; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const argv_1 = __importDefault(require("./argv")); const runner_1 = require("./runner"); const bluebird_1 = __importDefault(require("bluebird")); const bluebird_2 = require("bluebird"); const utils_1 = __importDefault(require("./lib/extensions/utils")); const rxjs_1 = __importDefault(require("./lib/extensions/rxjs")); const rxjs = __importStar(require("rxjs/operators")); const fs_1 = require("fs"); const path_1 = require("path"); exports.default = (argv) => argv_1.default(argv).then((options) => { const input = options.input; const output = options.output; const outputMode = options['output-mode']; const home = options.home; const command = options.command; const inline = options.inline; return verify(home).spread((cmdsPath, utilsPath, pluginsJson) => { const runner = new runner_1.ScriptRunner(input) .addGlobal(rxjs) .addGlobal(utils_1.default) .addGlobal(rxjs_1.default); if (utilsPath) runner.addGlobalFromPath(utilsPath); if (cmdsPath) runner.setCommandsPath(cmdsPath); if (pluginsJson) runner.setPluginsInfo(pluginsJson); if (command) runner.setCommand(command.name). setCommandArgs(command.args); if (inline) runner.setInlineScript(inline); return bluebird_2.attempt(() => runner.run()). then((observable => new bluebird_1.default((resolve, reject) => outputMode(observable, output). on('error', (err) => reject(err)). on('finish', () => resolve())))); }); }); function verify(repository) { return dirExists(repository). then(rootPath => { if (!rootPath) return [false, false, false]; return bluebird_1.default.all([ dirExists(path_1.resolve(repository, 'scripts')), dirExists(path_1.resolve(repository, 'utils')), fileExists(path_1.resolve(repository, 'plugins.json')) ]); }); function fileExists(path) { return exists(path, stats => !stats.isDirectory()); } function dirExists(path) { return exists(path, stats => stats.isDirectory()); } function exists(path, check) { return new bluebird_1.default(resolve => { fs_1.stat(path, (err, stats) => resolve(stats && check(stats) && path)); }); } }