Skip to content

Commit

Permalink
Merge pull request #403 from HubSpot/hotfix/add-delete-option-to-deploy
Browse files Browse the repository at this point in the history
Added delete option to hs functions deploy command to support BE in alpha release
  • Loading branch information
miketalley authored Jan 4, 2021
2 parents 8b31b3b + 58aa4fc commit 6a2b260
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
46 changes: 34 additions & 12 deletions packages/cms-cli/commands/functions/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ const {
ApiErrorContext,
} = require('@hubspot/cms-lib/errorHandlers');
const { logger } = require('@hubspot/cms-lib/logger');
const { buildPackage } = require('@hubspot/cms-lib/api/functions');
const {
buildPackage,
deletePackage,
} = require('@hubspot/cms-lib/api/functions');
const { validateAccount } = require('../../lib/validation');

const makeSpinner = (functionPath, accountIdentifier) => {
const makeSpinner = (actionText, functionPath, accountIdentifier) => {
return ora(
`Building and deploying new bundle for '${functionPath}' on account '${accountIdentifier}'.\n`
`${actionText} bundle for '${functionPath}' on account '${accountIdentifier}'.\n`
);
};

Expand All @@ -48,12 +51,12 @@ exports.handler = async options => {

const { path: functionPath } = options;
const accountId = getAccountId(options);
const spinner = makeSpinner(functionPath, accountId);
const { delete: shouldDeletePackage } = options;
const splitFunctionPath = functionPath.split('.');
let spinner;

trackCommandUsage('functions-deploy', { functionPath }, accountId);

const splitFunctionPath = functionPath.split('.');

if (
!splitFunctionPath.length ||
splitFunctionPath[splitFunctionPath.length - 1] !== 'functions'
Expand All @@ -66,15 +69,23 @@ exports.handler = async options => {
`Starting build and deploy for .functions folder with path: ${functionPath}`
);

spinner.start();
try {
await buildPackage(accountId, `${functionPath}/package.json`);
let successMessage;
if (shouldDeletePackage) {
spinner = makeSpinner('Deleting', functionPath, accountId);
spinner.start();
await deletePackage(accountId, `${functionPath}/package.json`);
successMessage = `Successfully removed build package for ${functionPath} on account ${accountId}.`;
} else {
spinner = makeSpinner('Building and deploying', functionPath, accountId);
spinner.start();
await buildPackage(accountId, `${functionPath}/package.json`);
successMessage = `Successfully built and deployed bundle from package.json for ${functionPath} on account ${accountId}.`;
}
spinner.stop();
logger.success(
`Successfully built and deployed bundle from package.json for ${functionPath} on account ${accountId}.`
);
logger.success(successMessage);
} catch (e) {
spinner.stop();
spinner && spinner.stop && spinner.stop();
if (e.statusCode === 404) {
logger.error(`Unable to find package.json for function ${functionPath}.`);
} else if (e.statusCode === 400) {
Expand All @@ -94,11 +105,22 @@ exports.builder = yargs => {
describe: 'Path to .functions folder',
type: 'string',
});

yargs.option('delete', {
alias: 'D',
describe: 'Remove currently built and deployed package',
type: 'boolean',
});

yargs.example([
[
'$0 functions deploy myFunctionFolder.functions',
'Build and deploy a new bundle for all functions within the myFunctionFolder.functions folder',
],
[
'$0 functions deploy myFunctionFolder.functions --delete',
'Delete the currently built and deployed bundle used by all functions within the myFunctionFolder.functions folder',
],
]);

addConfigOptions(yargs, true);
Expand Down
7 changes: 7 additions & 0 deletions packages/cms-lib/api/designManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ async function fetchBuiltinMapping(accountId) {
});
}

async function fetchRawAssetByPath(accountId, path) {
return http.get(accountId, {
uri: `${DESIGN_MANAGER_API_PATH}/raw-assets/by-path/${path}?portalId=${accountId}`,
});
}

module.exports = {
fetchBuiltinMapping,
fetchMenus,
fetchRawAssetByPath,
};
10 changes: 10 additions & 0 deletions packages/cms-lib/api/functions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const http = require('../http');
const { fetchRawAssetByPath } = require('./designManager');

const FUNCTION_API_PATH = 'cms/v3/functions';

Expand All @@ -23,8 +24,17 @@ async function buildPackage(portalId, path) {
});
}

async function deletePackage(portalId, path) {
return fetchRawAssetByPath(portalId, path).then(resp => {
return http.delete(portalId, {
uri: `${FUNCTION_API_PATH}/package?portalId=${portalId}&rawAssetId=${resp.id}`,
});
});
}

module.exports = {
buildPackage,
deletePackage,
getFunctionByPath,
getRoutes,
};

0 comments on commit 6a2b260

Please sign in to comment.