UNPKG

zippycli

Version:
172 lines (140 loc) 3.42 kB
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; } /* eslint-disable import/no-default-export */ import { flags } from '@oclif/command'; import { extract } from 'zs-extract'; import { DEFAULT_TIMEOUT } from "../constants.mjs"; import { Command } from "../command.mjs"; /** * JSON encode a variable. * * @param v Any value. * @returns JSON string. */ const jsonE = v => JSON.stringify(v); /** * Extract command. */ export class Extract extends 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 extract(source, req); if (isJSON) { this.log(` "download": ${jsonE(download)},`); this.log(` "filename": ${jsonE(filename)}`); } else { this.log(`download: ${download}`); this.log(`filename: ${filename || ''}`); } } } _defineProperty(Extract, "aliases", ['ex', 'e']); _defineProperty(Extract, "description", 'extract data about download'); _defineProperty(Extract, "examples", []); _defineProperty(Extract, "flags", { help: flags.help({ char: 'h' }), format: flags.string({ char: 'f', description: 'output format', options: ['text', 'json'], default: 'text' }), input: flags.boolean({ char: 'i', description: 'source is input file with a URL on each line' }), timeout: flags.integer({ char: 't', description: 'request timeout in seconds', default: DEFAULT_TIMEOUT }) }); _defineProperty(Extract, "args", [{ name: 'source', required: true, description: 'source to extract from' }]); export default Extract; //# sourceMappingURL=extract.mjs.map