ant-design/scripts/utils.js

64 lines
1.8 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-04 18:06:34 +08:00
exports.findMDFile = function findMDFile(fileName, shallow) {
const filePaths = Array.isArray(fileName) ? fileName : [fileName];
2016-02-29 14:27:11 +08:00
let mds = [];
2016-03-04 18:06:34 +08:00
R.forEach((filePath) => {
2016-02-29 14:27:11 +08:00
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');
2016-03-04 18:06:34 +08:00
let hasIndexFile = false;
try {
hasIndexFile = fs.statSync(indexFile).isFile();
} catch (e) {}
if (shallow && hasIndexFile) {
2016-03-03 11:12:46 +08:00
mds.push(indexFile);
} else {
2016-03-04 18:06:34 +08:00
const subFiles = fs.readdirSync(filePath)
.map((subFile) => path.join(filePath, subFile));
mds = mds.concat(findMDFile(subFiles, shallow));
2016-03-03 11:12:46 +08:00
}
2016-02-29 14:27:11 +08:00
}
2016-03-04 18:06:34 +08:00
}, filePaths);
2016-02-29 14:27:11 +08:00
return mds;
};
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
};