-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeploy.ts
More file actions
97 lines (81 loc) · 2.71 KB
/
deploy.ts
File metadata and controls
97 lines (81 loc) · 2.71 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
import {isNullish} from '@dfinity/utils';
import kleur from 'kleur';
import {modules} from '../modules/modules';
import {buildContext} from '../services/context.services';
import type {Module} from '../services/modules/module.services';
import type {CliContext} from '../types/context';
const {green, cyan, red} = kleur;
export const deploy = async (args?: string[]) => {
const context = await buildContext(args);
await deployModules({
context,
mods: modules,
installFn: installModulesParallel,
postInstallFn: postInstallModulesParallel
});
};
interface DeployModulesParams {
context: CliContext;
mods: Module[];
installFn: InstallModulesFn;
postInstallFn: PostInstallModulesFn;
}
type InstallModulesFn = (params: {context: CliContext; mods: Module[]}) => Promise<void>;
type PostInstallModulesFn = (params: {context: CliContext; mods: Module[]}) => Promise<void>;
const deployModules = async ({context, mods, installFn, postInstallFn}: DeployModulesParams) => {
// 1. Create canisters that do not exist yet
await Promise.all(
mods
.filter((mod) => mod.status(context) === undefined)
.map(async (mod) => {
await mod.prepare(context);
})
);
// 2. Split the canister already deployed and those which still need to be installed
const [deployed, rest] = mods.reduce(
([deployed, rest]: [Module[], Module[]], mod) => {
const ready = mod.isDeployed(context);
return [
[...deployed, ...(ready ? [mod] : [])],
// eslint-disable-next-line no-negated-condition
[...rest, ...(!ready ? [mod] : [])]
];
},
[[], []]
);
// 3. Print out the list of those already deployed
await Promise.all(
deployed.map(async (mod) => {
const id = mod.canisterId(context);
if (isNullish(id)) {
console.log(`⚠️ ${red(mod.name)} was not initialized. This is unexpected!`);
return;
}
console.log(`🆗 ${green(mod.name)} already exists. Skipping deployment. ID: ${cyan(id)}`);
})
);
// 4. Deploy / install code in the not yet populated canisters
await installFn({context, mods: rest});
// 5. Some canisters may require post installation configuration that needs to be executed only once like that dev-painful Governance canister
await postInstallFn({context, mods: rest});
};
const installModulesParallel = async ({context, mods}: {context: CliContext; mods: Module[]}) => {
await Promise.all(
mods.map(async (mod) => {
await mod.install(context);
})
);
};
const postInstallModulesParallel = async ({
context,
mods
}: {
context: CliContext;
mods: Module[];
}) => {
await Promise.all(
mods.map(async (mod) => {
await mod.postInstall(context);
})
);
};