@lcap/builder
Version:
lcap builder utils
102 lines (101 loc) • 4.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = genThemeConfig;
/* eslint-disable newline-per-chained-call */
const fast_glob_1 = __importDefault(require("fast-glob"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const lodash_1 = require("lodash");
const parse_css_vars_1 = __importDefault(require("../../nasl/parse-css-vars"));
const parse_css_vars_old_1 = __importDefault(require("../../nasl/parse-css-vars-old"));
function getCssContent(options) {
const cssVarFiles = [];
if (options.themeVarCssPath && fs_extra_1.default.existsSync(options.themeVarCssPath)) {
cssVarFiles.push(options.themeVarCssPath);
}
const varsPath = options.findThemeType === 'component' ? '*/theme/vars.css' : '*/vars.css';
const varFiles = fast_glob_1.default.sync(varsPath, { cwd: options.themeComponentFolder, absolute: true });
if (varFiles.length > 0) {
cssVarFiles.push(...varFiles);
}
if (options.dependencies && options.dependencies.length > 0) {
options.dependencies.forEach(({ rootPath }) => {
const depVarFiles = fast_glob_1.default.sync('*/vars.css', { cwd: path_1.default.resolve(rootPath, './src/theme/components'), absolute: true });
if (depVarFiles.length > 0) {
cssVarFiles.push(...depVarFiles);
}
});
}
const cssContents = [];
cssVarFiles.forEach((filePath) => {
cssContents.push(fs_extra_1.default.readFileSync(filePath, { encoding: 'utf-8' }).toString());
});
return cssContents.join('\n\n');
}
function genThemeConfig(options, framework) {
const themeConfig = {
defaultTheme: {},
previewPages: options.previewPages,
components: [],
global: {
selector: '',
variables: [],
},
};
let themeInfo = null;
if (options.oldCssVarPath && fs_extra_1.default.existsSync(options.oldCssVarPath)) {
themeInfo = (0, parse_css_vars_old_1.default)(fs_extra_1.default.readFileSync(options.oldCssVarPath, 'utf-8').toString(), options.themeComponentFolder);
}
const cssContent = getCssContent(options);
if (cssContent) {
const { global, components } = (0, parse_css_vars_1.default)(cssContent);
if (!themeInfo) {
themeInfo = { global, components };
}
else {
themeInfo.global.variables = [].concat(themeInfo.global.variables).concat(global.variables);
components.forEach((comp) => {
const it = themeInfo === null || themeInfo === void 0 ? void 0 : themeInfo.components.find((itComp) => itComp.name === comp.name);
if (!it) {
themeInfo === null || themeInfo === void 0 ? void 0 : themeInfo.components.push(comp);
}
});
}
}
if (!themeInfo) {
return themeConfig;
}
themeConfig.components = options.components.map(({ group, title, name, children, show, }) => {
const compTheme = themeInfo.components.find((c) => (c.name === name || ((0, lodash_1.kebabCase)(c.name) === (0, lodash_1.kebabCase)(name)
&& framework && framework.startsWith('vue'))));
if (!compTheme || compTheme.hidden) {
return {
name,
title,
group,
useGlobalTokens: [],
selector: '',
variables: [],
};
}
compTheme.variables.forEach(({ name: key, value }) => {
themeConfig.defaultTheme[key] = value;
});
return Object.assign(Object.assign({}, compTheme), { name,
title,
group, children: children || [], hidden: show === false });
}).filter((comp) => {
return comp.useGlobalTokens.length > 0 || comp.variables.length > 0;
}).map((comp) => {
return Object.assign(Object.assign({}, comp), { variables: comp.variables.filter((cssVar) => !cssVar.hidden) });
});
themeConfig.global.selector = themeInfo.global.selector;
themeConfig.global.variables = themeInfo.global.variables.filter((cssVar) => {
themeConfig.defaultTheme[cssVar.name] = cssVar.value;
return !cssVar.hidden;
});
return themeConfig;
}