generator-pr
Version:
Tool for Planning and Retrospect
76 lines (61 loc) • 2.25 kB
JavaScript
'use strict'
var yeoman = require('yeoman-generator')
var chalk = require('chalk')
var yosay = require('yosay')
var _ = require('lodash')
var moment = require('moment')
var getPlanningDir = require('../../common/util').getPlanningDir
var getPlanningMonthPath = require('../../common/util').getPlanningMonthPath
var VALID_WEEKS = ['W1', 'W2', 'W3', 'W4']
module.exports = yeoman.Base.extend({
constructor: function () {
yeoman.Base.apply(this, arguments)
this.argument('week', { type: String, required: true })
if (VALID_WEEKS.indexOf(this.week) < 0) {
this.log('Valid week value choices are: ' + VALID_WEEKS)
process.exit(-1)
}
this.option('month', { type: Number, required: false })
var month = this.options.month
if (!month || month < 1 || month > 12) {
// Default to plan for this month
this.time = moment()
} else {
// Set the months parsed in
this.time = moment().set('month', this.options.month - 1)
}
},
writing: function () {
var monthlyTemplate = null
try {
var monthDataPath = getPlanningMonthPath(this.time)
monthlyTemplate = require(require.resolve(this.destinationPath(monthDataPath)))
} catch (e) {
// No planned month data
}
var dest = getPlanningDir(this.time) + this.week + '.js'
var weekNo = this.week.charAt(1) - 1
if (monthlyTemplate) {
var weeklyTemplate = require(require.resolve(this.templatePath('week.js')))
weeklyTemplate.week = this.week
monthlyTemplate.items.forEach(function(item) {
var weeklyItem = _.clone(weeklyTemplate.items[0])
weeklyItem.weeklyProgressPlan = item.weeklyProgressPlan[weekNo] || ''
weeklyItem.type = item.type
weeklyItem.contributeToGoal = item.contributeToGoal
weeklyItem.contributeToValue = item.contributeToValue
weeklyTemplate.items.push(weeklyItem)
})
weeklyTemplate.items.splice(0, 1)
this.fs.write(this.destinationPath(dest), 'module.exports = ' + JSON.stringify(weeklyTemplate, null, 2))
} else {
this.fs.copyTpl(
this.templatePath('week.js'),
this.destinationPath(dest),
{
week: this.week
}
)
}
}
})