-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.page.ts
More file actions
221 lines (179 loc) · 5.71 KB
/
Copy pathcli.page.ts
File metadata and controls
221 lines (179 loc) · 5.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import {assertNonNullish, notEmptyString} from '@dfinity/utils';
import type {PrincipalText} from '@dfinity/zod-schemas';
import {execute, spawn} from '@junobuild/cli-tools';
import {statSync} from 'node:fs';
import {readdir, readFile, writeFile} from 'node:fs/promises';
import {join} from 'node:path';
const DEV = (process.env.NODE_ENV ?? 'production') === 'development';
const JUNO_CONFIG = join(process.cwd(), 'juno.config.ts');
const JUNO_TEST_ARGS = ['--mode', 'development', '--headless'];
const {command: JUNO_CMD, args: JUNO_CDM_ARGS} = DEV
? {command: 'node', args: ['dist/index.js']}
: {command: 'juno', args: []};
const buildArgs = (args: string[]): string[] => [...JUNO_CDM_ARGS, ...args, ...JUNO_TEST_ARGS];
export interface CliPageParams {
satelliteId: PrincipalText;
}
export class CliPage {
#satelliteId: PrincipalText;
private constructor({satelliteId}: CliPageParams) {
this.#satelliteId = satelliteId;
}
static initWithoutLogin(params: CliPageParams): CliPage {
return new CliPage(params);
}
static async initWithEmulatorLogin(params: CliPageParams): Promise<CliPage> {
const cliPage = new CliPage(params);
await cliPage.initConfig();
await cliPage.loginWithEmulator();
await cliPage.applyConfig();
return cliPage;
}
protected async initConfig(): Promise<void> {
let content = await readFile(JUNO_CONFIG, 'utf-8');
content = content.replace('<DEV_SATELLITE_ID>', this.#satelliteId);
await writeFile(JUNO_CONFIG, content, 'utf-8');
}
private async revertConfig(): Promise<void> {
let content = await readFile(JUNO_CONFIG, 'utf-8');
content = content.replace(this.#satelliteId, '<DEV_SATELLITE_ID>');
await writeFile(JUNO_CONFIG, content, 'utf-8');
}
async toggleSatelliteId({satelliteId}: {satelliteId: PrincipalText}): Promise<void> {
await this.revertConfig();
this.#satelliteId = satelliteId;
await this.initConfig();
}
protected async loginWithEmulator(): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['login', '--emulator'])
});
}
async applyConfig(): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['config', 'apply', '--force'])
});
}
private async logout(): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['logout'])
});
}
async clearHosting(): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['hosting', 'clear'])
});
}
async deployHosting({clear}: {clear: boolean}): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['hosting', 'deploy', ...(clear ? ['--clear'] : [])])
});
}
async createSnapshot({
target
}: {
target: 'satellite' | 'orbiter' | 'mission-control';
}): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['snapshot', 'create', '--target', target])
});
}
async restoreSnapshot({
target
}: {
target: 'satellite' | 'orbiter' | 'mission-control';
}): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['snapshot', 'restore', '--target', target])
});
}
async deleteSnapshot({
target
}: {
target: 'satellite' | 'orbiter' | 'mission-control';
}): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['snapshot', 'delete', '--target', target])
});
}
async downloadSnapshot({
target
}: {
target: 'satellite' | 'orbiter' | 'mission-control';
}): Promise<{snapshotFolder: string}> {
await execute({
command: JUNO_CMD,
args: buildArgs(['snapshot', 'download', '--target', target])
});
return await this.getSnapshotFsFolder();
}
// Retrieve where the snapshot was created
async getSnapshotFsFolder(): Promise<{snapshotFolder: string}> {
const snapshotsFolder = join(process.cwd(), '.snapshots');
const folders = await readdir(snapshotsFolder, {withFileTypes: true});
const [snapshotFolder] = folders
.filter((d) => d.isDirectory())
.map(({name}) => {
const path = join(snapshotsFolder, name);
const {birthtimeMs: time} = statSync(path);
return {path, time};
})
.sort((a, b) => b.time - a.time);
assertNonNullish(snapshotFolder);
return {snapshotFolder: snapshotFolder.path};
}
async uploadSnapshot({
target,
folder
}: {
target: 'satellite' | 'orbiter' | 'mission-control';
folder: string;
}): Promise<void> {
await execute({
command: JUNO_CMD,
args: buildArgs(['snapshot', 'upload', '--target', target, '--dir', folder])
});
}
async listSnapshot({
target
}: {
target: 'satellite' | 'orbiter' | 'mission-control';
}): Promise<{snapshotId: string | undefined}> {
let output = '';
await spawn({
command: JUNO_CMD,
args: buildArgs(['snapshot', 'list', '--target', target]),
stdout: (o) => (output += o),
silentErrors: true
});
const [_, snapshotId] = output.split('Snapshot found:');
return {snapshotId: notEmptyString(snapshotId) ? snapshotId.trim() : undefined};
}
async whoami(): Promise<{accessKey: string}> {
let output = '';
await spawn({
command: JUNO_CMD,
args: buildArgs(['whoami']),
stdout: (o) => (output += o),
silentErrors: true
});
const [_, __, ___, text] = output.split(' ');
const [value] = text.split('\n');
const accessKey = value.replace('\x1B[32m', '').replace('\x1B[39m', '');
return {accessKey: accessKey.trim()};
}
async close({revertConfig}: {revertConfig: boolean} = {revertConfig: true}): Promise<void> {
if (revertConfig) {
await this.revertConfig();
}
await this.logout();
}
}