@lcap/builder
Version:
lcap builder utils
99 lines (98 loc) • 4.96 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.getComponentConfigByName = exports.getPackName = exports.getConfigComponents = void 0;
exports.getComponentMetaInfos = getComponentMetaInfos;
/* eslint-disable no-await-in-loop */
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const vite_1 = require("vite");
const lodash_1 = require("lodash");
const component_path_1 = require("./component-path");
const logger_1 = __importDefault(require("./logger"));
const shared_1 = require("../shared");
const getConfigComponents = (rootPath) => {
const lcapConfigPath = path_1.default.resolve(rootPath, './lcap-ui.config.js');
if (!fs_extra_1.default.existsSync(lcapConfigPath)) {
return [];
}
// eslint-disable-next-line import/no-dynamic-require, global-require
const config = require(lcapConfigPath);
if (!config || !Array.isArray(config.components)) {
return [];
}
return config.components.map((c) => (Object.assign(Object.assign({}, c), { title: c.alias })));
};
exports.getConfigComponents = getConfigComponents;
const getPackName = (name, version) => `${name.replace(/@/g, '').replace(/\//g, '-')}-${version}.tgz`;
exports.getPackName = getPackName;
const getComponentConfigByName = (name, list) => {
const flatList = list.map((c) => {
const arr = [c];
arr.push(...c.children);
return arr;
}).flat();
return flatList.find((c) => c.name === name);
};
exports.getComponentConfigByName = getComponentConfigByName;
function getComponentMetaInfos(rootPath_1) {
return __awaiter(this, arguments, void 0, function* (rootPath, parseAPI = false) {
const components = (0, exports.getConfigComponents)(rootPath);
const packageInfo = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, 'package.json'));
const metaInfos = [];
if (components && components.length > 0) {
components.forEach((extConfig) => {
const componentRootDir = packageInfo.name === '@lcap/mobile-ui' ? 'src-vusion/components' : 'src/components';
const { componentDir } = (0, component_path_1.getComponentPathInfo)(extConfig.name, rootPath, componentRootDir);
const apiPath = extConfig.apiPath ? path_1.default.join(rootPath, componentRootDir, extConfig.apiPath) : path_1.default.join(componentDir, 'api.ts');
if (!fs_extra_1.default.existsSync(apiPath)) {
logger_1.default.error(`未找到组件 ${extConfig.name} 的描述文件(api.ts)`);
return;
}
metaInfos.push(Object.assign(Object.assign({}, extConfig), { tsPath: apiPath }));
});
}
else {
fast_glob_1.default.sync('src/**/api.ts', { cwd: rootPath, absolute: true }).forEach((apiPath) => {
const arr = (0, vite_1.normalizePath)(apiPath).split('/');
const basename = arr[arr.length - 2];
metaInfos.push({
name: (0, lodash_1.upperFirst)((0, lodash_1.camelCase)(basename)),
tsPath: apiPath,
});
});
}
const clearIndexs = [];
if (parseAPI) {
for (let i = 0; i < metaInfos.length; i += 1) {
const { tsPath } = metaInfos[i];
try {
const componentMeta = yield (0, shared_1.getComponentMetaByApiTs)(tsPath);
if (componentMeta) {
metaInfos[i] = Object.assign(Object.assign({}, metaInfos[i]), componentMeta);
}
else {
clearIndexs.push(i);
}
}
catch (e) {
logger_1.default.error(`解析组件 ${tsPath} 失败`);
clearIndexs.push(i);
}
}
}
return metaInfos.filter((_, index) => !clearIndexs.includes(index));
});
}