UNPKG

generator-tbc

Version:
175 lines (132 loc) 4.23 kB
'use strict'; var util = require('util'); var path = require('path'); var ABC = require('abc-generator'); var exec = require('child_process').exec; // 本地配置 var userConfig = {}; module.exports = AppGenerator; function AppGenerator(args, options, config) { ABC.UIBase.apply(this, arguments); try { this.abcJSON = require(path.resolve('abc.json')); } catch (e) { this.isFirstTime = true; this.abcJSON = {}; } this.pkg = JSON.parse(this.readFileAsString(path.join(__dirname, '../package.json'))); this.on('end', function () { this.log('done!'); }.bind(this)); } util.inherits(AppGenerator, ABC.UIBase); AppGenerator.prototype.getConfig = function getConfig() { var done = this.async(); // 获取当前分支 exec('git config --list', function(err, stdout, stderr, cb) { var reg = /user\.name=([^\n]+)\nuser\.email=([^\n]+)/, match = stdout.match(reg); if (match) { userConfig.author = { name: match[1], email: match[2] }; } done(); }); }; AppGenerator.prototype.askFor = function askFor() { var cb = this.async(); // welcome message this.log(this.abcLogo); this.log( '开始为您创建 tbc 组件:' + path.basename(process.cwd()) + '([ ] 中的值为可选项,( ) 中的值为默认值)' ); var abcJSON = this.abcJSON || {}; var author = userConfig.author || abcJSON.author || {}; var styleEngine = abcJSON._tbc ? abcJSON._tbc.styleEngine : 'css-combo'; var prompts = [ { name: 'author', message: 'Author Name', default: author.name, warning: '' }, { name: 'email', message: 'Author Email', default: author.email, warning: '' }, { name: 'styleEngine', message: '请选择使用的 CSS 编码语言:[css-combo|less|sass]', default: styleEngine, warning: '' } ]; this.prompt(prompts, function (props) { // manually deal with the response, get back and store the results. // we change a bit this way of doing to automatically do this in the self.prompt() method. this.projectName = path.basename(process.cwd()); this.author = props.author; this.email = props.email; this.styleEngine = props.styleEngine || 'css-combo'; this.enableLess = (/less/i).test(this.styleEngine); this.enableSass = (/sass/i).test(this.styleEngine); this.enableCSSCombo = (/css-combo/i).test(this.styleEngine); cb(); }.bind(this)); }; AppGenerator.prototype.gruntfile = function gruntfile() { this.template('Gruntfile.js'); }; AppGenerator.prototype.packageJSON = function packageJSON() { this.template('_package.json', 'package.json'); }; AppGenerator.prototype.git = function git() { this.copy('gitignore', '.gitignore'); //this.copy('gitattributes', '.gitattributes'); }; /* AppGenerator.prototype.jshint = function jshint() { this.copy('jshintrc', '.jshintrc'); }; AppGenerator.prototype.editorConfig = function editorConfig() { this.copy('editorconfig', '.editorconfig'); }; */ AppGenerator.prototype._createCss = function createCss(name) { if (name) { if (this.enableLess) { this.template('index.css', name + '.less'); } else if (this.enableSass) { this.template('index.css', name + '.scss'); } else { this.template('index.css', name + '.css'); } } }; AppGenerator.prototype.app = function app() { this.mkdir('mods'); this.mkdir('images'); this.mkdir('plugin'); this.mkdir('build'); this.template('abc.json'); if(this.isFirstTime) { // 如果之前有,就不再重建 this.copy('README.md', 'README.md'); this.template('index.js'); this.template('index.html'); this._createCss('index'); } //this.template('package-config.js', 'common/package-config.js'); }; AppGenerator.prototype.install = function install() { var cb = this.async(); this.npmInstall('', {}, function (err) { if (err) { return console.log('error', err); } console.log('\n\nnpm was installed successful. \n\n'); cb() }); };