From b699ae5cac971225ad4478eb00b682a2a3a41e25 Mon Sep 17 00:00:00 2001 From: Boris Kubiak Date: Tue, 11 Oct 2022 15:06:33 +0200 Subject: [PATCH] Add documentation & reformat some code --- README.md | 14 ++++++++- bin/cli.ts | 2 +- bin/metering.ts | 84 ++++++++++++++++++++++++------------------------- 3 files changed, 55 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index cc39f40..78ac25a 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,19 @@ Vous pouvez sauvegarder vos résultats dans un fichier JSON grâce à l'option ` linky loadcurve --start 2021-12-31 --end 2022-01-01 --output data/ma_conso.json ``` -Pour voir l'aide détaillée : +Vous pouvez changer le format d'affichage de sortie grâce à l'option `--format` + +```bash +linky daily --start 2022-01-01 --end 2022-01-08 --format json +``` + +Vous pouvez masquer les messages et animations de progression grâce à l'option `--quiet` + +```bash +linky maxpower --quiet +``` + +Pour voir l'aide détaillée et plus d'exemples : ```bash linky --help diff --git a/bin/cli.ts b/bin/cli.ts index 15d21cb..34f1876 100644 --- a/bin/cli.ts +++ b/bin/cli.ts @@ -45,7 +45,7 @@ const mainHelp = ` linky auth -a Kft3SIZrcq -r F3AR0K8eoC -u 225169 linky daily linky dailyprod --start 2022-01-01 --end 2022-01-08 - linky maxpower --start 2021-08-01 --end 2021-08-15 + linky maxpower --start 2021-08-01 --end 2021-08-15 --format json --quiet linky loadcurve -s 2022-01-01 -e 2022-01-08 -o data/ma_conso.json linky loadcurveprod -u 225169 `; diff --git a/bin/metering.ts b/bin/metering.ts index 8d7e129..c318713 100644 --- a/bin/metering.ts +++ b/bin/metering.ts @@ -93,30 +93,27 @@ function handle( const session = store.getStoredSession(usagePointId); const previousAccessToken = session?.accessToken; - const spinner = ora(); - if (!quiet) spinner.start(spinnerText); + const spinner = ora({ isSilent: quiet }).start(spinnerText); return promise .then(async (consumption) => { if (output) { try { await mkdirp(path.dirname(output)); - fs.writeFileSync(output, JSON.stringify(consumption, null, 4)); + fs.writeFileSync(output, JSON.stringify(consumption, null, 2)); } catch (e) { throw new Error(`Impossible d'écrire dans ${output}:\n${e.message}`); } } - if (!quiet) { - spinner.succeed(); + spinner.succeed(); - if (store.getStoredSession(session?.usagePointId)?.accessToken !== previousAccessToken) { - ora('Vos tokens ont été automatiquement renouvelés').succeed(); - } + if (store.getStoredSession(session?.usagePointId)?.accessToken !== previousAccessToken) { + ora({ isSilent: quiet }).succeed('Vos tokens ont été automatiquement renouvelés'); + } - if (output) { - ora(`Résultats sauvegardés dans ${output}`).succeed(); - } + if (output) { + ora({ isSilent: quiet }).succeed(`Résultats sauvegardés dans ${output}`); } if (!output) { @@ -124,41 +121,42 @@ function handle( } }) .catch((e) => { - spinner.fail(e.message); + spinner.stop(); + ora(e.message).fail(); throw new Error(); }); } -function render(format: Format, ...args: [Consumption, boolean]) { - Rendered[format](...args); -} - -const Rendered = { - json(consumption: Consumption, displayTime: boolean) { - console.log(JSON.stringify(consumption, null, 2)); - }, - pretty(consumption: Consumption, displayTime: boolean) { - const maxValue = Math.max(...consumption.data.map((x) => x.value)); - const chartLength = 30; - // Headers - console.info( - '\n' + - chalk.yellow.underline(`Date${' '.repeat(displayTime ? 16 : 7)}`) + - ' ' + - chalk.green.underline(`Valeur (${consumption.unit})`) + - ' ' + - chalk.cyan.underline(`Graphique${' '.repeat(chartLength - 9)}`) - ); - - for (const line of consumption.data) { +function render(format: Format, consumption: Consumption, displayTime: boolean) { + switch (format) { + case 'json': + console.info(JSON.stringify(consumption, null, 2)); + break; + case 'pretty': + const maxValue = Math.max(...consumption.data.map((x) => x.value)); + const chartLength = 30; + // Headers console.info( - chalk.yellow(`${line.date} `) + - chalk.green(`${line.value}`) + - ' '.repeat(10 + consumption.unit.length - line.value.toString().length) + - chalk.cyan( - '■'.repeat(maxValue && line.value ? Math.ceil((chartLength * line.value) / maxValue) : 0) - ) + '\n' + + chalk.yellow.underline(`Date${' '.repeat(displayTime ? 16 : 7)}`) + + ' ' + + chalk.green.underline(`Valeur (${consumption.unit})`) + + ' ' + + chalk.cyan.underline(`Graphique${' '.repeat(chartLength - 9)}`) ); - } - }, -}; + // Data + for (const line of consumption.data) { + console.info( + chalk.yellow(`${line.date} `) + + chalk.green(`${line.value}`) + + ' '.repeat(10 + consumption.unit.length - line.value.toString().length) + + chalk.cyan( + '■'.repeat(maxValue && line.value ? Math.ceil((chartLength * line.value) / maxValue) : 0) + ) + ); + } + break; + default: + ora(`Le format "${format}" est invalide. Formats acceptés: "pretty", "json".`).fail(); + } +}