akb-cli
Version:
akb cli
48 lines (43 loc) • 1.5 kB
JavaScript
/**
* @file 使用babel编译文件
* @author panyuqi(panyuqi@baidu.com)
*/
;
const babel = require('babel-core');
const fs = require('./fs-extra-as-promised');
const path = require('path');
const chalk = require('chalk');
const Promise = require('bluebird');
/* eslint-disable */
const glob = Promise.promisify(require('glob'));
const transformFile = Promise.promisify(babel.transformFile);
/* eslint-enable */
const log = console.log;
const builder = module.exports;
builder.start = argv => {
let destDir = path.join(process.cwd(), argv.destDir);
let tmpDir = path.resolve(process.cwd(), '../tmp-' + (new Date().getTime()));
let ignorePattern = [
path.resolve(destDir, './node_modules/**/*'),
path.resolve(destDir, './public/**/*')
]; // 无视node_modules和public
log(chalk.green('Start to build.'));
fs.removeAsync(destDir)
.then(() => fs.copyAsync(process.cwd(), tmpDir))
.then(() => fs.moveAsync(tmpDir, destDir))
.then(() => glob(path.resolve(destDir, './**/*.js'), {
nodir: true,
ignore: ignorePattern
}))
.then(jsFiles => Promise.all(jsFiles.map(
file => transformFile(file, {
plugins: ['transform-async-to-generator']
})
.then(result => fs.writeFileAsync(file, result.code, {encoding: 'utf-8'}))
.then(() => log(' ', chalk.green('create'), file))
))
)
.catch(err => {
log(chalk.red('Failed.', err));
});
};