-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexander Alemayhu <[email protected]>
- Loading branch information
Showing
1 changed file
with
44 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |