Skip to content

Commit

Permalink
add the dates and filtering options
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Alemayhu <[email protected]>
  • Loading branch information
aalemayhu committed Jan 5, 2019
1 parent f99d853 commit de69803
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit de69803

Please sign in to comment.