UNPKG

magica

Version:

ImageMagick for browser and Node.js, easy setup, high level API and Command Line Interface, including WASM binary for an easy setup.

117 lines 5.66 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const misc_utils_of_mine_generic_1 = require("misc-utils-of-mine-generic"); const file_1 = require("../file/file"); const magickLoaded_1 = require("../imageMagick/magickLoaded"); const options_1 = require("../options"); const command_1 = require("./command"); const customCommand_1 = require("./customCommand"); const executeCommandPreprocessor_1 = require("./executeCommandPreprocessor"); const main_1 = require("./main"); /** * Has a signature compatible with main, but if `script` is given instead of `command` option then it's * interpreted as a sequence of commands that are executed serially using [[main]] * * The output files of command N are added as input files for command n+1 replacing files with the same name. * This way users can write script-like behavior for complex tasks that require more than one command to be * implemented. * * Also it supports shell script comments (lines starting with `#` are ignored) and breaking a single command * in multiple lines using `\`. * * See [[RunOptions.script]] option. * * @returns the result of each command execution */ function run(o) { return __awaiter(this, void 0, void 0, function* () { const t0 = Date.now(); o.debug && options_1.setOptions({ debug: o.debug }); const emscriptenNodeFsRoot = options_1.getOption('emscriptenNodeFsRoot'); const { FS } = yield magickLoaded_1.magickLoaded; FS.chdir(emscriptenNodeFsRoot); let inputFiles = yield file_1.File.resolve(o.inputFiles); inputFiles = inputFiles.filter(misc_utils_of_mine_generic_1.notUndefined).map(file_1.File.asFile); const commands = yield resolveRunCommands(Object.assign(Object.assign({}, o), { inputFiles })); const finalResult = { results: [], commands, outputFiles: [], stderr: [], stdout: [], error: undefined, returnValue: undefined }; yield misc_utils_of_mine_generic_1.serial(commands.map((command, i) => () => __awaiter(this, void 0, void 0, function* () { // inputFiles = inputFiles.map(File.asFile) let mainOptions = Object.assign(Object.assign({}, o), { command, inputFiles }); //TODO yield executeCommandPreprocessor_1._runTimePreprocess(o, mainOptions, i); let result; if (yield customCommand_1.isCustomCommand(command)) { const customCommandOptions = { command, options: mainOptions, FS, // files: inputFiles, outputFiles: inputFiles }; result = Object.assign(Object.assign({}, yield customCommand_1.dispatchCustomCommand(customCommandOptions)), { outputFiles: inputFiles }); } else { result = yield main_1.main(mainOptions); } inputFiles = [ ...inputFiles.filter(f => !result.outputFiles.find(f2 => f2.name === f.name)), ...result.outputFiles ] .map(file_1.File.asFile); finalResult.results.push(result); finalResult.commands[i] = command_1.processCommand(mainOptions.command); }))); const r = Object.assign(Object.assign({}, finalResult), { stdout: finalResult.results.map(r => r.stdout).flat(), stderr: finalResult.results.map(r => r.stderr).flat(), outputFiles: finalResult.results.length ? finalResult.results[finalResult.results.length - 1].outputFiles : [], times: { total: Date.now() - t0 } }); return r; }); } exports.run = run; function resolveRunCommands(o) { return __awaiter(this, void 0, void 0, function* () { if ((!o.script || !o.script.length) && (!o.command || !o.command.length)) { throw new Error('No script or command given'); } o = yield executeCommandPreprocessor_1._compileTimePreprocess(o); let script; if (o.script && o.script.length) { script = misc_utils_of_mine_generic_1.asArray(o.script).join('\n'); } else { script = command_1.arrayToCliOne(misc_utils_of_mine_generic_1.asArray(o.command)); } const commands = command_1.cliToArray(script); return commands; }); } function runOne(script, input = []) { return __awaiter(this, void 0, void 0, function* () { const result = yield run({ inputFiles: misc_utils_of_mine_generic_1.asArray(input), script }); if (result.error) { throw new Error(result.error + '' + result.stderr.join(', ')); } if (!result.outputFiles.length) { throw new Error('Expected output files but none resulted'); } return result.outputFiles[0]; }); } exports.runOne = runOne; //# sourceMappingURL=run.js.map