Skip to content

Commit

Permalink
Merge pull request #87 from kkeeth/add-M-option
Browse files Browse the repository at this point in the history
feat: -M option
  • Loading branch information
kkeeth authored Jul 17, 2024
2 parents 46d3732 + 6d9ce27 commit 58b17c7
Show file tree
Hide file tree
Showing 6 changed files with 657 additions and 312 deletions.
4 changes: 4 additions & 0 deletions lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default yargs(process.argv.slice(2))
describe: 'Set "from" 1 month',
type: "boolean",
},
M: {
alias: "specify-month",
describe: "Set the month to be counted (but only this year)",
},
w: {
alias: "week",
describe: 'Set "from" 1 week',
Expand Down
12 changes: 1 addition & 11 deletions lib/check-stats-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,9 @@ export default () => {
const stats = [];
let total = 0;

if (`${new Date(start)}` === "Invalid Date") {
showHelp(start);
return;
}
if (`${new Date(end)}` === "Invalid Date") {
showHelp(end);
return;
}

// start date > end date
if (new Date(start).getTime() > new Date(end).getTime()) {
showHelp("The start date is specified to be later than the end date. \n\n");
return;
showHelp("The start date is specified to be later than the end date. \n");
}

for (const mod of args._) {
Expand Down
169 changes: 150 additions & 19 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from "chalk";
import yargs from "yargs";
import {
format,
parseISO,
Expand All @@ -9,17 +8,23 @@ import {
addMonths,
addYears,
getYear,
startOfMonth,
endOfMonth,
} from "date-fns";

const FORMAT_DATE = "yyyy-MM-dd";

/**
* show help
*
* @param {String} help message if specifying a message
* @return {String} full help message
*/
const showHelp = (text) => {
if (text) console.log(chalk.yellow.bold(text));
yargs(process.argv.slice(2)).showHelp();
console.error(
`${chalk.yellow.bold(text)}run "csm --help" for usage information`,
);
process.exit(1);
};

/**
Expand All @@ -31,30 +36,33 @@ const showHelp = (text) => {
const getStartDate = (args) => {
// check module names
if (args._.length === 0) {
return "Please enter the module names at least one. \n";
showHelp("Please enter the module names at least one. \n");
}
// check start date options
if (args.s) {
if (!/\d{4}-\d{2}-\d{2}/.test(args.s)) {
return "Please enter the date correctly. \n";
showHelp("Please enter the date correctly. \n");
}
if (!isValid(parseISO(args.s))) {
return "Please enter the date correctly. \n";
showHelp("Please enter the date correctly. \n");
}
}
if (args.m) {
return format(addMonths(new Date(), -1), "yyyy-MM-dd");
return format(addMonths(new Date(), -1), FORMAT_DATE);
}
if (args.M) {
return getMonthBounds(args.M, "start");
}
if (args.y) {
return format(addYears(new Date(), -1), "yyyy-MM-dd");
return format(addYears(new Date(), -1), FORMAT_DATE);
}
if (args.t) {
return `${getYear(new Date())}-01-01`;
}
if (args.w) {
return format(addWeeks(new Date(), -1), "yyyy-MM-dd");
return format(addWeeks(new Date(), -1), FORMAT_DATE);
}
return args.s || format(addDays(new Date(), -1), "yyyy-MM-dd");
return args.s || format(addDays(new Date(), -1), FORMAT_DATE);
};

/**
Expand All @@ -66,24 +74,27 @@ const getStartDate = (args) => {
const getEndDate = (args) => {
// check module names
if (args._.length === 0) {
return "Please enter the module names at least one. \n";
showHelp("Please enter the module names at least one. \n");
}
if (!args.s && args.e) {
return "Please enter the start date. \nBecause when using -s option, then start date is required. \n";
}
if (!args.e || Object.keys(args).length === 2) {
return format(new Date(), "yyyy-MM-dd");
showHelp(
"Please enter the start date. \nBecause when using -s option, then start date is required. \n",
);
}
// check end date options
if (args.e) {
if (!/\d{4}-\d{2}-\d{2}/.test(args.e)) {
return "Please enter the date correctly. \n";
showHelp("Please enter the date correctly. \n");
}
if (!isValid(parseISO(args.e))) {
return "Please enter the date correctly. \n";
showHelp("Please enter the date correctly. \n");
}
}
return args.e || format(new Date(), "yyyy-MM-dd");
if (args.M) {
return getMonthBounds(args.M, "end");
}

return args.e || format(new Date(), FORMAT_DATE);
};

/**
Expand All @@ -104,4 +115,124 @@ const compare = (a, b) => {
return 0;
};

export { showHelp, getStartDate, getEndDate, compare };
/**
* Checks if the given string is a valid month name or its 3-letter abbreviation.
* The month names and abbreviations are case-insensitive.
*
* @param {string} str - The input string to check.
* @returns {boolean} - Returns true if the input is a valid month name or abbreviation, false otherwise.
*
* @example
* isMonthName('January'); // true
* isMonthName('jan'); // true
* isMonthName('Octob'); // false
*/
const isMonthName = (str) => {
if (str === undefined || typeof str !== "string") {
return false;
}

const months = [
"january",
"jan",
"february",
"feb",
"march",
"mar",
"april",
"apr",
"may",
"june",
"jun",
"july",
"jul",
"august",
"aug",
"september",
"sep",
"october",
"oct",
"november",
"nov",
"december",
"dec",
];

const lowerCaseInput = str.toLowerCase();

return months.includes(lowerCaseInput);
};

/**
* Gets the first and last days of the given month.
*
* @param {string} str - The input string representing the month.
* @param {string} type - The type of day to retrieve ('start' or 'end').
* @returns {Object|null} - Returns an object with the first and last days of the month, or null if the str is invalid.
*
* @example
* getMonthDay('January', 'start'); // '2023-01-01'
* getMonthDay('jan', 'end'); // '2023-01-31'
* getMonthDay('Feb', 'start'); // '2023-02-01'
*/
const getMonthBounds = (str, type) => {
if (typeof str !== "string") {
showHelp("Please enter the Month correctly. \n");
}
if (type !== "start" && type !== "end") {
console.error(
chalk.red.bold(
"Unexpected error: Something went wrong. Please report this issue.\n",
),
);
process.exit(1);
}

// Convert the str to a full month name
const monthNames = {
jan: "january",
feb: "february",
mar: "march",
apr: "april",
may: "may",
jun: "june",
jul: "july",
aug: "august",
sep: "september",
oct: "october",
nov: "november",
dec: "december",
};

let fullMonthName = str.toLowerCase();
if (fullMonthName.length === 3) {
fullMonthName = monthNames[fullMonthName];
}
if (!isMonthName(fullMonthName)) {
showHelp("Please enter the Month correctly. \n");
}

const currentYear = new Date().getFullYear();
const dateStr = `${fullMonthName} 1, ${currentYear}`;
const date = new Date(dateStr);

if (!isValid(date)) {
showHelp("Please enter the Month correctly. \n");
}

if (type === "start") {
return format(startOfMonth(date), FORMAT_DATE);
}
if (type === "end") {
return format(endOfMonth(date), FORMAT_DATE);
}
};

export {
showHelp,
getStartDate,
getEndDate,
compare,
isMonthName,
getMonthBounds,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"chalk": "^5.3.0",
"date-fns": "^3.0.0",
"date-fns-tz": "^3.1.3",
"json-table": "^0.1.3",
"npm-stats-api": "^2.0.2",
"yargs": "^17.7.2"
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 58b17c7

Please sign in to comment.