Skip to content

Commit

Permalink
Add tomorrow and yesterday commands (#5)
Browse files Browse the repository at this point in the history
* Ignore vscode workspace.

* Reformat readme (satisfy vscode markdown linter).

* Improve error handling in cli.

* Improve tests (number of args, invalid date).

* Replace tabs with spaces in readme.

* Support yesterday and tomorrow dates.

* Update README with yesterday and tomorrow keywords.

* Satisfy linter.

* Update CLI help message

Co-authored-by: Knut Kirkhorn <[email protected]>
  • Loading branch information
millette and knutkirkhorn authored Oct 29, 2022
1 parent 403a77f commit a6a1c3b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": ["error", "tab"],
"no-tabs": "off",
"quotes": ["error", "single"],
Expand Down
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# week-number-cli

> 📅 Get the ISO 8601 week number of a date CLI
## Installation

```sh
npm install --global @knutkirkhorn/week-number-cli
```

## Usage

```sh
$ week-number --help

Usage
$ week-number
$ week-number <date>
Examples
$ week-number
$ week-number "September 3, 2019 23:15:30"
Usage
$ week-number
$ week-number "<date>"
$ week-number yesterday
$ week-number tomorrow
Examples
$ week-number
$ week-number "September 3, 2019 23:15:30"
$ week-number yesterday
```

## Related

- [week-number](https://github.com/knutkirkhorn/week-number) - API for this module
24 changes: 18 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
#!/usr/bin/env node

import meow from 'meow';
import weekNumber from '@knutkirkhorn/week-number';
import weekNumber, {tomorrowWeekNumber, yesterdayWeekNumber} from '@knutkirkhorn/week-number';

const cli = meow(`
Usage
$ week-number
$ week-number <date>
$ week-number "<date>"
$ week-number yesterday
$ week-number tomorrow
Examples
$ week-number
$ week-number "September 3, 2019 23:15:30"
$ week-number yesterday
`, {
importMeta: import.meta
});

const inputDate = cli.input[0];

if (cli.input.length === 0) {
console.log(weekNumber());
} else if (cli.input.length === 1) {
console.log(weekNumber(inputDate));
try {
if (cli.input.length > 1) {
throw new Error('Command takes 0 or 1 argument.');
}
if (inputDate === 'yesterday') {
console.log(yesterdayWeekNumber());
} else if (inputDate === 'tomorrow') {
console.log(tomorrowWeekNumber());
} else {
console.log(weekNumber(inputDate));
}
} catch (error) {
console.error(error);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://github.com/knutkirkhorn/week-number-cli#readme",
"dependencies": {
"@knutkirkhorn/week-number": "^2.0.0",
"@knutkirkhorn/week-number": "^2.1.0",
"meow": "^10.1.3"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,23 @@ test('cli can print out', async t => {
const {stdout} = await execa('./cli.js', ['--version']);
t.true(stdout.length > 0);
});

test('cli yesterday', async t => {
const {stdout} = await execa('./cli.js', ['yesterday']);
t.true(stdout.length > 0);
});

test('cli tomorrow', async t => {
const {stdout} = await execa('./cli.js', ['tomorrow']);
t.true(stdout.length > 0);
});

test('cli errors, 2 or more arguments', async t => {
const {stderr} = await execa('./cli.js', ['arg1', 'arg2']);
t.true(stderr.length > 0);
});

test('cli errors, invalid date', async t => {
const {stderr} = await execa('./cli.js', ['arg1']);
t.true(stderr.length > 0);
});

0 comments on commit a6a1c3b

Please sign in to comment.