UNPKG

@whook/whook

Version:

Build strong and efficient REST web services.

60 lines (54 loc) 1.44 kB
import { autoService } from 'knifecycle'; import { hasDefinedKey, noop } from '../libs/utils.js'; import { YError } from 'yerror'; import { type AppEnvVars } from 'application-services'; import { type LogService } from 'common-services'; import { type WhookCommandHandler, type WhookCommandDefinition, } from '../types/commands.js'; export const definition = { name: 'env', description: 'A command printing env values', example: `whook env --name NODE_ENV --default "default value"`, config: { promptArgs: true }, arguments: [ { name: 'name', required: true, description: 'Environment variable name to pick-up', schema: { type: 'string' }, }, { name: 'default', description: 'Provide a default value', schema: { type: 'string' }, }, ], } as const satisfies WhookCommandDefinition; export default autoService(initEnvCommand); async function initEnvCommand({ ENV, log = noop, }: { ENV: AppEnvVars; log?: LogService; }): Promise< WhookCommandHandler<{ name: string; default?: string; }> > { return async (args) => { const { namedArguments: { name, default: defaultValue }, } = args; if ('undefined' === typeof defaultValue && !hasDefinedKey(ENV, name)) { throw new YError('E_NO_ENV_VALUE', [name]); } log( 'info', `${(ENV as unknown as Record<string, string>)[name] || defaultValue}`, ); }; }