diff --git a/src/i18n.ts b/src/i18n.ts index 329206a..074e43b 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -1,7 +1,8 @@ import Language from './types/Language'; - export enum MessageKey { + cliHelp, + cliVersion, promptWorkDayDuration, excludingLunch, promptStartTime, @@ -27,6 +28,14 @@ export enum MessageKey { } const messages: Record> = { + [MessageKey.cliHelp]: { + [Language.en]: 'Show this help', + [Language.fi]: 'Näytä tämä ohje', + }, + [MessageKey.cliVersion]: { + [Language.en]: 'Show program version', + [Language.fi]: 'Näytä ohjelman versio', + }, [MessageKey.promptWorkDayDuration]: { [Language.en]: 'How long is your work day today{0}? [{1}]: ', [Language.fi]: 'Kuinka pitkä työpäiväsi on tänään{0}? [{1}]: ', diff --git a/src/input.ts b/src/input.ts index afc847d..386060f 100644 --- a/src/input.ts +++ b/src/input.ts @@ -4,14 +4,14 @@ import * as readline from 'readline/promises'; import { formatDuration, formatTime } from './format'; import { Dayjs } from 'dayjs'; import { WtcPromptResult } from './types/WtcPromptResult'; -import WtcConfig from './types/WtcConfig'; -import { MessageKey, message } from './i18n'; +import { WtcRuntimeConfig } from './types/WtcConfig'; +import { MessageKey } from './i18n'; import dayjs, { Duration } from './dayjs'; const { error } = console; -const input = async (config: WtcConfig): Promise => { - const msg = message(config.language); +const input = async (runtimeCfg: WtcRuntimeConfig): Promise => { + const { config, msg } = runtimeCfg; const fmtDuration = formatDuration(config.language); const { defaults, askInput, unpaidLunchBreakDuration: lunchBreakDuration } = config; const rl = readline.createInterface({ diff --git a/src/main.ts b/src/main.ts index f244ada..6e3b054 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,38 @@ import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; import ui from './ui.js'; +import update from './update.js'; +import getConfig from './config.js'; +import { MessageKey, message } from './i18n.js'; +import { WtcRuntimeConfig } from './types/WtcConfig.js'; + +// Build runtime config +const config = getConfig(); +const msg = message(config.language); +const runtimeConfig: WtcRuntimeConfig = { + config, + msg, +}; // Process args. Yargs will exit if it detects help or version -yargs(hideBin(process.argv)).usage('Work time calculator').alias('help', 'h').alias('version', 'v').argv; +const args = await yargs(hideBin(process.argv)) + .usage('Work time calculator') + .alias('help', 'h') + .alias('version', 'v') + .options({ + help: { + description: msg(MessageKey.cliHelp), + }, + version: { + description: msg(MessageKey.cliVersion), + }, + }).argv; -// Run UI if help or version is not prompted -ui(); +// Run updater if requested +if (args.update) { + update(); + process.exit(0); +} + +// Run UI if no arguments +ui(runtimeConfig); diff --git a/src/output.ts b/src/output.ts index 9d8de7b..86e0619 100644 --- a/src/output.ts +++ b/src/output.ts @@ -1,15 +1,15 @@ import chalk from 'chalk'; import { formatDuration, getHoursRoundedStr } from './format'; import { WtcPromptResult } from './types/WtcPromptResult'; -import { MessageKey, message } from './i18n.js'; -import WtcConfig from './types/WtcConfig'; +import { MessageKey } from './i18n.js'; +import { WtcRuntimeConfig } from './types/WtcConfig'; import dayjs from './dayjs'; const { log } = console; -const output = (result: WtcPromptResult, config: WtcConfig) => { +const output = (result: WtcPromptResult, runtimeCfg: WtcRuntimeConfig) => { + const {config, msg} = runtimeCfg; const { language, timestampFormat } = config; - const msg = message(language); const fmtDuration = formatDuration(language); const hoursRounded = getHoursRoundedStr(language); const { startedAt, stoppedAt, stoppedWorking, worked, unLogged, workLeft, workedOvertime, hadLunch } = result; diff --git a/src/types/WtcConfig.ts b/src/types/WtcConfig.ts index 15d6382..7d822b8 100644 --- a/src/types/WtcConfig.ts +++ b/src/types/WtcConfig.ts @@ -1,6 +1,7 @@ import { Dayjs } from 'dayjs'; import { Duration } from 'dayjs/plugin/duration.js'; import Language from './Language.js'; +import { message } from '../i18n.js'; export default interface WtcConfig { language: Language, @@ -19,3 +20,10 @@ export default interface WtcConfig { logged: boolean; }; } + +/** Config and current language msg function together */ +export interface WtcRuntimeConfig { + config: WtcConfig; + msg: ReturnType; +} + diff --git a/src/ui.ts b/src/ui.ts index 45d0285..9d792fe 100644 --- a/src/ui.ts +++ b/src/ui.ts @@ -1,11 +1,7 @@ -import getConfig from './config.js'; import input from './input.js'; import output from './output.js'; +import { WtcRuntimeConfig } from './types/WtcConfig.js'; -const ui = async () => { - const config = getConfig(); - const result = await input(config); - output(result, config); -}; +const ui = async (config: WtcRuntimeConfig) => output(await input(config), config); export default ui; diff --git a/src/update.ts b/src/update.ts new file mode 100644 index 0000000..92ad114 --- /dev/null +++ b/src/update.ts @@ -0,0 +1,7 @@ +const { log } = console; + +const update = () => { + log('update'); +}; + +export default update;