UNPKG

lemon-core

Version:
211 lines 8.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * `exec-cli.ts` * - command line runner w/ local http request. * * * ## run in command line. * ```bash * $ node . -ep goods -sid lemon -cmd sync-list -opt save=0 -page 1 * ``` * * @author Steve Jung <steve@lemoncloud.io> * @date 2019-08-01 initial optimized via `imweb-forms-api/run.js` * @date 2019-11-26 cleanup and optimized for `lemon-core#v2` * * @copyright (C) lemoncloud.io 2019 - All Rights Reserved. */ const request_1 = __importDefault(require("request")); /** ******************************************************************************************************************** * boot loading for global instance manager ** *******************************************************************************************************************/ //! override envrionment. const $env = { TS: '1', LC: '1' }; process.env = Object.assign(process.env, $env); //! - load engine after `process.env` const engine_1 = require("./engine"); const tools_1 = require("./tools/"); //! - initial values. const NS = engine_1.$U.NS('EXEC', 'cyan'); const $pack = (0, tools_1.loadJsonSync)('package.json'); const NAME = $pack.name || 'LEMON API'; const VERS = $pack.version || '0.0.0'; const PORT = engine_1.$U.N($pack.port, 0); // default server port. if (!PORT) throw new Error('.port is required at package.json!'); (0, engine_1._log)(NS, `###### exec[${NAME}@${engine_1.$U.NS(VERS, 'cyan')}${PORT}] ######`); /** ******************************************************************************************************************** * main application ** *******************************************************************************************************************/ //! do run http const do_http = (options) => { if (!options || !options.uri) return Promise.reject(new Error('invalid options')); // const cookies = $cm.prepare(options.uri); options.headers = options.headers || {}; // _log(NS, '! options =', options); //! preven error `body:null` if json. if (options.json && !options.body) { delete options.body; } // options.headers.Cookie = (options.headers.Cookie||'') + (options.headers.Cookie ? '; ':'') + cookies; return new Promise((resolve, reject) => { (0, engine_1._inf)(NS, options.method, options.uri); (0, request_1.default)(options, (error, res, body) => { if (error) { (0, engine_1._err)(NS, '!ERR=', error); return reject(error); } const ctype = res.headers['content-type'] || ''; // _log(NS, '! content-type =', ctype); if (ctype.startsWith('application/json') && typeof body == 'string' && body.startsWith('{') && body.endsWith('}')) { try { body = JSON.parse(body); resolve(body); } catch (e) { (0, engine_1._err)(NS, '! invalid json body =', body); reject(e); } } else { // _log(NS, '! text body =', body); resolve(body); } }); }); }; //! prepare request(json) options const prepare_json = function (method, path, qs, body) { method = method || 'GET'; if (!path) throw Error('path is required!'); const options = { method, uri: path, json: true, qs, body, }; // if (body) options.body = typeof body == 'object' ? JSON.stringify(body) : body; if (body) options.method = 'POST'; body && (0, engine_1._inf)(NS, '> json.body =', JSON.stringify(body)); return options; }; //! wait some const wait_sometime = (that, time) => { time = time || 1500; return new Promise((resolve, reject) => { setTimeout(() => { resolve(that); }, time); }); }; /** ******************************************************************************************************************** * main batch configuration. ** *******************************************************************************************************************/ /** * page로 하는, 배치 작업을 한번에 실행 시키기.. * * ```sh * # example * $ node . -ep user -sid lemon -cmd test-self -opt 'force=1' -page 1 -max 2 */ //! batch-run const ENDPOINT = `http://localhost:${PORT}`; const METHOD = (0, tools_1.getRunParam)('m', 'GET'); const EP = (0, tools_1.getRunParam)('ep', ''); const ID = (0, tools_1.getRunParam)('id', '0'); const IPP = (0, tools_1.getRunParam)('ipp', 0); const WAIT = (0, tools_1.getRunParam)('wait', 1000); const SID = (0, tools_1.getRunParam)('sid', ''); const CMD = (0, tools_1.getRunParam)('cmd', ''); const OPT = (0, tools_1.getRunParam)('opt', ''); const [PAGE, MAX] = (() => { let page = (0, tools_1.getRunParam)('page', ''); let max = (0, tools_1.getRunParam)('max', 1); if (`${page}`.indexOf('~') > 0) { const pages = `${page}`.split('~').map((_) => _.trim()); page = parseInt(pages[0]) || 0; max = parseInt(pages[1]) || 0; } else { page = Number(page); max = Number(max); } return [page, max]; })(); (0, engine_1._log)(NS, 'PAGE ~ MAX =', PAGE, '~', MAX); //! execute page by page. const run_batch = (that) => { //! invoke http(json). const my_chain_run_page = (that) => { const page = engine_1.$U.N(that.page, -1); (0, engine_1._inf)(NS, '#page := ', page); if (page < 0) return Promise.reject(new Error('page is required!')); const body = {}; //{map: that.map, default: that.default, layout: that.layout}; if (that.map) body.map = that.map; if (that.default) body.default = that.default; if (that.layout) body.layout = that.layout; const req = prepare_json(METHOD, `${ENDPOINT}/${EP}/${ID}/${CMD}?sid=${SID}` + (page ? '&page=' + page : '') + (IPP ? '&ipp=' + IPP : '') + (OPT ? '&' : '') + OPT, null, Object.keys(body).length ? body : null); return do_http(req).then((_) => { _.layout && (0, engine_1._log)(NS, '! that[' + page + '].layout =', _.layout); _.range && (0, engine_1._log)(NS, '! that[' + page + '].range =', _.range); _.list && (0, engine_1._log)(NS, '! that[' + page + '].list =', _.list); // if has list. _.list || (0, engine_1._inf)(NS, '!WARN res =', _); // if not list. //! attach to that. if (_.map) that.map = _.map; if (_.default) that.default = _.default; if (_.layout) that.layout = _.layout; if (_.list) that.list = _.list; return that; }); }; return Promise.resolve(that) .then(my_chain_run_page) .then(_ => wait_sometime(_, WAIT)) .then((that) => { const page = engine_1.$U.N(that.page, 0); const list = that.list; const cnt = list ? list.length : -1; // '0' means EOF, -1 means N/A. const total = engine_1.$U.N(that.total, 0); (0, engine_1._inf)(NS, '> cnt@page =', cnt + '@' + page, ':', total); const page2 = METHOD != 'DELETE' && page ? page + 1 : page; if (cnt === 0 || (MAX > 0 && page2 > MAX)) { (0, engine_1._log)(NS, 'FINISHED! Page =', page); return list; } that.page = page2; return run_batch(that); }) .catch(e => { (0, engine_1._err)(NS, '!ERR! FIN=', e); throw e; }); }; //! export. const run = () => { run_batch({ page: PAGE }); }; exports.default = run; //# sourceMappingURL=exec-cli.js.map