-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
63 lines (55 loc) · 1.9 KB
/
Copy pathindex.ts
File metadata and controls
63 lines (55 loc) · 1.9 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
import { spawn } from 'child_process';
import {streamWrite, streamEnd, onExit} from '@rauschma/stringio';
export async function dsLogin (apiKey: string, username: string) {
const child = spawn('ds', ['login', username], {stdio: ['pipe', process.stdout, process.stderr]});
if(child.stdin != null) {
await streamWrite(child.stdin, `${apiKey}\n`)
if(child.stdout != null) {
child.stdout.on('data', (data: object) => {
console.log(data.toString());
});
}
}
}
function execPromise (command: string, args: Array<string>) : Promise<Array<string>> {
return new Promise((resolve, reject) => {
const child = spawn(command, args)
let output: Array<string> = [];
let err: Array<string> = [];
child.stdout.on('data', (data) => {
output.push(data)
})
child.stderr.on('data', (data) => {
err.push(data)
})
child.on('close', (code) => {
if (code !== 0)
{
console.error(`Command execution failed with code: ${code}`)
reject(err)
}
else {
console.log(`Command execution completed with code: ${code}`)
resolve(output)
}
})
})
}
/**
* @Method: Runs a Dotscience command task.
* @Param {string}
* @Return {string}
*/
export async function dsRun (apiKey: string, username: string, hostname: string, project: string, command: string, image: string) : Promise<Array<string>> {
// set the url first - if blank, don't set it
if(hostname != "") {
let hnameOutput: Array<string> = await execPromise("ds", ['set', 'server-url', hostname])
console.log(hnameOutput.join("\n"))
}
// login
await dsLogin(apiKey, username)
// run ds run!
let output: Array<string> = await execPromise("ds", ['run', '-v', '-p', project, '-I', image, '--', 'bash', '-c', command])
console.log(`finished command: ds run -v -p ${project} -I ${image} -- bash -c ${command}`)
return output
}