generator-pr
Version:
Tool for Planning and Retrospect
88 lines (71 loc) • 2.42 kB
JavaScript
'use strict'
var yeoman = require('yeoman-generator')
var chalk = require('chalk')
var yosay = require('yosay')
var path = require('path')
var _ = require('lodash')
var moment = require('moment')
var getPlanningMonthPath = require('../../common/util').getPlanningMonthPath
module.exports = yeoman.Base.extend({
constructor: function () {
yeoman.Base.apply(this, arguments)
this.option('month', { type: Number, required: false })
this.option('items', { type: String, required: false })
this.items = this.options.items
var month = this.options.month
if (!month || month < 1 || month > 12) {
// Default to plan for next month
this.time = moment().add(1, 'months')
} else {
// Set the months parsed in
this.time = moment().set('month', this.options.month - 1)
}
},
prompting: function () {
var prompts = []
if (this.items) {
prompts.push({
type: 'input',
name: 'saveItems',
message: 'Do you want to save the provided items for default monthly items?',
default: false
})
}
return this.prompt(prompts).then(function (props) {
this.props = props
}.bind(this))
},
config: function() {
if (this.props.saveItems === 'true') {
this.config.set('defaultMonthlyItems', this.items)
}
},
writing: function () {
var monthlyTemplate = require(require.resolve(this.templatePath('month.js')))
var goals = []
monthlyTemplate.month = this.time.month() + 1
var items = this.items || this.config.get('defaultMonthlyItems')
if (items) {
items = items.split(';')
items.forEach(function(item) {
var type = item
var goals = ['']
var goalIdx = item.indexOf(':')
if (goalIdx > -1) {
type = item.substring(0, goalIdx)
goals = item.substring(goalIdx + 1).split(',')
}
goals = goals.map(function(goal) {
var monthlyItem = _.cloneDeep(monthlyTemplate.items[0])
monthlyItem.type = type
monthlyItem.contributeToGoal = goal
return monthlyItem
})
monthlyTemplate.items = monthlyTemplate.items.concat(goals)
})
monthlyTemplate.items.splice(0, 1)
}
var dest = getPlanningMonthPath(this.time)
this.fs.write(this.destinationPath(dest), 'module.exports = ' + JSON.stringify(monthlyTemplate, null, 2))
}
})