UNPKG

@ali-i18n-fe/dada-component

Version:
209 lines (187 loc) 4.95 kB
const path = require("path"); const fs = require("fs"); const pickBy = require("lodash/pickBy"); const portfinder = require("portfinder"); const exec = require("shelljs.exec"); const { log } = require("@ali-i18n-fe/lsc-utils"); const { cacheWrap } = require("./cache"); const chunk = require("lodash/chunk"); const wait = (time = 0) => { return new Promise((resolve) => setTimeout(resolve, time)); }; exports.wait = wait; /** * shell 同步执行命令 * @param command * @param options */ exports.shellSyncExec = (command, options = {}) => { const { code, error } = exec(command, { stdio: "inherit", ...options, }); if (!!code) { throw new Error(error); process.exit(1); } }; /** * 读取对应目录的package.json name * @param pathValue * @returns {boolean|*} */ exports.readPackageJsonVersion = (pathValue) => { const source_dest = path.resolve(process.cwd(), "./"); try { const { name } = require(path.resolve( source_dest, pathValue, "./package.json" )); return name; } catch (e) { return false; } }; /** * 获取当前目录下的所有文件夹 * @returns {*} */ exports.getDirList = (source_dest = false) => { if (!source_dest) { source_dest = path.resolve(process.cwd(), "./"); } return fs.readdirSync(source_dest); }; async function getDevConfig() { const config = getCurrentPathConfig(); const port = await portfinder.getPortPromise({ port: config.devPort || 18000, }); return Object.assign(config, { devPort: port }); } exports.getDevConfig = getDevConfig; function getPackageConfig() { const { getPackageConfig } = require("./getPackageJson"); return getPackageConfig(); } exports.getPackageConfig = getPackageConfig; /** * 获取当前目录下 component.config.js * @returns {{ * libraryName: *, * extendTemplate: String, * webpackMerge: *, * publicPath: String, * devPort: Number, * rootPath: * * }} */ function getCurrentPathConfig() { const currentPath = path.resolve(process.cwd()); let configRc = {}; let targetPackageJson = {}; let extendTemplate; try { targetPackageJson = require(path.resolve(currentPath, "./package.json")); } catch (e) { throw new Error("指定项目不存在Package.json"); } try { const compPath = path.resolve(currentPath, "./component.config.js"); if (fs.existsSync(compPath)) { configRc = require(compPath); } const lscPath = path.resolve(currentPath, "./dada.config.js"); if (fs.existsSync(lscPath)) { configRc = require(lscPath); } } catch (e) { console.error(e); } try { if (configRc.extendTemplate) { extendTemplate = fs.existsSync(configRc.extendTemplate) ? fs.readFileSync(configRc.extendTemplate, "utf8") : configRc.extendTemplate; } } catch (e) {} return pickBy( { type: configRc.type, group: configRc.group, project: configRc.project, libraryName: configRc.libraryName || targetPackageJson.name || "component", extendTemplate, storiesWebpackMerge: configRc.storiesWebpackMerge, webpackMerge: configRc.webpackMerge, publicPath: configRc.publicPath, snapshot: configRc.snapshot, devPort: configRc.port, mockPath: configRc.mockPath, rootPath: currentPath, }, (val) => val !== undefined ); } exports.getCurrentPathConfig = cacheWrap(getCurrentPathConfig); /** * 根据文件逻辑获取是Ts或者Tsx * @param filePath * @returns {*} */ function getTsOrTsx(filePath) { const paths = [ filePath.replace(/\.[tj]sx?$/, ".ts"), filePath.replace(/\.[tj]sx?$/, ".tsx"), ]; return paths.find((path) => fs.existsSync(path)); } exports.getTsOrTsx = getTsOrTsx; /** * 捕获fn运行时异常 * @param fn * @returns {Promise<void>} */ function catchException(fn) { return async () => { try { await fn(); } catch (e) { log.error(e); process.exit(1); } }; } exports.catchException = catchException; /** * 是否存在Preview图片 * @param previewPath * @returns {* | void | Promise<void> | Promise<any>} */ function hasPreviewAtPath( previewPath = path.resolve(process.cwd(), "preview.png") ) { const hasPreview = fs.existsSync(previewPath); return hasPreview && previewPath; } exports.hasPreviewAtPath = hasPreviewAtPath; /** * 分块执行Promise * @param promiseAll * @param limitNum * @returns {Promise<Array>} */ async function promiseAllLimit(promiseAll, limitNum = 10) { const collection = chunk(promiseAll, limitNum); const promiseResult = []; for (const everyPromiseArr of collection) { const promiseArr = everyPromiseArr.map((fn) => fn()); const results = await Promise.all(promiseArr.map((p) => p.catch((e) => e))); promiseResult.push(...results); console.log(`done ${limitNum} list`); } return promiseResult; } exports.promiseAllLimit = promiseAllLimit;