@secretstartups/workshop 0.5.0 → 0.6.0

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.
@@ -0,0 +1,658 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.SandboxModule = exports.SandboxCommand = exports.SandboxDeployCommand = exports.SandboxDownCommand = exports.SandboxTemplatesCommand = exports.SandboxSaveCommand = exports.SandboxUnpinCommand = exports.SandboxPinCommand = exports.SandboxVarCommand = exports.SandboxSetCommand = exports.SandboxListCommand = exports.SandboxUpCommand = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const nest_commander_1 = require("nest-commander");
18
+ const chalk_1 = __importDefault(require("chalk"));
19
+ const ora_1 = __importDefault(require("ora"));
20
+ const api_service_1 = require("../common/api/api.service");
21
+ const cli_config_service_1 = require("../common/config/cli-config.service");
22
+ const output_service_1 = require("../common/output/output.service");
23
+ const sandbox_spec_1 = require("./sandbox-spec");
24
+ const WAIT_LIMIT_MS = 20 * 60_000;
25
+ const rows = (data) => (Array.isArray(data) ? data : (data?.data ?? []));
26
+ let SandboxSub = class SandboxSub extends nest_commander_1.CommandRunner {
27
+ constructor(api, config, output) {
28
+ super();
29
+ this.api = api;
30
+ this.config = config;
31
+ this.output = output;
32
+ }
33
+ org() {
34
+ const orgId = this.config.getCurrentOrgId();
35
+ if (!orgId)
36
+ throw new Error('No organization selected. Run `workshop org use <id>` first.');
37
+ return orgId;
38
+ }
39
+ project(given) {
40
+ const id = given ?? this.config.getCurrentProjectId();
41
+ if (!id)
42
+ throw new Error('No project: pass --project or run `workshop project use <id>`.');
43
+ return id;
44
+ }
45
+ base() {
46
+ return `/organizations/${this.org()}/sandboxes`;
47
+ }
48
+ previews() {
49
+ return `/organizations/${this.org()}/previews`;
50
+ }
51
+ async services(projectId) {
52
+ return rows(await this.api.get('/services', { projectId }));
53
+ }
54
+ async environments(projectId) {
55
+ return rows(await this.api.get('/environments', { projectId }));
56
+ }
57
+ async specs(projectId, args = []) {
58
+ if (!args.length)
59
+ return [];
60
+ const services = await this.services(projectId);
61
+ return args.map((a) => ({ serviceId: (0, sandbox_spec_1.pick)(services, a.service, 'service').id, source: a.source }));
62
+ }
63
+ show(p) {
64
+ this.output.keyValue({
65
+ Sandbox: p.id,
66
+ Name: p.sandbox?.name ?? p.branch,
67
+ Status: colour(p.status),
68
+ Open: p.openUrl,
69
+ Expires: p.sandbox?.pinned ? 'never (kept alive)' : new Date(p.expiresAt).toLocaleString(),
70
+ });
71
+ for (const note of p.notes ?? [])
72
+ this.output.warn(note);
73
+ if (p.services.length) {
74
+ this.output.table(['Service', 'Runs', 'State', 'Address', 'Error'], p.services.map((s) => [
75
+ s.name,
76
+ s.imageSource === 'pinned' ? `image ${s.imageTag}` : s.role === 'carried' ? 'base image' : (s.ref ?? ''),
77
+ colour(s.state),
78
+ s.url,
79
+ s.error ? s.error.split('\n')[0].slice(0, 80) : null,
80
+ ]));
81
+ }
82
+ }
83
+ async waitFor(id, json) {
84
+ const spinner = json ? null : (0, ora_1.default)('Spinning up the sandbox…').start();
85
+ const deadline = Date.now() + WAIT_LIMIT_MS;
86
+ try {
87
+ for (;;) {
88
+ const p = await this.api.get(`${this.previews()}/${id}/wait`, { timeoutSec: 100 }, { timeout: 130_000 });
89
+ if (spinner)
90
+ spinner.text = p.summary;
91
+ if (p.settled || Date.now() > deadline)
92
+ return p;
93
+ }
94
+ }
95
+ finally {
96
+ spinner?.stop();
97
+ }
98
+ }
99
+ fail(err) {
100
+ this.output.error(err.message);
101
+ process.exitCode = 1;
102
+ }
103
+ };
104
+ SandboxSub = __decorate([
105
+ (0, common_1.Injectable)(),
106
+ __metadata("design:paramtypes", [api_service_1.ApiService,
107
+ cli_config_service_1.CliConfigService,
108
+ output_service_1.OutputService])
109
+ ], SandboxSub);
110
+ let SandboxUpCommand = class SandboxUpCommand extends SandboxSub {
111
+ async run([name], o) {
112
+ try {
113
+ const projectId = this.project(o.project);
114
+ const body = {
115
+ projectId,
116
+ name,
117
+ services: await this.specs(projectId, o.service),
118
+ ttlHours: o.ttl,
119
+ mode: o.shared ? 'shared' : 'isolated',
120
+ };
121
+ if (o.template) {
122
+ const templates = rows(await this.api.get(`/organizations/${this.org()}/projects/${projectId}/sandbox-templates`));
123
+ body.templateId = (0, sandbox_spec_1.pick)(templates, o.template, 'template').id;
124
+ }
125
+ if (o.fromEnv || o.base) {
126
+ const envs = await this.environments(projectId);
127
+ if (o.fromEnv)
128
+ body.cloneEnvironmentId = (0, sandbox_spec_1.pick)(envs, o.fromEnv, 'environment').id;
129
+ if (o.base)
130
+ body.baseEnvironmentId = (0, sandbox_spec_1.pick)(envs, o.base, 'environment').id;
131
+ }
132
+ let p = await this.api.post(this.base(), body);
133
+ if (o.wait)
134
+ p = await this.waitFor(p.id, o.json);
135
+ if (o.json)
136
+ return this.output.json(p);
137
+ this.show(p);
138
+ if (!o.wait)
139
+ this.output.info(`Starting. \`workshop preview status ${p.id} --wait\` to follow it.`);
140
+ else if (p.status === 'ready')
141
+ this.output.success(`Ready: ${p.openUrl}`);
142
+ else if (p.status === 'failed') {
143
+ this.output.fail(`It did not come up. \`workshop preview logs ${p.id}\` says why.`);
144
+ process.exitCode = 1;
145
+ }
146
+ }
147
+ catch (err) {
148
+ this.fail(err);
149
+ }
150
+ }
151
+ parseProject(v) {
152
+ return v;
153
+ }
154
+ parseService(v, prev = []) {
155
+ return [...prev, (0, sandbox_spec_1.parseServiceSpec)(v)];
156
+ }
157
+ parseTemplate(v) {
158
+ return v;
159
+ }
160
+ parseFromEnv(v) {
161
+ return v;
162
+ }
163
+ parseBase(v) {
164
+ return v;
165
+ }
166
+ parseTtl(v) {
167
+ return Number(v);
168
+ }
169
+ parseShared() {
170
+ return true;
171
+ }
172
+ parseWait() {
173
+ return true;
174
+ }
175
+ parseJson() {
176
+ return true;
177
+ }
178
+ };
179
+ exports.SandboxUpCommand = SandboxUpCommand;
180
+ __decorate([
181
+ (0, nest_commander_1.Option)({ flags: '-p, --project <projectId>', description: 'Project (default: the current one)' }),
182
+ __metadata("design:type", Function),
183
+ __metadata("design:paramtypes", [String]),
184
+ __metadata("design:returntype", void 0)
185
+ ], SandboxUpCommand.prototype, "parseProject", null);
186
+ __decorate([
187
+ (0, nest_commander_1.Option)({
188
+ flags: '-s, --service <service=source>',
189
+ description: 'A service and what it runs: api=base, api=feat/x, api=held:main, api=commit:main@abc1234, web=image:v1.4.2 (repeatable)',
190
+ }),
191
+ __metadata("design:type", Function),
192
+ __metadata("design:paramtypes", [String, Array]),
193
+ __metadata("design:returntype", void 0)
194
+ ], SandboxUpCommand.prototype, "parseService", null);
195
+ __decorate([
196
+ (0, nest_commander_1.Option)({ flags: '-t, --template <template>', description: 'Start from a saved template (name or id)' }),
197
+ __metadata("design:type", Function),
198
+ __metadata("design:paramtypes", [String]),
199
+ __metadata("design:returntype", void 0)
200
+ ], SandboxUpCommand.prototype, "parseTemplate", null);
201
+ __decorate([
202
+ (0, nest_commander_1.Option)({ flags: '--from-env <environment>', description: 'Clone what an environment runs (name or id)' }),
203
+ __metadata("design:type", Function),
204
+ __metadata("design:paramtypes", [String]),
205
+ __metadata("design:returntype", void 0)
206
+ ], SandboxUpCommand.prototype, "parseFromEnv", null);
207
+ __decorate([
208
+ (0, nest_commander_1.Option)({ flags: '--base <environment>', description: "What base images come from (default: the project's preview environment)" }),
209
+ __metadata("design:type", Function),
210
+ __metadata("design:paramtypes", [String]),
211
+ __metadata("design:returntype", void 0)
212
+ ], SandboxUpCommand.prototype, "parseBase", null);
213
+ __decorate([
214
+ (0, nest_commander_1.Option)({ flags: '--ttl <hours>', description: 'How long it lives, in hours' }),
215
+ __metadata("design:type", Function),
216
+ __metadata("design:paramtypes", [String]),
217
+ __metadata("design:returntype", void 0)
218
+ ], SandboxUpCommand.prototype, "parseTtl", null);
219
+ __decorate([
220
+ (0, nest_commander_1.Option)({ flags: '--shared', description: "Use the base environment's data instead of throwaway databases" }),
221
+ __metadata("design:type", Function),
222
+ __metadata("design:paramtypes", []),
223
+ __metadata("design:returntype", void 0)
224
+ ], SandboxUpCommand.prototype, "parseShared", null);
225
+ __decorate([
226
+ (0, nest_commander_1.Option)({ flags: '--wait', description: 'Wait until it is ready or fails' }),
227
+ __metadata("design:type", Function),
228
+ __metadata("design:paramtypes", []),
229
+ __metadata("design:returntype", void 0)
230
+ ], SandboxUpCommand.prototype, "parseWait", null);
231
+ __decorate([
232
+ (0, nest_commander_1.Option)({ flags: '--json', description: 'Output as JSON' }),
233
+ __metadata("design:type", Function),
234
+ __metadata("design:paramtypes", []),
235
+ __metadata("design:returntype", void 0)
236
+ ], SandboxUpCommand.prototype, "parseJson", null);
237
+ exports.SandboxUpCommand = SandboxUpCommand = __decorate([
238
+ (0, nest_commander_1.SubCommand)({
239
+ name: 'up',
240
+ arguments: '<name>',
241
+ description: 'Spin up a sandbox: services at the versions you pick, a clone of an environment (--from-env), or a template (--template)',
242
+ })
243
+ ], SandboxUpCommand);
244
+ let SandboxListCommand = class SandboxListCommand extends SandboxSub {
245
+ async run(_a, o) {
246
+ try {
247
+ const list = await this.api.get(this.base(), {
248
+ projectId: o.project ?? this.config.getCurrentProjectId(),
249
+ mine: o.mine ? 'true' : undefined,
250
+ });
251
+ if (o.json)
252
+ return this.output.json(list);
253
+ this.output.table(['ID', 'Name', 'Status', 'Open', 'Expires'], list.map((p) => [
254
+ p.id,
255
+ p.sandbox?.name ?? p.branch,
256
+ colour(p.status),
257
+ p.openUrl,
258
+ p.sandbox?.pinned ? 'never' : new Date(p.expiresAt).toLocaleString(),
259
+ ]));
260
+ }
261
+ catch (err) {
262
+ this.fail(err);
263
+ }
264
+ }
265
+ parseProject(v) {
266
+ return v;
267
+ }
268
+ parseMine() {
269
+ return true;
270
+ }
271
+ parseJson() {
272
+ return true;
273
+ }
274
+ };
275
+ exports.SandboxListCommand = SandboxListCommand;
276
+ __decorate([
277
+ (0, nest_commander_1.Option)({ flags: '-p, --project <projectId>', description: 'Project (default: the current one)' }),
278
+ __metadata("design:type", Function),
279
+ __metadata("design:paramtypes", [String]),
280
+ __metadata("design:returntype", void 0)
281
+ ], SandboxListCommand.prototype, "parseProject", null);
282
+ __decorate([
283
+ (0, nest_commander_1.Option)({ flags: '--mine', description: 'Only yours' }),
284
+ __metadata("design:type", Function),
285
+ __metadata("design:paramtypes", []),
286
+ __metadata("design:returntype", void 0)
287
+ ], SandboxListCommand.prototype, "parseMine", null);
288
+ __decorate([
289
+ (0, nest_commander_1.Option)({ flags: '--json', description: 'Output as JSON' }),
290
+ __metadata("design:type", Function),
291
+ __metadata("design:paramtypes", []),
292
+ __metadata("design:returntype", void 0)
293
+ ], SandboxListCommand.prototype, "parseJson", null);
294
+ exports.SandboxListCommand = SandboxListCommand = __decorate([
295
+ (0, nest_commander_1.SubCommand)({ name: 'ls', aliases: ['list'], description: 'Live sandboxes' })
296
+ ], SandboxListCommand);
297
+ let SandboxSetCommand = class SandboxSetCommand extends SandboxSub {
298
+ async run([id], o) {
299
+ try {
300
+ const projectId = this.project(o.project);
301
+ const services = await this.services(projectId);
302
+ const p = await this.api.patch(`${this.base()}/${id}/services`, {
303
+ set: await this.specs(projectId, o.service),
304
+ remove: (o.remove ?? []).map((r) => (0, sandbox_spec_1.pick)(services, r, 'service').id),
305
+ });
306
+ if (o.json)
307
+ return this.output.json(p);
308
+ this.show(p);
309
+ }
310
+ catch (err) {
311
+ this.fail(err);
312
+ }
313
+ }
314
+ parseProject(v) {
315
+ return v;
316
+ }
317
+ parseService(v, prev = []) {
318
+ return [...prev, (0, sandbox_spec_1.parseServiceSpec)(v)];
319
+ }
320
+ parseRemove(v, prev = []) {
321
+ return [...prev, v];
322
+ }
323
+ parseJson() {
324
+ return true;
325
+ }
326
+ };
327
+ exports.SandboxSetCommand = SandboxSetCommand;
328
+ __decorate([
329
+ (0, nest_commander_1.Option)({ flags: '-p, --project <projectId>', description: 'Project (default: the current one)' }),
330
+ __metadata("design:type", Function),
331
+ __metadata("design:paramtypes", [String]),
332
+ __metadata("design:returntype", void 0)
333
+ ], SandboxSetCommand.prototype, "parseProject", null);
334
+ __decorate([
335
+ (0, nest_commander_1.Option)({ flags: '-s, --service <service=source>', description: 'As for `up` (repeatable)' }),
336
+ __metadata("design:type", Function),
337
+ __metadata("design:paramtypes", [String, Array]),
338
+ __metadata("design:returntype", void 0)
339
+ ], SandboxSetCommand.prototype, "parseService", null);
340
+ __decorate([
341
+ (0, nest_commander_1.Option)({ flags: '--remove <service>', description: 'Take a service out (repeatable)' }),
342
+ __metadata("design:type", Function),
343
+ __metadata("design:paramtypes", [String, Array]),
344
+ __metadata("design:returntype", void 0)
345
+ ], SandboxSetCommand.prototype, "parseRemove", null);
346
+ __decorate([
347
+ (0, nest_commander_1.Option)({ flags: '--json', description: 'Output as JSON' }),
348
+ __metadata("design:type", Function),
349
+ __metadata("design:paramtypes", []),
350
+ __metadata("design:returntype", void 0)
351
+ ], SandboxSetCommand.prototype, "parseJson", null);
352
+ exports.SandboxSetCommand = SandboxSetCommand = __decorate([
353
+ (0, nest_commander_1.SubCommand)({
354
+ name: 'set',
355
+ arguments: '<sandboxId>',
356
+ description: 'Run services at other versions (--service), add them, or take them out (--remove)',
357
+ })
358
+ ], SandboxSetCommand);
359
+ let SandboxVarCommand = class SandboxVarCommand extends SandboxSub {
360
+ async run([id, service, pair], o) {
361
+ try {
362
+ const at = pair?.indexOf('=') ?? -1;
363
+ if (at <= 0)
364
+ throw new Error('Give the variable as NAME=value (NAME= removes it)');
365
+ const name = pair.slice(0, at);
366
+ const value = pair.slice(at + 1);
367
+ const p = await this.api.get(`${this.previews()}/${id}`);
368
+ const serviceId = (0, sandbox_spec_1.pick)(p.services.map((s) => ({ id: s.serviceId, name: s.name })), service, 'service in this sandbox').id;
369
+ const path = `${this.base()}/${id}/variables/${serviceId}/${encodeURIComponent(name)}`;
370
+ const after = value ? await this.api.put(path, { value }) : await this.api.delete(path);
371
+ if (o.json)
372
+ return this.output.json(after);
373
+ this.output.success(value ? `${name} set; ${service} is applied again.` : `${name} removed.`);
374
+ }
375
+ catch (err) {
376
+ this.fail(err);
377
+ }
378
+ }
379
+ parseJson() {
380
+ return true;
381
+ }
382
+ };
383
+ exports.SandboxVarCommand = SandboxVarCommand;
384
+ __decorate([
385
+ (0, nest_commander_1.Option)({ flags: '--json', description: 'Output as JSON' }),
386
+ __metadata("design:type", Function),
387
+ __metadata("design:paramtypes", []),
388
+ __metadata("design:returntype", void 0)
389
+ ], SandboxVarCommand.prototype, "parseJson", null);
390
+ exports.SandboxVarCommand = SandboxVarCommand = __decorate([
391
+ (0, nest_commander_1.SubCommand)({
392
+ name: 'var',
393
+ arguments: '<sandboxId> <service> <NAME=value>',
394
+ description: "Set a sandbox's own value for a variable (NAME= to remove it); never a credential",
395
+ })
396
+ ], SandboxVarCommand);
397
+ let SandboxPinCommand = class SandboxPinCommand extends SandboxSub {
398
+ async run([id]) {
399
+ try {
400
+ await this.api.post(`${this.base()}/${id}/pin`);
401
+ this.output.success('Kept alive until someone unpins or deletes it.');
402
+ }
403
+ catch (err) {
404
+ this.fail(err);
405
+ }
406
+ }
407
+ };
408
+ exports.SandboxPinCommand = SandboxPinCommand;
409
+ exports.SandboxPinCommand = SandboxPinCommand = __decorate([
410
+ (0, nest_commander_1.SubCommand)({ name: 'pin', arguments: '<sandboxId>', description: 'Keep a sandbox alive: it never expires' })
411
+ ], SandboxPinCommand);
412
+ let SandboxUnpinCommand = class SandboxUnpinCommand extends SandboxSub {
413
+ async run([id]) {
414
+ try {
415
+ const p = await this.api.delete(`${this.base()}/${id}/pin`);
416
+ this.output.success(`It expires ${new Date(p.expiresAt).toLocaleString()}.`);
417
+ }
418
+ catch (err) {
419
+ this.fail(err);
420
+ }
421
+ }
422
+ };
423
+ exports.SandboxUnpinCommand = SandboxUnpinCommand;
424
+ exports.SandboxUnpinCommand = SandboxUnpinCommand = __decorate([
425
+ (0, nest_commander_1.SubCommand)({ name: 'unpin', arguments: '<sandboxId>', description: 'Let a sandbox expire again' })
426
+ ], SandboxUnpinCommand);
427
+ let SandboxSaveCommand = class SandboxSaveCommand extends SandboxSub {
428
+ async run([id, name], o) {
429
+ try {
430
+ const t = await this.api.post(`${this.base()}/${id}/save-as-template`, { name });
431
+ if (o.json)
432
+ return this.output.json(t);
433
+ this.output.success(`Template ${t.name} saved (${t.id}). \`workshop sandbox up <name> --template "${t.name}"\` spins it up.`);
434
+ }
435
+ catch (err) {
436
+ this.fail(err);
437
+ }
438
+ }
439
+ parseJson() {
440
+ return true;
441
+ }
442
+ };
443
+ exports.SandboxSaveCommand = SandboxSaveCommand;
444
+ __decorate([
445
+ (0, nest_commander_1.Option)({ flags: '--json', description: 'Output as JSON' }),
446
+ __metadata("design:type", Function),
447
+ __metadata("design:paramtypes", []),
448
+ __metadata("design:returntype", void 0)
449
+ ], SandboxSaveCommand.prototype, "parseJson", null);
450
+ exports.SandboxSaveCommand = SandboxSaveCommand = __decorate([
451
+ (0, nest_commander_1.SubCommand)({
452
+ name: 'save',
453
+ arguments: '<sandboxId> <templateName>',
454
+ description: 'Save what a sandbox (or any preview) runs as a template',
455
+ })
456
+ ], SandboxSaveCommand);
457
+ let SandboxTemplatesCommand = class SandboxTemplatesCommand extends SandboxSub {
458
+ async run(_a, o) {
459
+ try {
460
+ const projectId = this.project(o.project);
461
+ const [templates, services] = await Promise.all([
462
+ this.api.get(`/organizations/${this.org()}/projects/${projectId}/sandbox-templates`),
463
+ this.services(projectId),
464
+ ]);
465
+ if (o.json)
466
+ return this.output.json(templates);
467
+ const nameOf = (sid) => services.find((s) => s.id === sid)?.name ?? sid;
468
+ this.output.table(['ID', 'Name', 'Services'], rows(templates).map((t) => [
469
+ t.id,
470
+ t.name,
471
+ t.services.map((s) => `${nameOf(s.serviceId)}: ${(0, sandbox_spec_1.describeSource)(s.source)}`).join(', '),
472
+ ]));
473
+ }
474
+ catch (err) {
475
+ this.fail(err);
476
+ }
477
+ }
478
+ parseProject(v) {
479
+ return v;
480
+ }
481
+ parseJson() {
482
+ return true;
483
+ }
484
+ };
485
+ exports.SandboxTemplatesCommand = SandboxTemplatesCommand;
486
+ __decorate([
487
+ (0, nest_commander_1.Option)({ flags: '-p, --project <projectId>', description: 'Project (default: the current one)' }),
488
+ __metadata("design:type", Function),
489
+ __metadata("design:paramtypes", [String]),
490
+ __metadata("design:returntype", void 0)
491
+ ], SandboxTemplatesCommand.prototype, "parseProject", null);
492
+ __decorate([
493
+ (0, nest_commander_1.Option)({ flags: '--json', description: 'Output as JSON' }),
494
+ __metadata("design:type", Function),
495
+ __metadata("design:paramtypes", []),
496
+ __metadata("design:returntype", void 0)
497
+ ], SandboxTemplatesCommand.prototype, "parseJson", null);
498
+ exports.SandboxTemplatesCommand = SandboxTemplatesCommand = __decorate([
499
+ (0, nest_commander_1.SubCommand)({ name: 'templates', description: "A project's sandbox templates" })
500
+ ], SandboxTemplatesCommand);
501
+ let SandboxDownCommand = class SandboxDownCommand extends SandboxSub {
502
+ async run([id]) {
503
+ try {
504
+ await this.api.delete(`${this.previews()}/${id}`);
505
+ this.output.success(`Deleting sandbox ${id}.`);
506
+ }
507
+ catch (err) {
508
+ this.fail(err);
509
+ }
510
+ }
511
+ };
512
+ exports.SandboxDownCommand = SandboxDownCommand;
513
+ exports.SandboxDownCommand = SandboxDownCommand = __decorate([
514
+ (0, nest_commander_1.SubCommand)({ name: 'down', arguments: '<sandboxId>', description: 'Delete a sandbox and everything it runs' })
515
+ ], SandboxDownCommand);
516
+ let SandboxDeployCommand = class SandboxDeployCommand extends SandboxSub {
517
+ async run([environment], o) {
518
+ try {
519
+ const projectId = this.project(o.project);
520
+ const [services, envs] = await Promise.all([this.services(projectId), this.environments(projectId)]);
521
+ const env = (0, sandbox_spec_1.pick)(envs, environment, 'environment');
522
+ const items = o.all
523
+ ? services.map((s) => ({ serviceId: s.id }))
524
+ : (o.service ?? []).map((v) => {
525
+ const at = v.indexOf('=');
526
+ const name = at > 0 ? v.slice(0, at) : v;
527
+ const tag = at > 0 ? v.slice(at + 1).replace(/^image:/, '') : '';
528
+ return { serviceId: (0, sandbox_spec_1.pick)(services, name, 'service').id, ...(tag ? { imageTag: tag } : {}) };
529
+ });
530
+ if (!items.length)
531
+ throw new Error('Name services with --service (repeatable), or pass --all');
532
+ const res = await this.api.post(`/projects/${projectId}/deployments`, { environmentId: env.id, items, confirm: o.confirm });
533
+ if (o.json)
534
+ return this.output.json(res);
535
+ this.output.table(['Service', 'Deploy', 'Why'], res.results.map((r) => [r.serviceName, colour(r.state === 'started' ? 'deploying' : r.state), r.error ?? '']));
536
+ if (res.results.some((r) => r.state !== 'started'))
537
+ process.exitCode = 1;
538
+ }
539
+ catch (err) {
540
+ this.fail(err);
541
+ }
542
+ }
543
+ parseProject(v) {
544
+ return v;
545
+ }
546
+ parseService(v, prev = []) {
547
+ return [...prev, v];
548
+ }
549
+ parseAll() {
550
+ return true;
551
+ }
552
+ parseConfirm(v) {
553
+ return v;
554
+ }
555
+ parseJson() {
556
+ return true;
557
+ }
558
+ };
559
+ exports.SandboxDeployCommand = SandboxDeployCommand;
560
+ __decorate([
561
+ (0, nest_commander_1.Option)({ flags: '-p, --project <projectId>', description: 'Project (default: the current one)' }),
562
+ __metadata("design:type", Function),
563
+ __metadata("design:paramtypes", [String]),
564
+ __metadata("design:returntype", void 0)
565
+ ], SandboxDeployCommand.prototype, "parseProject", null);
566
+ __decorate([
567
+ (0, nest_commander_1.Option)({ flags: '-s, --service <service[=tag]>', description: 'A service, optionally at an image tag (repeatable)' }),
568
+ __metadata("design:type", Function),
569
+ __metadata("design:paramtypes", [String, Array]),
570
+ __metadata("design:returntype", void 0)
571
+ ], SandboxDeployCommand.prototype, "parseService", null);
572
+ __decorate([
573
+ (0, nest_commander_1.Option)({ flags: '--all', description: "Every service of the project" }),
574
+ __metadata("design:type", Function),
575
+ __metadata("design:paramtypes", []),
576
+ __metadata("design:returntype", void 0)
577
+ ], SandboxDeployCommand.prototype, "parseAll", null);
578
+ __decorate([
579
+ (0, nest_commander_1.Option)({ flags: '--confirm <environmentName>', description: 'For a production environment: its name' }),
580
+ __metadata("design:type", Function),
581
+ __metadata("design:paramtypes", [String]),
582
+ __metadata("design:returntype", void 0)
583
+ ], SandboxDeployCommand.prototype, "parseConfirm", null);
584
+ __decorate([
585
+ (0, nest_commander_1.Option)({ flags: '--json', description: 'Output as JSON' }),
586
+ __metadata("design:type", Function),
587
+ __metadata("design:paramtypes", []),
588
+ __metadata("design:returntype", void 0)
589
+ ], SandboxDeployCommand.prototype, "parseJson", null);
590
+ exports.SandboxDeployCommand = SandboxDeployCommand = __decorate([
591
+ (0, nest_commander_1.SubCommand)({
592
+ name: 'deploy',
593
+ arguments: '<environment>',
594
+ description: "Deploy several of a project's services to an environment, each after what it requires (images only; use a sandbox for branches)",
595
+ })
596
+ ], SandboxDeployCommand);
597
+ let SandboxCommand = class SandboxCommand extends nest_commander_1.CommandRunner {
598
+ async run() {
599
+ console.log('Usage: workshop sandbox <subcommand>\n');
600
+ console.log(' up <name> [-s api=feat/x] [--from-env staging | --template t] [--wait] Spin one up');
601
+ console.log(' ls [--mine] Live sandboxes');
602
+ console.log(' set <id> [-s web=image:v2] [--remove api] Change its services');
603
+ console.log(' var <id> <service> NAME=value Its own value for a variable');
604
+ console.log(' pin <id> | unpin <id> Keep it alive, or let it expire');
605
+ console.log(' save <id> <template> Save it as a template');
606
+ console.log(' templates Saved templates');
607
+ console.log(' down <id> Delete it');
608
+ console.log(' deploy <environment> -s api=v2 -s web Deploy several services to an environment');
609
+ console.log(`\nOnce up, a sandbox is a preview: ${chalk_1.default.bold('workshop preview status|logs|extend|share <id>')} work on it.`);
610
+ }
611
+ };
612
+ exports.SandboxCommand = SandboxCommand;
613
+ exports.SandboxCommand = SandboxCommand = __decorate([
614
+ (0, nest_commander_1.Command)({
615
+ name: 'sandbox',
616
+ description: "Ephemeral environments of a project's services, at any version, for testing or anything else",
617
+ subCommands: [
618
+ SandboxUpCommand,
619
+ SandboxListCommand,
620
+ SandboxSetCommand,
621
+ SandboxVarCommand,
622
+ SandboxPinCommand,
623
+ SandboxUnpinCommand,
624
+ SandboxSaveCommand,
625
+ SandboxTemplatesCommand,
626
+ SandboxDownCommand,
627
+ SandboxDeployCommand,
628
+ ],
629
+ })
630
+ ], SandboxCommand);
631
+ let SandboxModule = class SandboxModule {
632
+ };
633
+ exports.SandboxModule = SandboxModule;
634
+ exports.SandboxModule = SandboxModule = __decorate([
635
+ (0, common_1.Module)({
636
+ providers: [
637
+ SandboxCommand,
638
+ SandboxUpCommand,
639
+ SandboxListCommand,
640
+ SandboxSetCommand,
641
+ SandboxVarCommand,
642
+ SandboxPinCommand,
643
+ SandboxUnpinCommand,
644
+ SandboxSaveCommand,
645
+ SandboxTemplatesCommand,
646
+ SandboxDownCommand,
647
+ SandboxDeployCommand,
648
+ ],
649
+ })
650
+ ], SandboxModule);
651
+ function colour(state) {
652
+ if (state === 'ready' || state === 'started')
653
+ return chalk_1.default.green(state);
654
+ if (state === 'failed' || state === 'blocked')
655
+ return chalk_1.default.red(state);
656
+ return chalk_1.default.yellow(state);
657
+ }
658
+ //# sourceMappingURL=sandbox.commands.js.map