diff --git a/lib/plugins/run/index.js b/lib/plugins/run/index.js index 1654d897b..6312cf7d9 100644 --- a/lib/plugins/run/index.js +++ b/lib/plugins/run/index.js @@ -1,19 +1,10 @@ 'use strict'; -/* - curl --request POST \ - --url http://127.0.0.1:8081/v1/functions \ - --header 'content-type: application/json' \ - --data '{"functionId": "hello", "provider":{"type": "http", "url": "http://localhost:8082/v0/emulator/api/functions/invoke"' - */ -/* - * test - */ const BbPromise = require('bluebird'); const _ = require('lodash'); const chalk = require('chalk'); const childProcess = BbPromise.promisifyAll(require('child_process')); -const fdk = require('fdk'); // not published yet +const fdk = require('@serverless/fdk'); const path = require('path'); const os = require('os'); @@ -29,6 +20,7 @@ const eventGatewayInstalled = require('./utils/eventGatewayInstalled'); const installLocalEmulator = require('./utils/installLocalEmulator'); const installEventGateway = require('./utils/installEventGateway'); +const getLatestEventGatewayVersion = require('./utils/getLatestEventGatewayVersion'); class Run { constructor(serverless, options) { @@ -102,7 +94,8 @@ class Run { }) .then(() => { if (!eventGatewayInstalled()) { - return installEventGateway(); + return getLatestEventGatewayVersion() + .then(version => installEventGateway(version)); } return BbPromise.resolve(); }) @@ -111,9 +104,8 @@ class Run { if (!functionsDeployed) { let initialized = false; this.logServerless('Spinning Up the Local Emulator...'); - const localEmulatorChildProcess = childProcess.spawn('node', - ['/Users/eslam/serverless-stuff/local-emulator/dist/index.js', - '--port', this.options.port]); + const localEmulatorChildProcess = childProcess.spawn('sle', + ['--port', this.options.lport]); localEmulatorChildProcess.stdout.on('data', chunk => { this.logLocalEmulator(chunk.toString('utf8')); @@ -142,14 +134,14 @@ class Run { eventGatewayChildProcess.stdout.on('data', chunk => { this.logEventGateway(chunk.toString('utf8')); - // if (!initialized) { - // initialized = true; - // return this.registerFunctionsToEventGateway(); - // } }); eventGatewayChildProcess.stderr.on('data', chunk => { this.logEventGateway(chunk.toString('utf8')); + if (!initialized) { + initialized = true; + return this.registerFunctionsToEventGateway(); + } }); eventGatewayChildProcess.on('close', () => resolve()); @@ -196,7 +188,7 @@ class Run { _.each(service.functions, (functionConfig, functionName) => { - const functionId = `${serviceName}-${functionName}`; + const functionId = `${serviceName}${functionName}`; const invokeFunctionUrl = `${getLocalRootUrl(this.options.lport) }/v0/emulator/api/invoke/${serviceName}/${functionName}`; @@ -210,8 +202,11 @@ class Run { functionsArray.push(functionObject); }); - return gateway.configure({ functions: functionsArray }) - .then(() => this.logServerless('Functions Registered in the Event Gateway!')); + setTimeout(() => { + return gateway.configure({ functions: functionsArray }) + .then(() => this.logServerless('Functions Registered in the Event Gateway!')); + }, 3000); + } logServerless(message) { diff --git a/lib/plugins/run/utils/deployFunctionToLocalEmulator.js b/lib/plugins/run/utils/deployFunctionToLocalEmulator.js index a8313efe8..f614e68df 100644 --- a/lib/plugins/run/utils/deployFunctionToLocalEmulator.js +++ b/lib/plugins/run/utils/deployFunctionToLocalEmulator.js @@ -11,7 +11,7 @@ function deployFunctionToLocalEmulator(serviceName, functionName, config, localE 'content-type': 'application/json', }, method: 'POST', - timeout: 1000, + timeout: 10000, body: JSON.stringify(config), }); } diff --git a/lib/plugins/run/utils/getLatestEventGatewayVersion.js b/lib/plugins/run/utils/getLatestEventGatewayVersion.js new file mode 100644 index 000000000..772bdf464 --- /dev/null +++ b/lib/plugins/run/utils/getLatestEventGatewayVersion.js @@ -0,0 +1,15 @@ +'use strict'; + +const fetch = require('node-fetch'); + +function getLatestEventGatewayVersion() { + const url = 'https://api.github.com/repos/serverless/event-gateway/releases/latest'; + + return fetch(url, { + method: 'GET', + timeout: 10000, + }).then(res => res.json()) + .then(json => json.name); +} + +module.exports = getLatestEventGatewayVersion; diff --git a/lib/plugins/run/utils/installEventGateway.js b/lib/plugins/run/utils/installEventGateway.js index 65f1d66c6..51d4dd944 100644 --- a/lib/plugins/run/utils/installEventGateway.js +++ b/lib/plugins/run/utils/installEventGateway.js @@ -4,8 +4,14 @@ const download = require('download'); const os = require('os'); const path = require('path'); -function installEventGateway() { - const eventGatewayDownloadUrl = 'https://github.com/serverless/event-gateway/releases/download/0.2.0/event-gateway_0.2.0_darwin_amd64.tar.gz'; +function installEventGateway(eventGatewayVersion) { + let eventGatewayDownloadUrl = `https://github.com/serverless/event-gateway/releases/download/${eventGatewayVersion}/event-gateway_${eventGatewayVersion}_darwin_386.tar.gz`; + + if (os.platform() === 'linux') { + eventGatewayDownloadUrl = `https://github.com/serverless/event-gateway/releases/download/${eventGatewayVersion}/event-gateway_${eventGatewayVersion}_linux_386.tar.gz`; + } else if (os.platform() === 'win32') { + eventGatewayDownloadUrl = `https://github.com/serverless/event-gateway/releases/download/${eventGatewayVersion}/event-gateway_${eventGatewayVersion}_windows_386.tar.gz`; + } const eventGatewayDownloadPath = path.join(os.homedir(), '.serverless', 'event-gateway'); return download( diff --git a/lib/plugins/run/utils/localEmulatorInstalled.js b/lib/plugins/run/utils/localEmulatorInstalled.js index cc7648211..5df4e0b45 100644 --- a/lib/plugins/run/utils/localEmulatorInstalled.js +++ b/lib/plugins/run/utils/localEmulatorInstalled.js @@ -1,12 +1,24 @@ 'use strict'; -const BbPromise = require('bluebird'); -const childProcess = BbPromise.promisifyAll(require('child_process')); +// const BbPromise = require('bluebird'); +// const childProcess = BbPromise.promisifyAll(require('child_process')); +// +// function localEmulatorInsatlled() { +// const stdout = childProcess.execSync('npm list -g serverless-local-emulator'); +// const stdoutString = new Buffer(stdout, 'base64').toString(); +// return stdoutString.includes('serverless-local-emulator'); +// } +// +// module.exports = localEmulatorInsatlled; -function localEmulatorInsatlled() { - const stdout = childProcess.execSync('npm list -g serverless-local-emulator'); - const stdoutString = new Buffer(stdout, 'base64').toString(); - return stdoutString.includes('serverless-local-emulator'); +const path = require('path'); +const os = require('os'); +const fileExistsSync = require('../../../utils/fs/fileExistsSync'); + +function localEmulatorInstalled() { + const localEmulatorPackageJsonFilePath = path + .join(os.homedir(), 'node_modules', 'serverless-local-emulator', 'package.json'); + return fileExistsSync(localEmulatorPackageJsonFilePath); } -module.exports = localEmulatorInsatlled; +module.exports = localEmulatorInstalled; diff --git a/package-lock.json b/package-lock.json index 4baf21dbf..b40982741 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,16 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@serverless/fdk": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@serverless/fdk/-/fdk-0.1.0.tgz", + "integrity": "sha512-Kg+Mp8QZivtzAYnitXAk8gKfwhHlY/AYLx3fQSP9lpqZAKDC3NstIwkFUmQBp/wc84fRlsEPOsLDejlkLaTdxg==", + "requires": { + "aws-sdk": "2.67.0", + "isomorphic-fetch": "2.2.1", + "ramda": "0.24.1" + } + }, "@types/async": { "version": "2.0.40", "resolved": "https://registry.npmjs.org/@types/async/-/async-2.0.40.tgz", @@ -2623,6 +2633,15 @@ "isarray": "1.0.0" } }, + "isomorphic-fetch": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz", + "integrity": "sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=", + "requires": { + "node-fetch": "1.7.1", + "whatwg-fetch": "2.0.3" + } + }, "isstream": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", @@ -4312,6 +4331,11 @@ "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" }, + "ramda": { + "version": "0.24.1", + "resolved": "https://registry.npmjs.org/ramda/-/ramda-0.24.1.tgz", + "integrity": "sha1-w7d1UZfzW43DUCIoJixMkd22uFc=" + }, "randomatic": { "version": "1.1.7", "resolved": "https://registry.npmjs.org/randomatic/-/randomatic-1.1.7.tgz", diff --git a/package.json b/package.json index 4d651ebff..09436da5e 100644 --- a/package.json +++ b/package.json @@ -86,12 +86,12 @@ "sinon-chai": "^2.9.0" }, "dependencies": { + "@serverless/fdk": "^0.1.0", "apollo-client": "^1.4.2", "archiver": "^1.1.0", "async": "^1.5.2", "aws-sdk": "^2.7.13", "bluebird": "^3.4.0", - "@serverless/fdk": "^0.1.0", "chalk": "^1.1.1", "ci-info": "^1.0.0", "download": "^5.0.2",