@lcap/builder
Version:
lcap builder utils
171 lines (170 loc) • 7.91 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProjectSourceSchema = exports.removeComponentFiles = exports.getComponentMetaInfos = exports.getComponentConfigByName = exports.getPackName = exports.getConfigComponents = void 0;
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 parser = __importStar(require("@babel/parser"));
const traverse_1 = __importDefault(require("@babel/traverse"));
const generator_1 = __importDefault(require("@babel/generator"));
const component_path_1 = require("./component-path");
const logger_1 = __importDefault(require("./logger"));
const babel_utils_1 = require("./babel-utils");
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, 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,
});
});
}
if (parseAPI) {
return metaInfos.map(({ tsPath, show }) => {
try {
return Object.assign({ show }, (0, babel_utils_1.getComponentMetaByApiTs)(tsPath));
}
catch (e) {
logger_1.default.error(`解析组件 ${tsPath} 失败`);
return null;
}
}).filter((v) => !!v);
}
return metaInfos;
}
exports.getComponentMetaInfos = getComponentMetaInfos;
function removeComponentFiles(rootPath, name) {
const components = getComponentMetaInfos(rootPath, true);
const compMeta = components.find((c) => c.name === name);
if (!compMeta) {
throw new Error(`未找到组件 ${name}`);
}
const componentFolder = path_1.default.dirname(compMeta.tsPath);
const componentFolderName = path_1.default.basename(componentFolder);
const exportsFilePath = ['index.ts', 'index.js'].map((fileName) => path_1.default.resolve(componentFolder, '../', fileName)).filter((p) => fs_extra_1.default.existsSync(p))[0];
const code = fs_extra_1.default.readFileSync(exportsFilePath, 'utf-8').toString();
const source = `./${componentFolderName}`;
const ast = parser.parse(code, {
sourceType: 'module',
plugins: ['typescript', 'jsx'],
});
const removeNames = [name];
(0, traverse_1.default)(ast, {
ExportNamedDeclaration(path) {
if (path.node.source && path.node.source.value === source) {
path.remove();
}
},
ImportDeclaration(path) {
if (path.node.source && path.node.source.value === source) {
path.traverse({
ImportSpecifier(p) {
if (p.node.local.name === name) {
removeNames.push(p.node.local.name);
}
},
});
path.remove();
}
},
ExportAllDeclaration(path) {
if (path.node.source && path.node.source.value === source) {
path.remove();
}
},
ExportSpecifier(path) {
if (removeNames.includes(path.node.local.name)) {
path.remove();
}
},
});
fs_extra_1.default.rmSync(componentFolder, { recursive: true });
fs_extra_1.default.writeFileSync(exportsFilePath, (0, generator_1.default)(ast).code, 'utf-8');
}
exports.removeComponentFiles = removeComponentFiles;
function getProjectSourceSchema(rootPath = process.cwd()) {
var _a, _b;
const pkgInfo = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, 'package.json'));
if (!((_a = pkgInfo.lcap) === null || _a === void 0 ? void 0 : _a.schema)) {
throw new Error('未找到本地NPM扫描结果文件');
}
const schema = (_b = pkgInfo.lcap) === null || _b === void 0 ? void 0 : _b.schema;
if (!schema || !fs_extra_1.default.existsSync(path_1.default.resolve(rootPath, schema))) {
throw new Error(`schema 文件 ${schema} 不存在`);
}
const material = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, schema), 'utf-8');
if (!material.components || material.components.length === 0) {
throw new Error(`schema 文件 ${schema} 中没有组件`);
}
return material;
}
exports.getProjectSourceSchema = getProjectSourceSchema;