UNPKG

pandora

Version:

A powerful and lightweight application manager for Node.js applications powered by TypeScript.

88 lines 2.94 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const fs_1 = require("fs"); const const_1 = require("../const"); const GlobalConfigProcessor_1 = require("./GlobalConfigProcessor"); const extend = require('extend'); function calcAppName(dir) { let ret; try { const pkg = require(path_1.join(process.cwd(), 'package.json')); ret = pkg.name; } catch (err) { ret = null; } if (!ret && dir) { ret = dir.split('/').slice(-1)[0] || null; } return ret; } exports.calcAppName = calcAppName; function attachEntryParams(command, cliConfig, defaultConfig = {}) { const currentPath = process.cwd(); let pandoraConfig; try { const pandoraAttachConfig = require(`${currentPath}/package.json`)['pandora']; pandoraConfig = pandoraAttachConfig[command] || {}; pandoraConfig['config'] = pandoraConfig['config'] || []; // set global config to environment pandoraConfig['config'].push(process.env[const_1.PANDORA_GLOBAL_CONFIG] || ''); process.env[const_1.PANDORA_GLOBAL_CONFIG] = pandoraConfig['config'].filter((text) => { return !!text; }).join(GlobalConfigProcessor_1.GlobalConfigProcessor.GLOBAL_PACKAGE_SPLIT); } catch (err) { // console.log(err); pandoraConfig = {}; } const sendConfig = extend(true, defaultConfig, pandoraConfig || {}, cliConfig); sendConfig['entry'] = sendConfig['entry'] || currentPath; try { const p = path_1.resolve(sendConfig['entry']); let fd = fs_1.statSync(p); if (fd.isDirectory()) { sendConfig['appDir'] = p; } else if (fd.isFile()) { sendConfig['entryFile'] = p; sendConfig['appDir'] = currentPath; } } catch (err) { console.error(err); } if (cliConfig.name) { sendConfig.appName = cliConfig.name; } if (sendConfig.env) { try { const envMap = {}; const splitEnv = sendConfig.env.split(' '); for (const item of splitEnv) { const execRes = /^(.*?)=(.*)$/.exec(item); const key = execRes[1]; const value = execRes[2]; envMap[key] = value; } sendConfig.globalEnv = envMap; } catch (err) { console.error(err); } } if (sendConfig['argv']) { sendConfig.globalArgv = sendConfig.argv.split(' '); } const sendConfig2nd = {}; for (const key of ['appName', 'appDir', 'entryFileBaseDir', 'entryFile', 'scale', 'mode', 'globalEnv', 'globalArgv']) { if (sendConfig.hasOwnProperty(key)) { sendConfig2nd[key] = sendConfig[key]; } } return sendConfig2nd; } exports.attachEntryParams = attachEntryParams; //# sourceMappingURL=Helpers.js.map