From de698035d771d46cff71e517d29d4d6e53f079cc Mon Sep 17 00:00:00 2001 From: Alexander Alemayhu Date: Sat, 5 Jan 2019 23:21:43 +0100 Subject: [PATCH] add the dates and filtering options Signed-off-by: Alexander Alemayhu --- src/index.js | 59 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index 05f9054..ec1c54f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,24 +1,53 @@ -const {Command, flags} = require('@oclif/command') +const { Command, flags } = require("@oclif/command"); + +// https://stackoverflow.com/questions/25588473/how-to-get-list-of-days-in-a-month-with-moment-js +const lastTuesdayIn = (year, month) => { + const names = Object.freeze(["s", "m", "Tuesday", "w", "t", "f", "s"]); + const date = new Date(year, month - 1, 1); + const tuesdays = []; + while (date.getMonth() == month - 1) { + if (names[date.getDay()] === "Tuesday") { + tuesdays.push(date.toLocaleDateString()); + } + date.setDate(date.getDate() + 1); + } + if (!tuesdays || tuesdays.length == 0) { + return undefined; + } + return tuesdays[tuesdays.length - 1]; +}; class JamstackOsloLastTuesdayInCommand extends Command { async run() { - const {flags} = this.parse(JamstackOsloLastTuesdayInCommand) - const name = flags.name || 'world' - this.log(`hello ${name} from ./src/index.js`) + const { flags } = this.parse(JamstackOsloLastTuesdayInCommand); + const today = new Date(); + const year = flags.year || today.getUTCFullYear(); + const month = flags.month || today.getUTCMonth() + 1; + const remainder = flags.remainder || false; + + if (remainder) { + for (let i = month; i <= 12; i++) { + this.log(lastTuesdayIn(year, i)); + } + } else { + this.log(lastTuesdayIn(year, month)); + } } } -JamstackOsloLastTuesdayInCommand.description = `Describe the command here -... -Extra documentation goes here -` +JamstackOsloLastTuesdayInCommand.description = `Print out the last tuesdays +Running without arguments will default ot the current year and month.`; JamstackOsloLastTuesdayInCommand.flags = { - // add --version flag to show CLI version - version: flags.version({char: 'v'}), - // add --help flag to show CLI version - help: flags.help({char: 'h'}), - name: flags.string({char: 'n', description: 'name to print'}), -} + version: flags.version({ char: "v" }), + help: flags.help({ char: "h" }), + name: flags.string({ char: "n", description: "name to print" }), + month: flags.integer({ char: "m", description: "month for filter" }), + year: flags.integer({ char: "y", description: "year for filter" }), + remainder: flags.boolean({ + char: "r", + description: "print all last remaining tuesdays" + }) +}; -module.exports = JamstackOsloLastTuesdayInCommand +module.exports = JamstackOsloLastTuesdayInCommand;