-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.ts
94 lines (80 loc) · 2.35 KB
/
config.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { credentials } from "./credentials.ts";
import { ConfigError } from "./errors.ts";
import { logger } from "./logger.ts";
import { config as zcliConfig, didYouMean, fmt, locale, z } from "./zcli.ts";
export const CONFIG_PATH = `${Deno.env.get("HOME")}/.paperspace/config.toml`;
const configShape = {
version: z.literal(1).default(1),
team: z
.string()
.refine(async (value): Promise<string | null> => {
const creds = await credentials.get("keys");
if (creds[value]) {
return value;
}
const teams = Object.keys(creds);
logger.error(
`Team "${value}" was not found in your credentials file. Retaining current team.`,
);
const correction = teams.length
? didYouMean(value, teams)
: `→ Are you logged in? Try running "${
fmt.colors.bold("pspace login")
}" first.`;
throw new ConfigError(
`A team named "${value}" was not found in your credentials file.\n ${correction}`,
);
})
.describe("The name of the current team.")
.nullable()
.default(null),
projects: z.record(z.object({ id: z.string(), path: z.string() }))
.default(
{},
),
locale: z
.string()
.transform((value) => {
try {
const nextLocale = value.split(":")[0].split(".")[0].replace("_", "-");
new Intl.DateTimeFormat(nextLocale);
return nextLocale;
} catch (_err) {
return "en-US";
}
})
.default(locale),
} as const;
/**
* This is the configuration file that is used to store things like the current
* team and locale. You can add new keys to this object and they will be added
* to the configuration file automatically.
*/
export const config = zcliConfig(configShape, {
defaultConfig: {
version: 1,
team: null,
projects: {},
locale,
},
path: CONFIG_PATH,
});
export const configPaths = deepKeys(configShape).filter((p) =>
p !== "version"
) as [string, ...string[]];
function deepKeys(obj: Record<string | number | symbol, unknown>): string[] {
// Use tail call optimization
const stack = [obj];
const keys: string[] = [];
while (stack.length > 0) {
const obj = stack.pop();
for (const key in obj) {
keys.push(key);
const value = obj[key];
if (value instanceof z.ZodObject) {
stack.push(value.shape);
}
}
}
return keys;
}