generator-homework
Version:
CS 3300 Homework Template Generator
140 lines (122 loc) • 3.16 kB
JavaScript
;
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');
var fs = require('fs');
var path = require('path');
var inquirer = yeoman.inquirer;
function readHomeworkConfig(sourceRoot) {
var DIRNAME = sourceRoot + '/config';
var result = [];
var fileNames = fs.readdirSync(DIRNAME);
fileNames.forEach(function (fileName) {
if (fileName[0] !== '.' && fileName.substring(fileName.length - 4) === 'json') {
var content = fs.readFileSync(DIRNAME + '/' + fileName);
result.push(JSON.parse(content));
}
});
return result.sort(function (a, b) {
return a.id - b.id;
});
}
function getListChoices(input) {
return input.map(function (item) {
return {
name: item.name,
value: item.id
};
});
}
function getDetails(input, id) {
return input.filter(function (item) {
return item.id === id;
})[0];
};
module.exports = yeoman.generators.Base.extend({
prompting: function prompting() {
var _this = this;
var done = this.async();
var homeworks = readHomeworkConfig(this.sourceRoot());
this.log(yosay('Not all homeworks were created equal. Some homeworks are more equal than others!'));
var prompts = [{
type: 'input',
name: 'studentName',
message: 'Student Name',
'default': 'Danerys Targayen',
store: true
}, {
type: 'input',
name: 'studentNetId',
message: 'Student NetID',
'default': 'dt2041',
store: true
}, {
type: 'list',
name: 'homeworkId',
message: 'Homework #',
choices: getListChoices(homeworks),
'default': 0,
store: false
}];
this.prompt(prompts, function (stdin) {
_this.student = {
name: stdin.studentName,
netId: stdin.studentNetId
};
_this.homework = getDetails(homeworks, stdin.homeworkId);
done();
});
},
writing: function writing() {
var _this2 = this;
var filesToCopy = [{
src: 'index.html',
dest: 'index.html',
template: true
}, {
src: 'eslintrc',
dest: '.eslintrc',
template: false
}, {
src: 'gitattributes',
dest: '.gitattributes',
template: false
}, {
src: 'editorconfig',
dest: '.editorconfig',
template: false
}, {
src: 'jshintrc',
dest: '.jshintrc',
template: false
}, {
src: 'csscomb.json',
dest: '.csscomb.json',
template: false
}, {
src: 'gitignore',
dest: '.gitignore',
template: false
}, {
src: 'jscsrc',
dest: '.jscsrc',
template: false
}, {
src: 'css',
dest: 'css',
template: false
}, {
src: 'js',
dest: 'js',
template: false
}];
filesToCopy.forEach(function (item) {
if (item.template) {
_this2.fs.copyTpl(_this2.sourceRoot() + '/' + item.src, _this2.destinationPath() + '/' + item.dest, _this2);
} else {
_this2.fs.copy(_this2.sourceRoot() + '/' + item.src, _this2.destinationPath() + '/' + item.dest);
}
});
}
});
//# sourceMappingURL=index.js.map