tbom
Version:
Taobao Open Platform Modules
55 lines (47 loc) • 1.31 kB
JavaScript
;
const fs = require('fs');
const path = require('path');
const Utils = {
getDirs: function() {
const base = path.join(__dirname, '../src');
const dirs = [];
fs.readdirSync(base).forEach(function(file) {
if (fs.statSync(path.join(base, file)).isDirectory() && file !== 'common') {
return dirs.push(file);
}
});
return dirs;
},
getEntry: function() {
const dirs = Utils.getDirs();
const ret = {};
dirs.forEach(function(dir) {
ret[dir] = path.join(__dirname, `../src/${dir}/index.js`);
});
return ret;
},
buildIndex: function() {
const dirs = Utils.getDirs();
let indexString = '';
dirs.forEach(function(dir) {
indexString += `var ${dir} = require('./${dir}');\n`;
});
const kv = dirs.map(function(dir, index) {
if (index === dirs.length - 1) {
return ` ${dir}: ${dir}`;
}
return ` ${dir}: ${dir},`;
});
indexString += `\nmodule.exports = {\n${kv.join('\n')}\n};`;
fs.writeFileSync(path.join(__dirname, '../index.js'), indexString);
},
clear: function() {
const dirs = Utils.getDirs();
dirs.forEach(function(dir) {
try {
fs.unlink(path.join(__dirname, `../${dir}`));
} catch(e) {}
})
}
};
module.exports = Utils;