-
Notifications
You must be signed in to change notification settings - Fork 30
/
install.ts
74 lines (63 loc) · 2.55 KB
/
install.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
import { Flags } from "@oclif/core";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { InputError } from "../../lib/errors.js";
import { installCliDevDeps } from "../../lib/tasks.js";
import { SUPPORTED_DEPS } from "../../lib/consts.js";
import { DependencyName, SwankyConfig, getSwankyConfig } from "../../index.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
export class Install extends SwankyCommand<typeof Install> {
static description = "Install dev dependencies";
static flags = {
deps: Flags.string({
description: `Install the specified dev dependency name and version in the format <dependency@version>. The following options are supported: ${Object.keys(
SUPPORTED_DEPS
).join(", ")}. For installing rust nightly version run: env install --deps rust@nightly`,
multiple: true,
char: "d",
}),
};
async run(): Promise<void> {
const { flags } = await this.parse(Install);
const depsArray = flags.deps ?? [];
const localConfig = getSwankyConfig("local") as SwankyConfig;
const depsToInstall = depsArray.length > 0 ? this.parseDeps(depsArray) : localConfig.env;
if (Object.keys(depsToInstall).length === 0) {
this.log("No dependencies to install.");
return;
}
await this.installDeps(depsToInstall);
if (depsArray.length > 0) {
await this.updateLocalConfig(depsToInstall);
}
this.log("Swanky Dev Dependencies Installed successfully");
}
parseDeps(deps: string[]): Record<string, string> {
return deps.reduce(
(acc, dep) => {
const [key, value] = dep.split("@");
if (!Object.keys(SUPPORTED_DEPS).includes(key)) {
throw new InputError(
`Unsupported dependency '${key}'. Supported: ${Object.keys(SUPPORTED_DEPS).join(", ")}`
);
}
acc[key] = value || "latest";
return acc;
},
{} as Record<string, string>
);
}
async installDeps(dependencies: Record<string, string>) {
for (const [dep, version] of Object.entries(dependencies)) {
await this.spinner.runCommand(
() => installCliDevDeps(this.spinner, dep as DependencyName, version),
`Installing ${dep}@${version}`
);
}
}
async updateLocalConfig(newDeps: Record<string, string>): Promise<void> {
await this.spinner.runCommand(async () => {
const newLocalConfig = new ConfigBuilder(getSwankyConfig("local")).updateEnv(newDeps).build();
await this.storeConfig(newLocalConfig, "local");
}, "Updating Swanky config with new Dev Dependencies...");
}
}