i18n CLI options

This commit is contained in:
Marko Korhonen 2023-11-27 17:57:13 +02:00
parent 3239a7c611
commit c97472f6f5
Signed by: FunctionalHacker
GPG key ID: A7F78BCB859CD890
7 changed files with 67 additions and 18 deletions

View file

@ -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, Record<Language, string>> = {
[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}]: ',

View file

@ -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<WtcPromptResult> => {
const msg = message(config.language);
const input = async (runtimeCfg: WtcRuntimeConfig): Promise<WtcPromptResult> => {
const { config, msg } = runtimeCfg;
const fmtDuration = formatDuration(config.language);
const { defaults, askInput, unpaidLunchBreakDuration: lunchBreakDuration } = config;
const rl = readline.createInterface({

View file

@ -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);

View file

@ -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;

View file

@ -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<typeof message>;
}

View file

@ -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;

7
src/update.ts Normal file
View file

@ -0,0 +1,7 @@
const { log } = console;
const update = () => {
log('update');
};
export default update;