-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.ts
56 lines (45 loc) · 1.45 KB
/
run.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
import {Args} from '@oclif/core'
import {execSync} from 'node:child_process'
import {BaseCommand} from '../lib/base-command'
/** arguments */
const CMD_ARG = 'command' as const
/**
* Oclif command class
*/
export default class Run extends BaseCommand<typeof Run> {
static description = 'run a given command with loaded env variables';
static examples = [
{
command: 'envtk run "npm run dev" -s ./load-env.js',
description: 'Run command with env variables returned from script',
},
{
command: 'envtk run "npm run dev" -s ./load-env.js -e .env',
description:
' Run command with env variables returned from script and include defaults from ".env"',
},
];
static args = {
[CMD_ARG]: Args.string({
name: 'command',
required: true,
description: 'command to run with the loaded environment variables',
}),
};
public async run(): Promise<any> {
const {args, flags} = await this.parse(Run)
const USER_COMMAND = args[CMD_ARG] // Command the user wants to run
if (flags.envFile) {
this.loadEnvironmentFile(flags.envFile)
}
if (flags.script) {
await this.loadUserScriptValues(flags.script!)
}
const envObj = Object.fromEntries(this.environment) as Record<string, any> // create object from map because exec doesn't work with maps
execSync(USER_COMMAND, {env: envObj, stdio: 'inherit'})
if (flags.json) {
return envObj
}
this.exit(0)
}
}