From f1dacdf3293d66ac147217704b75283f3e51c8e6 Mon Sep 17 00:00:00 2001 From: Faraaz Nishtar Date: Wed, 10 Jan 2018 15:11:27 -0800 Subject: [PATCH 1/4] Add output functionality --- api.js | 4 +++- lib/invoke.js | 8 ++++++++ package-lock.json | 18 +++++++++--------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/api.js b/api.js index 34edea43..5293d462 100644 --- a/api.js +++ b/api.js @@ -9,7 +9,7 @@ const invoke = require('./lib/invoke'); const linksPath = path.join(utils.sharedDirectoryPath(), 'links.json'); -module.exports.run = (action, d, cb) => { +module.exports.run = (action, d, outputs, cb) => { if (this.key && this.key.startsWith('demo')) { return maybe(cb, new Promise((resolve) => { console.log('This is a demo API key!'.red); @@ -58,6 +58,8 @@ module.exports.run = (action, d, cb) => { })); } + data['x-build-outputs'] = outputs; + return maybe(callback, new Promise((resolve, reject) => { return invoke(this.key, this.service, action, data).then((response) => { return resolve(response.body); diff --git a/lib/invoke.js b/lib/invoke.js index add8fd29..ec456371 100644 --- a/lib/invoke.js +++ b/lib/invoke.js @@ -1,5 +1,7 @@ const request = require('request-promise'); const os = require('os'); +const querystring = require('querystring'); + const { BUILD_URL, getSDKVersion } = require('../utils/utils'); module.exports = (key, service, action, data, isCLI) => { @@ -9,6 +11,12 @@ module.exports = (key, service, action, data, isCLI) => { 'X-Build-Meta-OS': `${os.type().toLowerCase()}@${os.release()}`, }; + if (data['x-build-outputs'] !== undefined) { + const parsedOutput = querystring.stringify(data['x-build-outputs']); + delete data['x-build-outputs']; + headers['X-Build-Output'] = parsedOutput; + } + return request.post(`${BUILD_URL}/run/${prefixName(service)}/${action}`, { json: data, resolveWithFullResponse: true, diff --git a/package-lock.json b/package-lock.json index 98b13026..e36467b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4686,6 +4686,15 @@ "resolved": "https://registry.npmjs.org/stealthy-require/-/stealthy-require-1.1.1.tgz", "integrity": "sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=" }, + "string-width": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.0.0.tgz", + "integrity": "sha1-Y1xUNsxypuDDh87KJ41OLuxSaH4=", + "requires": { + "is-fullwidth-code-point": "2.0.0", + "strip-ansi": "3.0.1" + } + }, "string_decoder": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.2.tgz", @@ -4701,15 +4710,6 @@ } } }, - "string-width": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.0.0.tgz", - "integrity": "sha1-Y1xUNsxypuDDh87KJ41OLuxSaH4=", - "requires": { - "is-fullwidth-code-point": "2.0.0", - "strip-ansi": "3.0.1" - } - }, "stringstream": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz", From 148d51940d2d0742e4ab2d29589a7a7f350af989 Mon Sep 17 00:00:00 2001 From: Faraaz Nishtar Date: Tue, 16 Jan 2018 15:42:48 -0800 Subject: [PATCH 2/4] Make it so that outputs object is optional when passing a callback to the `run` function --- package-lock.json | 16 ++++++++-------- src/api.js | 5 +++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 393bf553..fb7b36e0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3139,14 +3139,6 @@ } } }, - "string_decoder": { - "version": "1.0.1", - "bundled": true, - "dev": true, - "requires": { - "safe-buffer": "5.0.1" - } - }, "string-width": { "version": "1.0.2", "bundled": true, @@ -3157,6 +3149,14 @@ "strip-ansi": "3.0.1" } }, + "string_decoder": { + "version": "1.0.1", + "bundled": true, + "dev": true, + "requires": { + "safe-buffer": "5.0.1" + } + }, "stringstream": { "version": "0.0.5", "bundled": true, diff --git a/src/api.js b/src/api.js index 5293d462..80e02a85 100644 --- a/src/api.js +++ b/src/api.js @@ -26,6 +26,11 @@ module.exports.run = (action, d, outputs, cb) => { let data = d; let callback = cb; + // Make it so that outputs is an optional parameter + if (typeof outputs === 'function') { + callback = outputs; + } + // If no data is passed in, default to {} if (typeof data === 'function') { callback = data; From fad860307acd0fe33efb27ec71dd503cb0f421e3 Mon Sep 17 00:00:00 2001 From: Faraaz Nishtar Date: Tue, 16 Jan 2018 16:58:38 -0800 Subject: [PATCH 3/4] Last argument for `invoke` function now takes an object --- src/api.js | 4 +--- src/commands/run.js | 2 +- src/lib/invoke.js | 11 ++++++----- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/api.js b/src/api.js index 80e02a85..da2e857d 100644 --- a/src/api.js +++ b/src/api.js @@ -63,10 +63,8 @@ module.exports.run = (action, d, outputs, cb) => { })); } - data['x-build-outputs'] = outputs; - return maybe(callback, new Promise((resolve, reject) => { - return invoke(this.key, this.service, action, data).then((response) => { + return invoke(this.key, this.service, action, data, { outputs }).then((response) => { return resolve(response.body); }).catch((err) => { return reject(err.response.body); diff --git a/src/commands/run.js b/src/commands/run.js index d6afd5c6..c0076f1f 100644 --- a/src/commands/run.js +++ b/src/commands/run.js @@ -55,7 +55,7 @@ module.exports.run = (args, opts) => { service = `@${service}`; } - return invoke(team.key, service, action, data, true).then((response) => { + return invoke(team.key, service, action, data, { isCLI: true }).then((response) => { console.log(response.body); }).catch((err) => { try { diff --git a/src/lib/invoke.js b/src/lib/invoke.js index ec456371..3eff7e3c 100644 --- a/src/lib/invoke.js +++ b/src/lib/invoke.js @@ -4,16 +4,17 @@ const querystring = require('querystring'); const { BUILD_URL, getSDKVersion } = require('../utils/utils'); -module.exports = (key, service, action, data, isCLI) => { +module.exports = (key, service, action, data, opts = {}) => { + const outputs = opts.outputs; + const headers = { 'X-Build-Meta-Language': `node@${process.version.replace(/^v/, '')}`, - 'X-Build-Meta-SDK': getSDKVersion(isCLI), + 'X-Build-Meta-SDK': getSDKVersion(opts.isCLI), 'X-Build-Meta-OS': `${os.type().toLowerCase()}@${os.release()}`, }; - if (data['x-build-outputs'] !== undefined) { - const parsedOutput = querystring.stringify(data['x-build-outputs']); - delete data['x-build-outputs']; + if (outputs !== undefined) { + const parsedOutput = querystring.stringify(outputs); headers['X-Build-Output'] = parsedOutput; } From 7c7b2d6c66f84329c2a78365ee1945898e3f5fbc Mon Sep 17 00:00:00 2001 From: Faraaz Nishtar Date: Fri, 19 Jan 2018 12:25:30 -0800 Subject: [PATCH 4/4] Fixed a bug where the output would get incorrectly wrapped --- src/api.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api.js b/src/api.js index c6e8d3a7..c340f485 100644 --- a/src/api.js +++ b/src/api.js @@ -23,7 +23,7 @@ module.exports.config = (apiKey) => { }; const api = { - run(action, d, outputs, cb) { + run(action, d, opts, cb) { if (scope.key && scope.key.startsWith('demo')) { return maybe(cb, new Promise((resolve) => { console.log('This is a demo API key!'.red); @@ -41,8 +41,8 @@ const api = { let callback = cb; // Make outputs an optional parameter - if (typeof outputs === 'function') { - callback = outputs; + if (typeof opts === 'function') { + callback = opts; } // If no data is passed in, default to {} @@ -80,7 +80,7 @@ const api = { } return maybe(callback, new Promise((resolve, reject) => { - return invoke(scope.key, scope.service, action, data, { outputs }).then((response) => { + return invoke(scope.key, scope.service, action, data, opts).then((response) => { return resolve(response); }).catch((err) => { return reject(err);