Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Extract compiler from compile command #114

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"ipfs-http-client": "^29.1.1",
"is-git-url": "^1.0.0",
"js-yaml": "^3.13.1",
"lodash.pick": "^4.4.0",
"mesg-js": "^4.1.0",
"node-docker-api": "^1.1.22",
"rimraf": "^2.6.3",
Expand All @@ -45,6 +46,7 @@
"@types/hosted-git-info": "^2.7.0",
"@types/inquirer": "0.0.43",
"@types/is-git-url": "^1.0.0",
"@types/lodash.pick": "^4.4.6",
"@types/mocha": "^5.2.6",
"@types/node": "^10.14.6",
"@types/rimraf": "^2.0.2",
Expand Down
32 changes: 3 additions & 29 deletions src/commands/service/compile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {readFileSync} from 'fs'
import yaml from 'js-yaml'
import {Service} from 'mesg-js/lib/api'
import {join} from 'path'

import {WithoutPassphrase} from '../../account-command'
import MarketplaceCommand from '../../marketplace-command'
import Command from '../../root-command'
import compile from '../../utils/compiler'
import deployer, {createTar} from '../../utils/deployer'

const ipfsClient = require('ipfs-http-client')
Expand All @@ -29,39 +29,13 @@ export default class ServiceCompile extends Command {
const {args} = this.parse(ServiceCompile)
this.spinner.status = 'Download sources'
const path = await deployer(await this.processUrl(args.SERVICE))
const source = await this.deploySources(path)
const definition = this.parseYml(readFileSync(join(path, 'mesg.yml')).toString(), source)
const definition = await compile(readFileSync(join(path, 'mesg.yml')))
definition.source = await this.deploySources(path)
this.styledJSON(definition)
this.spinner.stop()
return definition
}

parseYml(content: string, source: string): Service {
const o = yaml.safeLoad(content)
const parseParams = (params: any): any => Object.keys(params || {})
.map((key: string) => {
const {name, description, type, repeated, optional, object} = params[key]
return {key, name, description, type, repeated, optional, object: parseParams(object || {})}
})
return {
sid: o.sid,
name: o.name,
description: o.description,
tasks: Object.keys(o.tasks || {}).map((key: string) => {
const {name, description, inputs, outputs} = o.tasks[key]
return {key, name, description, inputs: parseParams(inputs), outputs: parseParams(outputs)}
}),
events: Object.keys(o.events || {}).map((key: string) => {
const {name, description, data} = o.events[key]
return {key, name, description, data: parseParams(data)}
}),
dependencies: Object.keys(o.dependencies || {}).map((key: string) => ({key, ...o.dependencies[key]})),
configuration: o.configuration,
repository: o.repository,
source
}
}

async getAuthorizedServiceInfo(id: string, versionHash: string): Promise<{ sid: string, source: string, type: string }> {
const {addresses} = await this.execute({
instanceHash: await this.engineServiceInstance(WithoutPassphrase.SERVICE_NAME),
Expand Down
31 changes: 31 additions & 0 deletions src/utils/compiler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import yaml from 'js-yaml'
import pick from 'lodash.pick'

const decode = (content: Buffer) => yaml.safeLoad(content.toString())

const mapToArray = (inputs: any) => Object.keys(inputs || {}).map(key => ({
...inputs[key],
key
}))

const parseParams = (params: any): any => mapToArray(params).map(x => ({
...pick(x, ['key', 'name', 'description', 'type', 'repeated', 'optional']),
object: parseParams(x.object),
}))

export default async (content: Buffer): Promise<any> => {
const definition = decode(content)
return {
...pick(definition, ['sid', 'name', 'description', 'configuration', 'repository']),
dependencies: mapToArray(definition.dependencies),
tasks: mapToArray(definition.tasks).map(x => ({
...pick(x, ['key', 'name', 'description']),
inputs: parseParams(x.inputs),
outputs: parseParams(x.outputs),
})),
events: mapToArray(definition.events).map(x => ({
...pick(x, ['key', 'name', 'description']),
data: parseParams(x.data)
})),
}
}