From 501b78f14b9a83ad1a1ce1207b96ed2c0c1a6fa0 Mon Sep 17 00:00:00 2001 From: Anthony ESTEBE Date: Mon, 22 Jul 2019 17:59:14 +0700 Subject: [PATCH] refactor(compilation): extract compiler from compile command --- package-lock.json | 42 ++++++++++++++++++++++----------- package.json | 2 ++ src/commands/service/compile.ts | 32 +++---------------------- src/utils/compiler.ts | 31 ++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 src/utils/compiler.ts diff --git a/package-lock.json b/package-lock.json index d11cab1..75a60a5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -637,6 +637,15 @@ "integrity": "sha512-ORj7IBWj13iYufXt/VXrCNMbUuCTJfhzme5kx9U/UtcIPdJYuvPDUAlHlbNhz/8lKCLy9XGIZnGrqXOtQbPGoQ==", "dev": true }, + "@types/lodash.pick": { + "version": "4.4.6", + "resolved": "https://registry.npmjs.org/@types/lodash.pick/-/lodash.pick-4.4.6.tgz", + "integrity": "sha512-u8bzA16qQ+8dY280z3aK7PoWb3fzX5ATJ0rJB6F+uqchOX2VYF02Aqa+8aYiHiHgPzQiITqCgeimlyKFy4OA6g==", + "dev": true, + "requires": { + "@types/lodash": "*" + } + }, "@types/long": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.0.tgz", @@ -2666,9 +2675,9 @@ "dev": true }, "grpc": { - "version": "1.21.1", - "resolved": "https://registry.npmjs.org/grpc/-/grpc-1.21.1.tgz", - "integrity": "sha512-PFsZQazf62nP05a0xm23mlImMuw5oVlqF/0zakmsdqJgvbABe+d6VThY2PfhqJmWEL/FhQ6QNYsxS5EAM6++7g==", + "version": "1.22.2", + "resolved": "https://registry.npmjs.org/grpc/-/grpc-1.22.2.tgz", + "integrity": "sha512-gaK59oAA5/mlOIn+hQO5JROPoAzsaGRpEMcrAayW5WGETS8QScpBoQ+XBxEWAAF0kbeGIELuGRCVEObKS1SLmw==", "requires": { "lodash.camelcase": "^4.3.0", "lodash.clone": "^4.5.0", @@ -2742,7 +2751,7 @@ "bundled": true }, "fs-minipass": { - "version": "1.2.5", + "version": "1.2.6", "bundled": true, "requires": { "minipass": "^2.2.1" @@ -2851,23 +2860,23 @@ } }, "needle": { - "version": "2.3.1", + "version": "2.4.0", "bundled": true, "requires": { - "debug": "^4.1.0", + "debug": "^3.2.6", "iconv-lite": "^0.4.4", "sax": "^1.2.4" }, "dependencies": { "debug": { - "version": "4.1.1", + "version": "3.2.6", "bundled": true, "requires": { "ms": "^2.1.1" } }, "ms": { - "version": "2.1.1", + "version": "2.1.2", "bundled": true } } @@ -2954,7 +2963,7 @@ "bundled": true }, "process-nextick-args": { - "version": "2.0.0", + "version": "2.0.1", "bundled": true }, "protobufjs": { @@ -3033,7 +3042,7 @@ "bundled": true }, "signal-exit": { - "version": "3.0.2", + "version": "3.0.1", "bundled": true }, "string-width": { @@ -3064,16 +3073,16 @@ "bundled": true }, "tar": { - "version": "4.4.8", + "version": "4.4.10", "bundled": true, "requires": { "chownr": "^1.1.1", "fs-minipass": "^1.2.5", - "minipass": "^2.3.4", - "minizlib": "^1.1.1", + "minipass": "^2.3.5", + "minizlib": "^1.2.1", "mkdirp": "^0.5.0", "safe-buffer": "^5.1.2", - "yallist": "^3.0.2" + "yallist": "^3.0.3" } }, "util-deprecate": { @@ -4073,6 +4082,11 @@ "integrity": "sha1-+wMJF/hqMTTlvJvsDWngAT3f7bI=", "dev": true }, + "lodash.pick": { + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/lodash.pick/-/lodash.pick-4.4.0.tgz", + "integrity": "sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=" + }, "lodash.template": { "version": "4.4.0", "resolved": "https://registry.npmjs.org/lodash.template/-/lodash.template-4.4.0.tgz", diff --git a/package.json b/package.json index 7f095b4..08f3245 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/commands/service/compile.ts b/src/commands/service/compile.ts index 40f723f..0b943c7 100644 --- a/src/commands/service/compile.ts +++ b/src/commands/service/compile.ts @@ -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') @@ -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), diff --git a/src/utils/compiler.ts b/src/utils/compiler.ts new file mode 100644 index 0000000..3ac70b3 --- /dev/null +++ b/src/utils/compiler.ts @@ -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 => { + 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) + })), + } +}