lemon-devkit
Version:
Lemon Serverless Micro-Service Platform for local development
222 lines • 8.99 kB
JavaScript
;
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = exports.run_batch = void 0;
/**
* `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 2025-05-20 optimize for `lemon-core#v4`
*
* @copyright (C) lemoncloud.io 2025 - 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 lemon_core_1 = require("lemon-core");
const shared_1 = require("./tools/shared");
//* - initial values.
const NS = lemon_core_1.$U.NS('EXEC', 'cyan');
const $pack = (0, lemon_core_1.loadJsonSync)('package.json');
const NAME = $pack.name || 'LEMON API';
const VERS = $pack.version || '0.0.0';
const PORT = lemon_core_1.$U.N($pack.port, 0); // default server port.
if (!PORT)
throw new Error('.port is required at package.json!');
(0, lemon_core_1._log)(NS, `###### exec[${NAME}@${lemon_core_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, lemon_core_1._inf)(NS, options.method, options.uri);
(0, request_1.default)(options, (error, res, body) => {
if (error) {
(0, lemon_core_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, lemon_core_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, lemon_core_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, shared_1.getRunParam)('m', 'GET');
const EP = (0, shared_1.getRunParam)('ep', '');
const ID = (0, shared_1.getRunParam)('id', '0');
const IPP = (0, shared_1.getRunParam)('ipp', 0);
const WAIT = (0, shared_1.getRunParam)('wait', 1000);
const SID = (0, shared_1.getRunParam)('sid', '');
const CMD = (0, shared_1.getRunParam)('cmd', '');
const OPT = (0, shared_1.getRunParam)('opt', '');
const [PAGE, MAX] = (() => {
let page = (0, shared_1.getRunParam)('page', '');
let max = (0, shared_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, lemon_core_1._log)(NS, 'PAGE ~ MAX =', PAGE, '~', MAX);
//* execute page by page.
const run_batch = (that) => __awaiter(void 0, void 0, void 0, function* () {
//* invoke http(json).
const my_chain_run_page = (that) => {
const page = lemon_core_1.$U.N(that.page, -1);
(0, lemon_core_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, lemon_core_1._log)(NS, '! that[' + page + '].layout =', _.layout);
_.range && (0, lemon_core_1._log)(NS, '! that[' + page + '].range =', _.range);
_.list && (0, lemon_core_1._log)(NS, '! that[' + page + '].list =', _.list); // if has list.
_.list || (0, lemon_core_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 = lemon_core_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 = lemon_core_1.$U.N(that.total, 0);
(0, lemon_core_1._inf)(NS, '> cnt@page =', cnt + '@' + page, ':', total);
const page2 = METHOD != 'DELETE' && page ? page + 1 : page;
if (cnt === 0 || (MAX > 0 && page2 > MAX)) {
(0, lemon_core_1._log)(NS, 'FINISHED! Page =', page);
return list;
}
that.page = page2;
return (0, exports.run_batch)(that);
})
.catch(e => {
(0, lemon_core_1._err)(NS, '!ERR! FIN=', e);
throw e;
});
});
exports.run_batch = run_batch;
//* export.
const run = () => {
(0, exports.run_batch)({ page: PAGE });
};
exports.run = run;
//# sourceMappingURL=exec-cli.js.map