This repository has been archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.ts
55 lines (46 loc) · 1.66 KB
/
compile.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
import {readFileSync} from 'fs'
import {Service} from 'mesg-js/lib/api/types'
import {join} from 'path'
import Command from '../../root-command'
import * as compile from '../../utils/compiler'
import deployer, {createTar} from '../../utils/deployer'
const ipfsClient = require('ipfs-http-client')
export default class ServiceCompile extends Command {
static description = 'Compile a service and upload its source to IPFS server'
static flags = {
...Command.flags
}
static args = [{
name: 'SERVICE',
description: 'Path or url of a service',
default: './'
}]
private readonly IPFS = ipfsClient('ipfs.app.mesg.com', '5001', {protocol: 'http'})
async run(): Promise<Service> {
const {args} = this.parse(ServiceCompile)
this.spinner.status = 'Download sources'
const path = await deployer(args.SERVICE)
const definition = await compile.service(readFileSync(join(path, 'mesg.yml')))
definition.source = await this.deploySources(path)
this.styledJSON(definition)
this.spinner.stop()
return definition
}
private async deploySources(path: string): Promise<string> {
const buffer: any[] = []
return new Promise<string>((resolve, reject) => {
createTar(join(path))
.on('data', (data: any) => buffer.push(...data))
.once('error', reject)
.on('end', async () => resolve(await this.upload(Buffer.from(buffer)))
)
})
}
private async upload(buffer: Buffer): Promise<string> {
const res = await this.IPFS.add(Buffer.from(buffer), {pin: true})
if (!res.length) {
throw new Error('pushing service manifest failed: run service:compile again')
}
return res[0].hash
}
}