ant-design/scripts/utils.js
2016-03-10 10:50:38 +08:00

52 lines
1.3 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const R = require('ramda');
const isMDFile = R.compose(R.equals('.md'), path.extname);
exports.findMDFile = function findMDFile(dirPath, shallow) {
let mds = [];
R.forEach((fileName) => {
const filePath = path.join(dirPath, fileName);
const stat = fs.statSync(filePath);
if (stat.isFile() && isMDFile(filePath)) {
mds.push(filePath);
}
if (stat.isDirectory()) {
const indexFile = path.join(filePath, 'index.md');
if (shallow && fs.statSync(indexFile).isFile()) {
mds.push(indexFile);
} else {
mds = mds.concat(findMDFile(filePath));
}
}
}, fs.readdirSync(dirPath));
return mds;
};
exports.isIndex = R.compose(R.equals('index.md'), R.unary(path.basename));
exports.isDemo = R.complement(exports.isIndex);
const MT = require('mark-twain');
exports.parseFileContent = R.pipe(
fs.readFileSync,
R.toString,
MT,
R.prop('content')
);
const parseBasicMeta = R.pipe(
R.path(['1', 'children']),
R.map((child) => R.split(/:\s?/, child.children)),
R.fromPairs
);
const parseEnglishTitle = R.path(['0', 'children']);
exports.parseMeta = function parseMeta(fileContent) {
const meta = parseBasicMeta(fileContent);
meta.english = parseEnglishTitle(fileContent);
return meta;
};