diff --git a/.travis.yml b/.travis.yml index 432dbd0..f98fed0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - '11' + - '12' - '10' - '8' - - '6' diff --git a/cli.ts b/cli.ts deleted file mode 100644 index 4f17b3d..0000000 --- a/cli.ts +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env node -'use strict'; -import * as meow from 'meow'; -import tizoCli from './index'; - -const cli = meow( - ` - Usage - $ tizo [input] - - Options - --format, -f 'local', 'utc', 'original' [Default: 'local'] - - Examples - $ tizo 12pm - 24:00 - $ tizo --format=utc 12:21 cest - 10:21 UTC -`, - { - flags: { - format: { - type: 'string', - alias: 'f', - default: 'local', - }, - }, - } -); - -tizoCli(cli); diff --git a/index.ts b/index.ts index e5183a1..e3171e2 100644 --- a/index.ts +++ b/index.ts @@ -1,33 +1,41 @@ +#!/usr/bin/env node 'use strict'; import tizo from 'tizo'; +import chalk from 'chalk'; -function print([hours, minutes]) { - let formattedHours = hours; - if (hours < 10) { - formattedHours = String('0' + hours); +function format(tizoTime: [number, number]): string { + let hours = String(tizoTime[0]); + if (hours.length === 1) { + hours = '0' + hours; } - let formattedMinutes = minutes; - if (minutes < 10) { - formattedMinutes = String('0' + minutes); + let minutes = String(tizoTime[1]); + if (minutes.length === 1) { + minutes = '0' + minutes; } - console.log(`${formattedHours}:${formattedMinutes}`); + return `${hours}:${minutes}`; } -export default ({flags, input}) => { - const date = tizo(input.join(' ')); - switch (flags.format) { - case 'local': - print(date.local); - break; - case 'utc': - print(date.utc); - break; - case 'original': - print(date.original); - break; - default: - console.log('Unknown format'); - } -}; +const log = console.log; +const argv = process.argv.splice(2).join(' '); +if (argv.length === 0) { + console.error('No input supplied'); + process.exit(1); +} + +try { + var tizoResult = tizo(argv); +} catch (error) { + console.error(error.message); + process.exit(1); +} +const {original, local, utc, inputTimezone} = tizoResult; + +log(); +log( + ` ${chalk.blue('original')} ${format(original)} ${inputTimezone.name || + 'Local Time'}` +); +log(` ${chalk.blue('local')} ${format(local)}`); +log(` ${chalk.blue('utc')} ${format(utc)}`); diff --git a/package.json b/package.json index 20efcd7..741c5c8 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "tizo": "out/cli.js" }, "engines": { - "node": ">=6" + "node": ">= 8" }, "scripts": { "prettier-fix": "prettier --write *.ts", @@ -36,13 +36,14 @@ "parse" ], "dependencies": { + "chalk": "^2.4.2", "meow": "^5.0.0", - "tizo": "^2.0.1" + "tizo": "^3.0.0" }, "devDependencies": { "@types/meow": "^5.0.0", "@types/node": "^11.13.8", - "ava": "^1.1.0", + "ava": "^2.1.0", "prettier": "^1.16.4", "typescript": "^3.3.3333" } diff --git a/test.ts b/test.ts index 4b4d93e..e31324f 100644 --- a/test.ts +++ b/test.ts @@ -1,19 +1,14 @@ import {execSync} from 'child_process'; import test from 'ava'; -test('return correct format', t => { - const stdout = execSync('node ./out/cli.js -f="utc" 12am pdt'); - t.true(stdout.includes('07:00')); -}); - -test('use standard format if not provided', t => { - const stdout = execSync('node ./out/cli.js 12am pdt'); - t.true(stdout.length === 6); -}); - -test('throw error if format not found', t => { - const stdout = execSync( - 'node ./out/cli.js -f="idontexist" 12am pdt' - ).toString(); - t.true(stdout.includes('Unknown format')); +test('return correct formats', t => { + const stdout = execSync('node ./out/index.js 12am pt'); + t.( + stdout.toString(), + ` + original 00:00 Pacific Time + local 10:00 + utc 08:00 + ` + ); });