Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

Commit

Permalink
Add interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Jan 28, 2019
1 parent ab5db5b commit f25fcbc
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 70 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ $ npm i -g linky-cli
## Usage

```sh
# Basic usage
# Basic usage (interactive)
linky

# Basic usage (no questions asked)
linky (hour|day|month|year) -u <email> -p <password>

# Save data to file with -o
linky (hour|day|month|year) -u <email> -p <password> [-o <path>]
linky -o <path>

# Custom time period with -s (start) and -e (end)
linky (hour|day|month|year) -u <email> -p <password> [-s <DD/MM/YYYY>] [-e <DD/MM/YYYY>]
linky -s <DD/MM/YYYY> -e <DD/MM/YYYY>

# Show help and examples
# Show help and more examples
linky --help
```

Expand Down
48 changes: 29 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ const meow = require('meow');
const linky = require('@bokub/linky');
const chalk = require('chalk');
const display = require('./src/display');
const inquire = require('./src/inquire');
const save = require('./src/save');
const validate = require('./src/validate');

const cli = meow(`
const c = meow(`
Usage
$ linky (hour|day|month|year) -u <email> -p <password> [options]
$ linky [(hour|day|month|year)] [options]
Options
--user -u Linky client area e-mail
Expand All @@ -20,9 +21,11 @@ const cli = meow(`
--end -e End of the custom time period (DD/MM/YYYY)
Examples
$ linky month -u [email protected] -p password123
$ linky
$ linky year
$ linky month -u [email protected]
$ linky day -s 24/08/2018 -e 06/09/2018
$ linky hour -u [email protected] -p password123 -o ../export/hourly-data.json
$ linky day -u [email protected] -p password123 -s 24/08/2018 -e 06/09/2018
`, {
description: false,
flags: {
Expand All @@ -34,7 +37,28 @@ const cli = meow(`
}
});

async function main(i) {
main(c).catch(err => {
console.error(chalk.red(err));
process.exit(1);
});

async function main(cli) {
await inquire(cli);

const validationResult = validate(cli);
if (validationResult.error) {
console.error(chalk.red(validationResult.message));
cli.showHelp();
process.exit(1);
}

const data = await getData(cli);
await save(data, cli.flags.output);

display(data);
}

async function getData(i) {
const session = await linky.login(i.flags.user, i.flags.password);
let data;
switch (i.input[0]) {
Expand All @@ -55,17 +79,3 @@ async function main(i) {
}
return data;
}

const validationResult = validate(cli);
if (validationResult.error) {
console.error(chalk.red(validationResult.message));
cli.showHelp();
}

main(cli).then(data => {
save(data, cli.flags.output);
display(data);
}).catch(err => {
console.error(chalk.red(err));
process.exit(1);
});
80 changes: 35 additions & 45 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"@bokub/linky": "^0.1.0",
"chalk": "^2.4.1",
"inquirer": "^6.2.1",
"joi": "^14.0.6",
"joi-date-extensions": "^1.2.0",
"meow": "^5.0.0",
Expand Down
Loading

0 comments on commit f25fcbc

Please sign in to comment.