@highstate/pulumi 0.9.18 → 0.9.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/highstate.manifest.json +1 -1
- package/dist/index.js +181 -332
- package/dist/index.js.map +1 -1
- package/package.json +13 -7
- package/src/file.ts +68 -0
- package/src/index.ts +1 -1
- package/src/unit.ts +263 -469
- package/src/utils.ts +56 -141
- package/src/secret.ts +0 -61
package/src/utils.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type Input, type
|
1
|
+
import { type Input, type Output, output, type Unwrap } from "@pulumi/pulumi"
|
2
2
|
|
3
3
|
/**
|
4
4
|
* The input type for an array of inputs.
|
@@ -7,182 +7,97 @@ import { type Input, type Unwrap, type Output, output, all } from "@pulumi/pulum
|
|
7
7
|
export type InputArray<T> = Input<Input<T>[]>
|
8
8
|
|
9
9
|
/**
|
10
|
-
* The input type for a
|
10
|
+
* The input type for a record of inputs.
|
11
11
|
* The same as `Input<Record<string, Input<T>>>`, but more readable.
|
12
12
|
*/
|
13
|
-
export type
|
13
|
+
export type InputRecord<T> = Input<Record<string, Input<T>>>
|
14
14
|
|
15
15
|
/**
|
16
16
|
* The input or input array type for a value.
|
17
17
|
*/
|
18
18
|
export type InputOrArray<T> = Input<T> | InputArray<T>
|
19
19
|
|
20
|
+
type LeafValue = string | number | boolean | null | undefined
|
21
|
+
type IsUnknown<T> = unknown extends T ? (T extends unknown ? true : false) : false
|
22
|
+
|
20
23
|
/**
|
21
|
-
* The input
|
24
|
+
* The recursive input type for a value.
|
22
25
|
*/
|
23
|
-
export type DeepInput<T> = [T] extends [
|
24
|
-
?
|
25
|
-
|
26
|
+
export type DeepInput<T> = [T] extends [LeafValue]
|
27
|
+
? Input<T>
|
28
|
+
: IsUnknown<T> extends true
|
29
|
+
? Input<unknown>
|
26
30
|
: Input<{ [K in keyof T]: DeepInput<T[K]> }>
|
27
|
-
: [T] extends [Array<unknown> | undefined]
|
28
|
-
? [T] extends [(infer U)[] | undefined]
|
29
|
-
? Input<DeepInput<U>[] | undefined>
|
30
|
-
: Input<DeepInput<T>[]>
|
31
|
-
: Input<T>
|
32
31
|
|
33
32
|
/**
|
34
|
-
*
|
33
|
+
* Transforms an input value to a promise that resolves to the unwrapped value.
|
35
34
|
*
|
36
|
-
* @param
|
37
|
-
* @returns
|
35
|
+
* @param input The input value to transform.
|
36
|
+
* @returns A promise that resolves to the unwrapped value.
|
38
37
|
*/
|
39
|
-
export function
|
40
|
-
return
|
41
|
-
const result: T[] = []
|
42
|
-
for (const value of allValues) {
|
43
|
-
if (Array.isArray(value)) {
|
44
|
-
result.push(...(value as T[]))
|
45
|
-
} else if (value) {
|
46
|
-
result.push(value as T)
|
47
|
-
}
|
48
|
-
}
|
49
|
-
return result
|
50
|
-
})
|
38
|
+
export function toPromise<T>(input: Input<T>): Promise<Unwrap<T>> {
|
39
|
+
return new Promise(resolve => output(input).apply(resolve))
|
51
40
|
}
|
52
41
|
|
53
42
|
/**
|
54
|
-
*
|
55
|
-
*
|
43
|
+
* Receives an item and a collection, and returns an array containing the item and the collection.
|
44
|
+
*
|
45
|
+
* Excludes the item if it is undefined.
|
56
46
|
*
|
57
|
-
* @param
|
58
|
-
* @param
|
59
|
-
* @returns The output array.
|
47
|
+
* @param item The single item input.
|
48
|
+
* @param collection The collection of items input.
|
60
49
|
*/
|
61
|
-
export function
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
return output(array).apply(array => {
|
66
|
-
return array?.map((v, index) => fn(v as Unwrap<T>, index, array as Unwrap<T>[])) ?? []
|
67
|
-
})
|
68
|
-
}
|
50
|
+
export function normalize<T>(item: T | undefined, collection: T[] | undefined): T[] {
|
51
|
+
if (item && collection) {
|
52
|
+
return [item, ...collection]
|
53
|
+
}
|
69
54
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
fn: (v: Unwrap<T>, index: number, all: Unwrap<T>[]) => U,
|
74
|
-
): Output<U[]>
|
55
|
+
if (item) {
|
56
|
+
return [item]
|
57
|
+
}
|
75
58
|
|
76
|
-
|
77
|
-
|
78
|
-
v2: InputOrArray<T>,
|
79
|
-
v3: InputOrArray<T>,
|
80
|
-
fn: (v: Unwrap<T>, index: number, all: Unwrap<T>[]) => U,
|
81
|
-
): Output<U[]>
|
59
|
+
return collection ?? []
|
60
|
+
}
|
82
61
|
|
83
62
|
/**
|
84
|
-
*
|
63
|
+
* The same as `normalize`, but accepts inputs and returns output.
|
85
64
|
*
|
86
|
-
* @param
|
87
|
-
* @param
|
65
|
+
* @param item The single item input.
|
66
|
+
* @param collection The collection of items input.
|
88
67
|
*/
|
89
|
-
export function
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
68
|
+
export function normalizeInputs<T>(
|
69
|
+
item: Input<T> | undefined,
|
70
|
+
collection: InputArray<T> | undefined,
|
71
|
+
): Output<Unwrap<T>[]> {
|
72
|
+
return (
|
73
|
+
output({ item, collection })
|
74
|
+
//
|
75
|
+
.apply(({ item, collection }) => normalize(item, collection)) as Output<Unwrap<T>[]>
|
76
|
+
)
|
96
77
|
}
|
97
78
|
|
98
79
|
/**
|
99
|
-
*
|
80
|
+
* The convenience function to normalize inputs and map them to a new type.
|
100
81
|
*
|
101
|
-
* @param
|
102
|
-
* @param
|
103
|
-
* @
|
82
|
+
* @param item The single item input.
|
83
|
+
* @param collection The collection of items input.
|
84
|
+
* @param mapFn The function to map each item to a new type.
|
104
85
|
*/
|
105
|
-
export function
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
return
|
111
|
-
}
|
112
|
-
|
113
|
-
export function toPromise<T>(input: Input<T>): Promise<Unwrap<T>> {
|
114
|
-
return new Promise(resolve => output(input).apply(resolve))
|
115
|
-
}
|
116
|
-
|
117
|
-
export function singleton<T>(factory: () => T): () => T {
|
118
|
-
let instance: T | undefined
|
119
|
-
return () => {
|
120
|
-
if (instance === undefined) {
|
121
|
-
instance = factory()
|
122
|
-
}
|
123
|
-
|
124
|
-
return instance
|
125
|
-
}
|
126
|
-
}
|
127
|
-
|
128
|
-
export function providerFactory<TInput>(
|
129
|
-
factory: (name: string) => TInput,
|
130
|
-
): (name: string) => TInput {
|
131
|
-
const instances = new Map<string, TInput>()
|
132
|
-
return name => {
|
133
|
-
if (!instances.has(name)) {
|
134
|
-
instances.set(name, factory(name))
|
135
|
-
}
|
136
|
-
|
137
|
-
return instances.get(name)!
|
138
|
-
}
|
86
|
+
export function normalizeInputsAndMap<T, U>(
|
87
|
+
item: Input<T> | undefined,
|
88
|
+
collection: InputArray<T> | undefined,
|
89
|
+
mapFn: (value: Unwrap<T>) => U,
|
90
|
+
): Output<U[]> {
|
91
|
+
return normalizeInputs(item, collection).apply(values => values.map(mapFn))
|
139
92
|
}
|
140
93
|
|
141
|
-
export function mergeInputObjects<
|
142
|
-
T1 extends Record<string, unknown>,
|
143
|
-
T2 extends Record<string, unknown>,
|
144
|
-
>(obj1: Input<T1 | undefined> | undefined, obj2: Input<T2 | undefined> | undefined): Output<T1 & T2>
|
145
|
-
|
146
|
-
export function mergeInputObjects<
|
147
|
-
T1 extends Record<string, unknown>,
|
148
|
-
T2 extends Record<string, unknown>,
|
149
|
-
T3 extends Record<string, unknown>,
|
150
|
-
>(
|
151
|
-
obj1: Input<T1 | undefined> | undefined,
|
152
|
-
obj2: Input<T2 | undefined> | undefined,
|
153
|
-
obj3: Input<T3 | undefined> | undefined,
|
154
|
-
): Output<T1 & T2 & T3>
|
155
|
-
|
156
94
|
/**
|
157
|
-
*
|
95
|
+
* Applies a function to the input and returns an output.
|
96
|
+
*
|
97
|
+
* Can be used in `remeda` pipelines.
|
158
98
|
*
|
159
|
-
* @param
|
160
|
-
* @returns The output object.
|
99
|
+
* @param fn The function to apply to the input.
|
161
100
|
*/
|
162
|
-
export function mergeInputObjects(
|
163
|
-
...objects: Input<Record<string, unknown>>[]
|
164
|
-
): Output<Record<string, unknown>> {
|
165
|
-
return output(objects).apply(array => {
|
166
|
-
return Object.assign({}, ...array) as Record<string, unknown>
|
167
|
-
})
|
168
|
-
}
|
169
|
-
|
170
|
-
export function normalize<T>(item: T | undefined, collection: T[] | undefined): T[] {
|
171
|
-
if (item && collection) {
|
172
|
-
return [item, ...collection]
|
173
|
-
}
|
174
|
-
|
175
|
-
if (item) {
|
176
|
-
return [item]
|
177
|
-
}
|
178
|
-
|
179
|
-
return collection ?? []
|
180
|
-
}
|
181
|
-
|
182
101
|
export function apply<T, U>(fn: (value: Unwrap<T>) => U): (input: Input<T>) => Output<U> {
|
183
102
|
return input => output(input).apply(fn)
|
184
103
|
}
|
185
|
-
|
186
|
-
export function applyMap<T, U>(fn: (value: Unwrap<T>) => U): (input: Input<T[]>) => Output<U[]> {
|
187
|
-
return input => output(input).apply(array => array.map(fn))
|
188
|
-
}
|
package/src/secret.ts
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
import { HighstateSignature, type UnitSecretModel } from "@highstate/contract"
|
2
|
-
import { type Input, output, type Output, secret as pulumiSecret } from "@pulumi/pulumi"
|
3
|
-
|
4
|
-
export type UnitSecret<TValue> = Omit<UnitSecretModel, "value"> & {
|
5
|
-
value: Output<TValue>
|
6
|
-
}
|
7
|
-
|
8
|
-
export const updatedSecretValues: Record<string, unknown> = {}
|
9
|
-
|
10
|
-
/**
|
11
|
-
* Ensures that the provided secret contains a value.
|
12
|
-
* If the secret value is undefined, it will create a new secret value using the provided create function.
|
13
|
-
*
|
14
|
-
* NOTE: This function mutates the internal state and affects the output of the unit.
|
15
|
-
* Avoid calling this function multiple times for the same secret in a single unit execution.
|
16
|
-
*
|
17
|
-
* @param secret The secret to ensure a value for.
|
18
|
-
* @param create The function to create a new secret value if the current value is undefined.
|
19
|
-
* @return The secret with a guaranteed value.
|
20
|
-
*/
|
21
|
-
export function ensureSecretValue<TValue>(
|
22
|
-
secret: UnitSecret<TValue>,
|
23
|
-
create: () => Input<TValue>,
|
24
|
-
): UnitSecret<NonNullable<TValue>> {
|
25
|
-
return {
|
26
|
-
[HighstateSignature.Secret]: true,
|
27
|
-
id: secret.id,
|
28
|
-
value: secret.value.apply(value => {
|
29
|
-
if (value !== undefined) {
|
30
|
-
return value
|
31
|
-
}
|
32
|
-
|
33
|
-
updatedSecretValues[secret.id] = pulumiSecret(create())
|
34
|
-
return updatedSecretValues[secret.id]
|
35
|
-
}) as Output<NonNullable<TValue>>,
|
36
|
-
}
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Updates the value of the provided secret.
|
41
|
-
* This function is used to store a new value for the secret regardless of its current value.
|
42
|
-
*
|
43
|
-
* NOTE: This function mutates the internal state and affects the output of the unit.
|
44
|
-
* Avoid calling this function multiple times for the same secret in a single unit execution.
|
45
|
-
*
|
46
|
-
* @param secret The secret to update.
|
47
|
-
* @param value The new value to store in the secret.
|
48
|
-
* @returns The updated secret with the new value.
|
49
|
-
*/
|
50
|
-
export function updateSecretValue<TValue>(
|
51
|
-
secret: UnitSecret<TValue>,
|
52
|
-
value: Input<TValue>,
|
53
|
-
): UnitSecret<TValue> {
|
54
|
-
updatedSecretValues[secret.id] = pulumiSecret(value)
|
55
|
-
|
56
|
-
return {
|
57
|
-
[HighstateSignature.Secret]: true,
|
58
|
-
id: secret.id,
|
59
|
-
value: output(updatedSecretValues[secret.id]),
|
60
|
-
}
|
61
|
-
}
|