pandora
Version:
A powerful and lightweight application manager for Node.js applications powered by TypeScript.
91 lines • 3.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const fs_1 = require("fs");
const pandora_dollar_1 = require("pandora-dollar");
function calcAppName(dir) {
let ret;
try {
const pkg = require(path_1.join(dir, 'package.json'));
pkg.name.toString();
ret = removePkgNameScope(pkg.name);
}
catch (err) {
ret = null;
}
if (!ret && dir) {
ret = dir.split('/').slice(-1)[0] || null;
}
return ret;
}
exports.calcAppName = calcAppName;
function removePkgNameScope(pkgName) {
return pkgName.replace(/^@[^\/]*\//, '');
}
exports.removePkgNameScope = removePkgNameScope;
function cliParamsToApplicationRepresentation(command, cliConfig) {
const result = {};
const cwd = process.cwd();
const targetPath = result.appDir = cliConfig.targetPath || cwd;
const appDir = path_1.resolve(targetPath);
if (!fs_1.statSync(appDir).isDirectory()) {
const dir = path_1.dirname(targetPath);
throw new Error(`Pandora.js can only start a Pandora.js project directory, like [ pandora ${command}${dir === '.' ? ' ./' : ' ' + dir + '/'} ]\nYou can use [ pandora init ${targetPath} ] to init a Pandora.js project`);
}
result.appDir = appDir;
result.appName = cliConfig.name || calcAppName(appDir);
if (cliConfig.env) {
try {
const envMap = {};
const splitEnv = cliConfig.env.split(' ');
for (const item of splitEnv) {
const execRes = /^(.*?)=(.*)$/.exec(item);
const key = execRes[1];
const value = execRes[2];
envMap[key] = value;
}
result.globalEnv = envMap;
}
catch (err) {
pandora_dollar_1.consoleLogger.error(err);
}
}
if (cliConfig.args) {
result.globalArgs = cliConfig.args.split(' ');
}
if (cliConfig['node-args']) {
result.globalExecArgv = cliConfig['node-args'].split(' ');
}
for (const optName of ['inspect-port', 'inspect']) {
if (cliConfig.hasOwnProperty(optName)) {
const inspect = cliConfig[optName];
if (optName === 'inspect' && inspect === '' || true === inspect) { // be an empty str
result.inspector = true;
}
else if (typeof inspect === 'string' && inspect) {
const { host, port } = parseInspectPort(inspect);
result.inspector = { host, port, setPortOnly: optName === 'inspect-port' };
}
break;
}
}
return result;
}
exports.cliParamsToApplicationRepresentation = cliParamsToApplicationRepresentation;
function parseInspectPort(inspect) {
const split = inspect.split(':');
let port, host;
if (split.length >= 2) {
host = split[0];
port = parseInt(split[1], 10);
}
else if (inspect.indexOf('.') > -1) {
host = inspect;
}
else {
port = parseInt(inspect, 10);
}
return { host, port };
}
exports.parseInspectPort = parseInspectPort;
//# sourceMappingURL=Helpers.js.map