UNPKG

@wgoo/cli

Version:

Wgoo Cli 是一个 React 组件库构建工具,通过 Wgoo Cli 可以快速搭建一套功能完备的 React 组件库。

127 lines (105 loc) 2.92 kB
const glob = require('fast-glob'); const { join, parse } = require('path'); const { existsSync, readdirSync } = require('fs-extra'); const { pascalize, removeExt, getWgooConfig, smartOutputFile, normalizePath, } = require('../common'); const { SRC_DIR, DOCS_DIR, getPackageJson, WGOO_CONFIG_FILE, SITE_DESKTOP_SHARED_FILE, } = require('../common/constant'); function formatName(component, lang) { component = pascalize(component); if (lang) { return `${component}_${lang.replace('-', '_')}`; } return component; } /** * i18n mode: * - action-sheet/README.md => ActionSheet_EnUS * - action-sheet/README.zh-CN.md => ActionSheet_ZhCN * * default mode: * - action-sheet/README.md => ActionSheet */ function resolveDocuments(components) { const wgooConfig = getWgooConfig(); const { locales, defaultLang } = wgooConfig.site; const docs = []; if (locales) { const langs = Object.keys(locales); langs.forEach((lang) => { const fileName = lang === defaultLang ? 'README.md' : `README.${lang}.md`; components.forEach((component) => { docs.push({ name: formatName(component, lang), path: join(SRC_DIR, component, fileName), }); }); }); } else { components.forEach((component) => { docs.push({ name: formatName(component), path: join(SRC_DIR, component, 'README.md'), }); }); } const staticDocs = glob .sync(normalizePath(join(DOCS_DIR, '**/*.md'))) .map((path) => { const pairs = parse(path).name.split('.'); return { name: formatName(pairs[0], pairs[1] || defaultLang), path, }; }); return [...staticDocs, ...docs.filter((item) => existsSync(item.path))]; } function genImportDocuments(items) { return items .map((item) => `import ${item.name} from '${normalizePath(item.path)}';`) .join('\n'); } function genExportDocuments(items) { return `export const documents = { ${items.map((item) => item.name).join(',\n ')} };`; } function genImportConfig() { return `import config from '${removeExt(normalizePath(WGOO_CONFIG_FILE))}';`; } function genExportConfig() { return 'export { config };'; } function genExportVersion() { return `export const packageVersion = '${getPackageJson().version}';`; } function genInstall() { return `import './package-style';`; } function genExportPackageEntry() { return `export { default as packageEntry } from './package-entry';`; } function genSiteDesktopShared() { const dirs = readdirSync(SRC_DIR); const documents = resolveDocuments(dirs); const code = `${genImportConfig()} ${genInstall()} ${genImportDocuments(documents)} ${genExportPackageEntry()} ${genExportConfig()} ${genExportDocuments(documents)} ${genExportVersion()} `; smartOutputFile(SITE_DESKTOP_SHARED_FILE, code); } module.exports = { genSiteDesktopShared };