-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprintEnv.ts
77 lines (66 loc) · 1.93 KB
/
printEnv.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { extra, autoService } from 'knifecycle';
import { type LogService } from 'common-services';
import { type AppEnvVars } from 'application-services';
import {
readArgs,
type WhookCommandArgs,
type WhookCommandDefinition,
} from '@whook/whook';
/* Architecture Note #5: Commands
We consider to be a good practice to bind the commands
you write to your API code. The `src/commands` folder
allows you to write commands in a similar way to handlers.
It leverages the dependency injection features of Whook
and has helpers for parsing the input parameters.
You can list every available commands by running:
```sh
npm run dev -- ls
```
Commands are a simple way to write utility scripts that leverage
your application setup. It allows to simply inject services
without worrying about their initialization.
*/
/* Architecture Note #5.1: Definition
To define a command, just write its definition
and export it to make it available to Whook's
command loader.
*/
export const definition: WhookCommandDefinition = {
description: 'A command printing every env values',
example: `whook printEnv --keysOnly`,
arguments: {
type: 'object',
additionalProperties: false,
required: [],
properties: {
keysOnly: {
description: 'Option to print only env keys',
type: 'boolean',
},
},
},
};
/* Architecture Note #5.2: Implementation
To implement a command, just write a function that takes
injected services as a first argument and return the
command as an asynchronous function.
*/
export default extra(definition, autoService(initPrintEnvCommand));
async function initPrintEnvCommand({
ENV,
log,
args,
}: {
ENV: AppEnvVars;
log: LogService;
args: WhookCommandArgs;
}) {
return async () => {
const {
namedArguments: { keysOnly },
} = readArgs<{
keysOnly: boolean;
}>(definition.arguments, args);
log('info', `${JSON.stringify(keysOnly ? Object.keys(ENV) : ENV)}`);
};
}