feat(cli): support locales

This commit is contained in:
陈嘉涵
2019-12-06 15:51:20 +08:00
parent 6bd858fac5
commit dc6cc6c5af
16 changed files with 336 additions and 135 deletions

View File

@@ -4,6 +4,7 @@ import { existsSync } from 'fs-extra';
import {
pascalize,
removeExt,
getVantConfig,
getComponents,
smartOutputFile
} from '../common';
@@ -19,23 +20,59 @@ type DocumentItem = {
path: string;
};
function formatName(component: string, lang?: string) {
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: string[]): DocumentItem[] {
const componentDocs = components
.filter(component => {
const absolutePath = join(SRC_DIR, component, 'README.md');
return existsSync(absolutePath);
})
.map(component => ({
name: pascalize(component),
path: join(SRC_DIR, component, 'README.md')
}));
const vantConfig = getVantConfig();
const { locales, defaultLang } = vantConfig.site;
const staticDocs = glob.sync(join(DOCS_DIR, '**/*.md')).map(path => ({
name: pascalize(parse(path).name),
path
}));
const docs: DocumentItem[] = [];
return [...componentDocs, ...staticDocs];
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(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: DocumentItem[]) {