bixi
Version:
企业级中后台前端解决方案
52 lines (48 loc) • 1.82 kB
text/typescript
import * as os from 'os';
import * as path from 'path';
import { ModuleDirConfig, SiteConfig } from '../interfaces';
const klawSync = require('klaw-sync');
const USERNAME_FLAG = '_USERNAME_FLAG_';
export function groupFiles(
srcPaths: string[],
config: ModuleDirConfig,
isSyncSpecific: boolean,
target: string,
siteConfig: SiteConfig,
) {
const files: { key: string; data: { [key: string]: string } }[] = [];
const langRe = new RegExp(`.(${siteConfig.langs.join('|')}){1}`, 'i');
srcPaths.forEach(srcPath => {
klawSync(srcPath, {
nodir: false,
filter: item => {
if (config.hasSubDir && item.stats.isDirectory()) return true;
return (
path.extname(item.path) === '.md' && item.stats.size > 1 && !item.path.includes(`${path.sep}demo${path.sep}`)
);
},
})
.filter(item => path.extname(item.path) === '.md')
.forEach(item => {
// 避免用户名中存在 ‘.’ 的情况
const username = os.userInfo().username;
const cleanPath = item.path.replace(username, USERNAME_FLAG);
const relativePath = cleanPath.split('.')[0].replace(USERNAME_FLAG, username);;
const key = path.relative(srcPath, config.hasSubDir ? path.dirname(item.path) : relativePath).trim();
if (key.length === 0) return;
if (isSyncSpecific && key !== target) return;
if (config.ignores && ~config.ignores.indexOf(key)) return;
let sourceItem = files.find(w => w.key === key);
if (!sourceItem) {
sourceItem = {
key,
data: {},
};
files.push(sourceItem);
}
const langMatch = item.path.match(langRe);
sourceItem.data[langMatch ? langMatch[1] : siteConfig.defaultLang] = item.path;
});
});
return files;
}