generator-npmsb
Version:
Generator for yeoman for npm scripts build workflow with coffeescript, karma, svg symbols and stuff
100 lines (84 loc) • 2.64 kB
JavaScript
var generators = require('yeoman-generator');
var packageJSON = require('./templates/_package.json');
var coffeeJSON = require('./templates/_coffee.json');
var jsJSON = require('./templates/_js.json');
var karmaJSON = require('./templates/_karma.json');
var svgJSON = require('./templates/_svg.json');
var deepExtend = require('deep-extend');
var fs = require('fs');
module.exports = generators.Base.extend({
constructor: function () {
generators.Base.apply(this, arguments);
this.option('script');
this.option('svg');
this.option('karma');
this.option('es6');
this.additionalWatchers = [];
this.additionalBuilders = [];
},
buildPackageJSON: function () {
if (this.options['es6']) {
deepExtend(packageJSON, jsJSON);
} else {
deepExtend(packageJSON, coffeeJSON);
}
if (this.options['svg']) {
deepExtend(packageJSON, svgJSON);
this.additionalBuilders.push(" && npm run build:svgs");
this.additionalWatchers.push(" watch:svgs");
}
if (this.options['karma']) {
deepExtend(packageJSON, karmaJSON);
}
this.additionalBuilders.forEach((builder) => {
packageJSON.scripts["build:all"] = packageJSON.scripts["build:all"] + builder;
});
this.additionalWatchers.forEach((watcher) => {
packageJSON.scripts["watch:all"] = packageJSON.scripts["watch:all"] + watcher;
});
},
copyScripts: function (ext) {
this.fs.copyTpl(
this.templatePath('app/javascripts/*.' + ext),
this.destinationPath('app/javascripts')
);
this.fs.copyTpl(
this.templatePath('spec/*.' + ext),
this.destinationPath('spec')
);
},
copyFiles: function () {
this.fs.writeJSON(this.destinationPath('package.json'), packageJSON);
if (this.options['karma']) {
this.fs.copyTpl(
this.templatePath(this.options['es6'] ? 'karmaconfjs' : 'karmaconfcoffee'),
this.destinationPath('karma.conf.js')
);
}
this.fs.copyTpl(
this.templatePath('gitignore'),
this.destinationPath('.gitignore')
);
this.directory('app/images', 'app/images');
if (this.options['svg']) {
this.directory('app/svgs', 'app/svgs');
}
this.directory('app/stylesheets', 'app/stylesheets');
if (this.options['es6']) {
this.copyScripts('js');
} else {
this.copyScripts('coffee');
}
this.fs.copyTpl(
this.templatePath('app/index.html'),
this.destinationPath('app/index.html')
);
},
writing: function () {
this.buildPackageJSON();
this.copyFiles();
},
install: function () {
this.npmInstall();
}
});