-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun-command.ts
73 lines (63 loc) · 2.18 KB
/
run-command.ts
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
import { Gitpod } from '../src/client';
import { findMostUsedEnvironmentClass, waitForEnvironmentRunning } from '../src/lib/environment';
import { runCommand } from '../src/lib/automation';
import { EnvironmentSpec } from '../src/resources/environments/environments';
import { verifyContextUrl } from './scm-auth';
import { withCleanup } from './cleanup';
/**
* Examples:
* - yarn ts-node examples/run-command.ts 'echo "Hello World!"'
* - yarn ts-node examples/run-command.ts 'echo "Hello World!"' https://github.com/gitpod-io/empty
*/
async function main() {
const args = process.argv.slice(2);
if (args.length < 1) {
console.log('Usage: yarn ts-node examples/run-command.ts "<COMMAND>" [CONTEXT_URL]');
process.exit(1);
}
const command = args[0];
const contextUrl = args[1];
await withCleanup(async (disposables) => {
const client = new Gitpod({
logLevel: 'info',
});
const envClass = await findMostUsedEnvironmentClass(client);
if (!envClass) {
console.error('Error: No environment class found. Please create one first.');
process.exit(1);
}
console.log(`Found environment class: ${envClass.displayName} (${envClass.description})`);
const spec: EnvironmentSpec = {
desiredPhase: 'ENVIRONMENT_PHASE_RUNNING',
machine: { class: envClass.id },
};
if (contextUrl) {
await verifyContextUrl(client, contextUrl, envClass.runnerId);
spec.content = {
initializer: {
specs: [
{
contextUrl: {
url: contextUrl,
},
},
],
},
};
}
console.log('Creating environment');
const { environment } = await client.environments.create({ spec });
disposables.add(() => client.environments.delete({ environmentId: environment.id }));
console.log('Waiting for environment to be ready');
await waitForEnvironmentRunning(client, environment.id);
console.log('Running command');
const lines = await runCommand(client, environment.id, command!);
for await (const line of lines) {
console.log(line);
}
});
}
main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});