Skip to content

Commit

Permalink
Merge pull request #441 from HubSpot/serverless/empathy-fixes
Browse files Browse the repository at this point in the history
Serverless empathy fixes
  • Loading branch information
miketalley authored Feb 11, 2021
2 parents f60755c + d6a11c3 commit e6503dc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/cli/commands/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
} = require('../lib/commonOpts');
const list = require('./functions/list');
const deploy = require('./functions/deploy');
const test = require('./functions/test');
const server = require('./functions/server');

exports.command = 'functions';
exports.describe = 'Commands for working with functions';
Expand All @@ -21,7 +21,7 @@ exports.builder = yargs => {
aliases: 'ls',
})
.command(deploy)
.command(test)
.command(server)
.demandCommand(1, '');

return yargs;
Expand Down
13 changes: 11 additions & 2 deletions packages/cli/commands/functions/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ const loadAndValidateOptions = async options => {
};

const logBuildOutput = async resp => {
const { cdnUrl } = resp;

if (!cdnUrl) {
logger.debug(
'Unable to display build output. No build log URL was provided.'
);
return;
}

return new Promise((resolve, reject) => {
try {
https
Expand All @@ -76,6 +85,7 @@ const logBuildOutput = async resp => {
data += chunk;
});
response.on('end', () => {
logger.log(data);
resolve(data);
});
})
Expand Down Expand Up @@ -121,8 +131,7 @@ exports.handler = async options => {
const successResp = await pollBuildStatus(accountId, buildId);
const buildTimeSeconds = (successResp.buildTime / 1000).toFixed(2);
spinner.stop();
const buildOutput = await logBuildOutput(successResp);
logger.log(buildOutput);
await logBuildOutput(successResp);
logger.success(
`Built and deployed bundle from package.json for ${functionPath} on account ${accountId} in ${buildTimeSeconds}s.`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const loadAndValidateOptions = async options => {
}
};

exports.command = 'test <path>';
exports.command = 'server <path>';
exports.describe = false;

exports.handler = async options => {
Expand All @@ -40,7 +40,7 @@ exports.handler = async options => {
trackCommandUsage('functions-test', { functionPath }, accountId);

logger.debug(
`Starting test server for .functions folder with path: ${functionPath}`
`Starting local test server for .functions folder with path: ${functionPath}`
);

startTestServer({
Expand Down Expand Up @@ -79,7 +79,7 @@ exports.builder = yargs => {

yargs.example([
[
'$0 functions test ./tmp/myFunctionFolder.functions',
'$0 functions server ./tmp/myFunctionFolder.functions',
'Run a local function test server.',
],
]);
Expand Down
21 changes: 16 additions & 5 deletions packages/serverless-dev-runtime/lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tmp = require('tmp');
const { logger } = require('@hubspot/cli-lib/logger');
const defaultFunctionPackageJson = require('./templates/default-function-package.json');

const installDeps = folderPath => {
const installDeps = (functionData, folderPath) => {
const npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm';
const packageJsonPath = `${folderPath}/package.json`;
const packageJsonExists = fs.existsSync(packageJsonPath);
Expand All @@ -18,17 +18,28 @@ const installDeps = folderPath => {
);
}

logger.debug(`Installing dependencies from ${folderPath}/package.json`);
logger.log(
`Installing dependencies from ${functionData.srcPath}/package.json`
);

return new Promise((resolve, reject) => {
try {
let errorData = '';
const npmInstallProcess = spawn(npmCmd, ['i'], {
env: process.env,
cwd: folderPath,
});

npmInstallProcess.on('exit', data => {
resolve(data);
npmInstallProcess.stderr.on('data', data => {
errorData += data;
});

npmInstallProcess.on('exit', code => {
if (code) {
logger.error(`Unable to install dependencies\n${errorData}`);
process.exit();
}
return resolve(code);
});
} catch (e) {
reject(e);
Expand All @@ -53,7 +64,7 @@ const createTemporaryFunction = async functionData => {
errorOnExist: true,
});

await installDeps(tmpDir.name);
await installDeps(functionData, tmpDir.name);

return {
...functionData,
Expand Down
11 changes: 7 additions & 4 deletions packages/serverless-dev-runtime/lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const addEndpointToApp = endpointData => {
},
},
});

if (options.logOutput) {
logger.log(
util.inspect(body, {
Expand All @@ -119,11 +120,13 @@ const addEndpointToApp = endpointData => {
})
);
}

outputTrackedLogs(trackedLogs);
res
.status(statusCode)
.set(headers)
.send(body);

if (statusCode) {
res.status(statusCode);
}
res.set(headers).send(body);
};

await main(dataForFunc, functionExecutionCallback);
Expand Down
12 changes: 7 additions & 5 deletions packages/serverless-dev-runtime/lib/secrets.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ const getDotEnvConfig = folderPath => {
const getSecrets = (dotEnvConfig, allowedSecrets) => {
let secretsDict = {};

allowedSecrets.forEach(secret => {
if (Object.prototype.hasOwnProperty.call(dotEnvConfig, secret)) {
secretsDict[secret] = dotEnvConfig[secret];
}
});
if (dotEnvConfig) {
allowedSecrets.forEach(secret => {
if (Object.prototype.hasOwnProperty.call(dotEnvConfig, secret)) {
secretsDict[secret] = dotEnvConfig[secret];
}
});
}

return secretsDict;
};
Expand Down

0 comments on commit e6503dc

Please sign in to comment.