ant-design/scripts/utils.js

80 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-02-29 14:27:11 +08:00
'use strict';
const fs = require('fs');
const path = require('path');
const R = require('ramda');
const isMDFile = R.compose(R.equals('.md'), path.extname);
2016-03-03 11:12:46 +08:00
exports.findMDFile = function findMDFile(dirPath, shallow) {
2016-02-29 14:27:11 +08:00
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()) {
2016-03-03 11:12:46 +08:00
const indexFile = path.join(filePath, 'index.md');
if (shallow && fs.statSync(indexFile).isFile()) {
mds.push(indexFile);
} else {
mds = mds.concat(findMDFile(filePath));
}
2016-02-29 14:27:11 +08:00
}
}, 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']),
2016-02-29 17:43:22 +08:00
R.map((child) => R.split(/:\s?/, child.children)),
2016-02-29 14:27:11 +08:00
R.fromPairs
);
const parseEnglishTitle = R.path(['0', 'children']);
exports.parseMeta = function parseMeta(fileContent) {
const meta = parseBasicMeta(fileContent);
meta.english = parseEnglishTitle(fileContent);
return meta;
};
exports.stringify = function stringify(data, d) {
const depth = d || 1;
const indent = ' '.repeat(depth);
let output = '';
if (Array.isArray(data)) {
output += '[\n';
data.forEach((item) => output += indent + stringify(item, depth + 1) + ',\n');
output += indent + ']';
} else if (data === null) {
return 'null';
} else if (typeof data === 'object') {
output += '{\n';
for (const key of Object.keys(data)) {
output += indent + JSON.stringify(key) + ': ' + stringify(data[key], depth + 1) + ',\n';
}
output += indent + '}';
} else if (typeof data === 'function') {
output += data.toString();
} else if (typeof data === 'string') {
output += JSON.stringify(data);
} else {
output += data;
}
return output
.replace(/var _antd = require\(['"]antd['"]\);/, '')
.replace(/require\('antd\/lib/, 'require(\'../../components'); // TODO
};