-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.ts
94 lines (83 loc) · 2.83 KB
/
types.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 { Project } from "./Project";
import { debug } from "./utils/logger";
type ParamType = "boolean" | "number" | "string";
type ParamValueType<T extends ParamType> = T extends "boolean"
? boolean
: T extends "number"
? number
: T extends "string"
? string
: never;
type ScriptParam<T extends ParamType, P extends Record<string, ParamType>> = {
title: string;
type: T;
defaultValue?:
| ParamValueType<T>
| ((args: { project: Project; params: { [K in keyof P]: ParamValueType<P[K]> } }) => ParamValueType<T> | undefined);
required?: boolean;
};
type Params<T extends Record<string, ParamType> = Record<string, ParamType>> = {
[K in keyof T]: ScriptParam<T[K], T> & { type: T[K] };
};
type Script<P extends Record<string, ParamType>> = {
id: string;
title?: string;
params?: Params<P>;
isValid?: (project: Project) => boolean;
run: (args: {
cwd: string;
project: Project;
self: Script<P>;
params: { [K in keyof P]: ParamValueType<P[K]> };
files: { path: string; content: string }[];
fileMap: Record<string, string>;
t: (text: string, params?: Record<string, boolean | number | string>) => string;
}) => void;
};
const getDefaultParamValue = <T extends ParamType>(type: T) => {
debug("Getting default value for param type:", type);
const defaultValues: Record<ParamType, ParamValueType<ParamType>> = {
boolean: false,
number: 0,
string: "",
};
const value = defaultValues[type] as ParamValueType<T>;
debug("Default value:", value);
return value;
};
const autoSymbol = Symbol.for("auto");
const auto = <P extends Record<string, ParamType>>(script: Script<P>) => {
debug("Initializing auto script:", script.id);
debug("Script configuration:", {
id: script.id,
title: script.title,
params: script.params,
isValid: script.isValid,
});
return {
[autoSymbol]: true,
...script,
isLocal: false,
path: "",
bootstrapParams: () => {
debug("Bootstrapping params for script:", script.id);
const params = Object.fromEntries(
Object.entries(script.params ?? {}).map(([key, param]) => {
debug("Processing param:", { key, param });
let value = getDefaultParamValue(param.type);
if (typeof param.defaultValue !== "function" && param.defaultValue !== undefined) {
value = param.defaultValue;
debug("Using static default value:", { key, value });
}
return [key, { ...param, value }];
}) as [keyof P, ScriptParam<P[keyof P], P> & { value: ParamValueType<P[keyof P]> }][]
);
debug("Bootstrapped params:", params);
return params;
},
};
};
type AutoType = typeof auto;
type AutoReturnType = ReturnType<AutoType>;
export type { AutoReturnType, AutoType, Params, ParamType, ParamValueType, Script, ScriptParam };
export { auto, autoSymbol };