UNPKG

zippycli

Version:
184 lines (146 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.Extract = void 0; var _command = require("@oclif/command"); var _zsExtract = require("zs-extract"); var _constants = require("../constants"); var _command2 = require("../command"); function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * JSON encode a variable. * * @param v Any value. * @returns JSON string. */ const jsonE = v => JSON.stringify(v); /** * Extract command. */ class Extract extends _command2.Command { /** * Aliases. */ /** * Description. */ /** * Examples. */ /** * Flags. */ /** * Arguments. */ /** * Handler. */ async run() { const { args, flags } = this.parse(Extract); const source = args.source; const { format, timeout } = flags; const isJSON = format === 'json'; const sources = flags.input ? await this._readInputFile(source) : [source]; const req = this._initRequest(timeout * 1000); let errors = false; if (isJSON) { this.log('['); } for (let i = 0; i < sources.length; i++) { const source = sources[i]; if (isJSON) { this.log(' {'); } else if (i) { this.log(''); } try { // eslint-disable-next-line no-await-in-loop await this._handleSource(source, req, format); } catch (err) { errors = true; const error = String(err); if (isJSON) { this.log(` "error": ${jsonE(error)}`); } else { this.log(`error: ${error}`); } } if (isJSON) { const c = i + 1 === sources.length ? '' : ','; this.log(` }${c}`); } } if (isJSON) { this.log(']'); } if (errors) { this.exit(1); } } /** * Handle an individual source. * * @param source The source. * @param req Request factory. * @param format Output format. */ async _handleSource(source, req, format) { const isJSON = format === 'json'; if (isJSON) { this.log(` "source": ${jsonE(source)},`); } else { this.log(`source: ${source}`); } const { download, filename } = await (0, _zsExtract.extract)(source, req); if (isJSON) { this.log(` "download": ${jsonE(download)},`); this.log(` "filename": ${jsonE(filename)}`); } else { this.log(`download: ${download}`); this.log(`filename: ${filename || ''}`); } } } exports.Extract = Extract; _defineProperty(Extract, "aliases", ['ex', 'e']); _defineProperty(Extract, "description", 'extract data about download'); _defineProperty(Extract, "examples", []); _defineProperty(Extract, "flags", { help: _command.flags.help({ char: 'h' }), format: _command.flags.string({ char: 'f', description: 'output format', options: ['text', 'json'], default: 'text' }), input: _command.flags.boolean({ char: 'i', description: 'source is input file with a URL on each line' }), timeout: _command.flags.integer({ char: 't', description: 'request timeout in seconds', default: _constants.DEFAULT_TIMEOUT }) }); _defineProperty(Extract, "args", [{ name: 'source', required: true, description: 'source to extract from' }]); var _default = Extract; exports.default = _default; //# sourceMappingURL=extract.js.map