@lcap/builder
Version:
lcap builder utils
146 lines (145 loc) • 6 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.updatePackageInfo = exports.updateLcapConfg = exports.getLcapConfig = exports.getSourceSchema = exports.getComponentList = exports.getExtensionProjectMeta = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
const YAML = __importStar(require("yaml"));
const constants_1 = require("../overload/constants");
const LCAP_UI = 'lcap-ui';
function getFrameWorkKind(pkgInfo, rootPath) {
if (!pkgInfo) {
pkgInfo = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, 'package.json'));
}
if (pkgInfo.peerDependencies && Object.keys(pkgInfo.peerDependencies).includes('@tarojs/taro')) {
return 'taro';
}
if (pkgInfo.peerDependencies && Object.keys(pkgInfo.peerDependencies).includes('react')) {
return 'react';
}
if (pkgInfo.peerDependencies
&& pkgInfo.peerDependencies.vue
&& (pkgInfo.peerDependencies.vue.startsWith('3.') || pkgInfo.peerDependencies.vue.startsWith('^3.'))) {
return 'vue3';
}
if (pkgInfo.peerDependencies
&& pkgInfo.peerDependencies.vue
&& (pkgInfo.peerDependencies.vue.startsWith('2.') || pkgInfo.peerDependencies.vue.startsWith('^2.'))) {
return 'vue2';
}
return '';
}
function getLcapUIInfo(pkgInfo, rootPath = process.cwd()) {
if (!pkgInfo) {
pkgInfo = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, 'package.json'));
}
if (!pkgInfo || !pkgInfo.lcap || !pkgInfo.lcap[LCAP_UI]) {
return null;
}
return pkgInfo.lcap[LCAP_UI];
}
function getExtensionProjectMeta(rootPath) {
const pkgInfo = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, 'package.json'));
const framework = getFrameWorkKind(pkgInfo, rootPath);
return {
framework,
name: pkgInfo.name,
version: pkgInfo.version,
title: pkgInfo.title,
description: pkgInfo.description,
libUIInfo: getLcapUIInfo(pkgInfo, rootPath),
};
}
exports.getExtensionProjectMeta = getExtensionProjectMeta;
function getComponentList(rootPath, pkgInfo) {
if (!pkgInfo) {
pkgInfo = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, 'package.json'));
}
if (!pkgInfo || !pkgInfo.lcap || !pkgInfo.lcap[LCAP_UI]) {
return [];
}
const moduleConfigPath = path_1.default.resolve(rootPath, constants_1.LCAP_UI_JSON_PATH);
if (!fs_extra_1.default.existsSync(moduleConfigPath)) {
return [];
}
try {
const naslConfigPath = path_1.default.resolve(rootPath, constants_1.LCAP_UI_CONFIG_PATH);
if (!fs_extra_1.default.existsSync(naslConfigPath)) {
return [];
}
const arr = fs_extra_1.default.readJSONSync(naslConfigPath);
return Array.isArray(arr) ? arr.filter((c) => c.show !== false).sort((a, b) => a.name.localeCompare(b.name)) : [];
}
catch (e) {
return [];
}
}
exports.getComponentList = getComponentList;
function getSourceSchema(rootPath) {
const pkgInfo = fs_extra_1.default.readJSONSync(path_1.default.resolve(rootPath, 'package.json'));
const schemaFilePath = pkgInfo.lcap && pkgInfo.lcap.schema ? path_1.default.resolve(rootPath, pkgInfo.lcap.schema) : '';
if (!schemaFilePath || !fs_extra_1.default.existsSync(schemaFilePath)) {
return null;
}
const schema = fs_extra_1.default.readJSONSync(schemaFilePath);
return schema;
}
exports.getSourceSchema = getSourceSchema;
function getLcapConfig() {
const lcapConfigPath = path_1.default.resolve(os_1.default.homedir(), '.lcaprc');
if (!fs_extra_1.default.existsSync(lcapConfigPath)) {
return null;
}
const yaml = fs_extra_1.default.readFileSync(lcapConfigPath, 'utf-8');
const config = YAML.parse(yaml);
return config;
}
exports.getLcapConfig = getLcapConfig;
function updateLcapConfg(config) {
const lcapConfig = getLcapConfig();
Object.keys(config).forEach((key) => {
if (lcapConfig[key]) {
lcapConfig[key] = config[key];
}
});
const yaml = YAML.stringify(lcapConfig);
fs_extra_1.default.writeFileSync(path_1.default.resolve(os_1.default.homedir(), '.lcaprc'), yaml);
}
exports.updateLcapConfg = updateLcapConfg;
function updatePackageInfo(rootPath, pkgInfo) {
const pkgPath = path_1.default.resolve(rootPath, 'package.json');
const pkg = fs_extra_1.default.readJSONSync(pkgPath);
Object.keys(pkgInfo).forEach((key) => {
if (pkg[key]) {
pkg[key] = pkgInfo[key];
}
});
fs_extra_1.default.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2));
}
exports.updatePackageInfo = updatePackageInfo;