-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdeploy.execute.services.ts
More file actions
178 lines (160 loc) · 4.43 KB
/
deploy.execute.services.ts
File metadata and controls
178 lines (160 loc) · 4.43 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {nonNullish} from '@dfinity/utils';
import type {
DeployParams,
DeployResult,
DeployResultWithProposal,
UploadFileStorage
} from '@junobuild/cli-tools';
import {postDeploy as cliPostDeploy, preDeploy as cliPreDeploy} from '@junobuild/cli-tools';
import type {SatelliteConfig} from '@junobuild/config';
import {type Asset} from '@junobuild/core';
import {red} from 'kleur';
import {lstatSync} from 'node:fs';
import {
type DeployFnParams,
type DeployOptions,
type UploadFileFnParams,
type UploadFn,
type UploadInput,
type UploadParams
} from '../../../types/deploy';
import type {SatelliteParametersWithId} from '../../../types/satellite';
import {assertConfigAndLoadSatelliteContext} from '../../../utils/juno.config.utils';
import {assertSatelliteMemorySize} from '../../assert.services';
import {listAssets} from '../_assets.list.services';
export const executeDeployWithProposal = async ({
options,
...rest
}: {
options: DeployOptions;
} & UploadFn<UploadFileStorage, DeployResultWithProposal>): Promise<DeployResultWithProposal> => {
return await executeDeploy<UploadFileStorage, DeployResultWithProposal>({
options,
...rest
});
};
export const executeDeployImmediate = async ({
deployFn,
uploadFn,
options
}: {
deployFn: (params: DeployFnParams) => Promise<DeployResult>;
uploadFn: (params: UploadFileFnParams) => Promise<void>;
options: DeployOptions;
}): Promise<DeployResult> => {
return await executeDeploy<UploadFileStorage, DeployResult>({
deployFn,
uploadFn,
options,
method: 'individual'
});
};
const executeDeploy = async <
P extends UploadFileStorage,
R extends DeployResult | DeployResultWithProposal
>({
options: {deprecatedGzip, ...restOptions},
...rest
}: {
options: DeployOptions;
} & UploadFn<P, R>): Promise<R> => {
const {satellite, satelliteConfig: satelliteConfigRead} =
await assertConfigAndLoadSatelliteContext();
/** @deprecated */
const precompress =
satelliteConfigRead.precompress ??
(nonNullish(deprecatedGzip)
? {
pattern: deprecatedGzip
}
: undefined);
const satelliteConfig: SatelliteConfig = {
...satelliteConfigRead,
...(nonNullish(precompress) && {precompress})
};
await cliPreDeploy({config: satelliteConfig.hosting ?? satelliteConfig});
const result = await deployWithMethod<P, R>({
...rest,
...restOptions,
satellite,
satelliteConfig
});
if (result.result === 'skipped') {
process.exit(0);
}
await cliPostDeploy({config: satelliteConfig.hosting ?? satelliteConfig});
return result;
};
const deployWithMethod = async <
P extends UploadFileStorage,
R extends DeployResult | DeployResultWithProposal
>({
deployFn,
uploadFn,
method,
uploadBatchSize,
satellite,
satelliteConfig
}: {
satellite: SatelliteParametersWithId;
satelliteConfig: SatelliteConfig;
} & Omit<DeployOptions, 'deprecatedGzip'> &
UploadFn<P, R>): Promise<R> => {
const assertMemory = async () => {
await assertSatelliteMemorySize();
};
const listExistingAssets = async ({startAfter}: {startAfter?: string}): Promise<Asset[]> =>
await listAssets({
startAfter,
satellite
});
const deployParams: DeployParams = {
config: satelliteConfig.hosting ?? satelliteConfig,
listAssets: listExistingAssets,
assertSourceDirExists,
assertMemory,
uploadBatchSize
};
if (method === 'batch') {
const uploadFiles = async (params: UploadInput<{files: P[]}, R>) => {
const paramsWithSatellite: UploadParams<{files: P[]}, R> = {
...params,
satellite
};
await uploadFn(paramsWithSatellite);
};
return await deployFn({
deploy: {
params: deployParams,
upload: uploadFiles
},
satellite
});
}
const uploadFile = async (params: UploadInput<P, R>) => {
const paramsWithSatellite: UploadParams<P, R> = {
...params,
satellite
};
await uploadFn(paramsWithSatellite);
};
return await deployFn({
deploy: {
params: deployParams,
upload: uploadFile
},
satellite
});
};
const assertSourceDirExists = (source: string) => {
try {
lstatSync(source);
} catch (_err: unknown) {
console.log(
`${red(
'Cannot proceed deployment.'
)}\nAre you sure the folder containing your built app files is correctly configured (in your juno.config "hosting > source"), or have you built your app?`
);
process.exit(1);
}
};