@fxext/cli
Version:
fanxing miniapp cli
63 lines (57 loc) • 1.62 kB
JavaScript
const glob = require('glob');
const path = require('path');
const fs = require('fs-extra');
const os = require('os');
const log = require('./log');
function errorHandler(desc, errMsg) {
throw new Error(`${desc} \r\n` + JSON.stringify(errMsg));
}
function getJsEntry(patterns, root) {
const files = glob.sync(patterns, { root });
return files.reduce((result, file) => {
const dirname = path.dirname(file);
const filename = path.basename(file, path.extname(file));
const key = path.join(dirname, filename).replace(root, '').replace(/\\/g, '/');
return Object.assign(result, {
[key]: [file],
});
}, {});
}
/**
* 解析project.config.json
*/
async function parseProjectConfig(root) {
try {
const configFilePath = path.resolve(root, './project.config.json');
const exists = await fs.pathExists(configFilePath);
if (exists) {
const config = require(configFilePath);
return config;
} else {
log.error('project.config.json不存在');
process.exit(1);
}
} catch (error) {
log.error(error);
process.exit(1);
}
}
// 获取本地ip地址
function getLocalIP() {
const interfaces = os.networkInterfaces();
for (const devName in interfaces) {
const iface = interfaces[devName];
for (let i = 0; i < iface.length; i++) {
const alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
}
module.exports = {
errorHandler,
getJsEntry,
parseProjectConfig,
getLocalIP,
};