From 1d492e9d916af39a5e2a887f75049ab9da8430e6 Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Tue, 5 Jan 2021 14:22:39 -0500 Subject: [PATCH 1/4] updated endpoint used for build/deploy and added polling --- packages/cli-lib/api/functions.js | 13 +++++++-- packages/cli/commands/functions/deploy.js | 34 +++++++++++++++++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/packages/cli-lib/api/functions.js b/packages/cli-lib/api/functions.js index b0d2ae044..40660f039 100644 --- a/packages/cli-lib/api/functions.js +++ b/packages/cli-lib/api/functions.js @@ -15,15 +15,21 @@ async function getRoutes(accountId) { }); } -async function buildPackage(portalId, path) { +async function buildPackage(portalId, folderPath) { return http.post(portalId, { - uri: `${FUNCTION_API_PATH}/package`, + uri: `${FUNCTION_API_PATH}/build`, body: { - path, + folderPath, }, }); } +async function pollBuild(portalId, buildId) { + return http.get(portalId, { + uri: `${FUNCTION_API_PATH}/build/${buildId}/poll`, + }); +} + async function deletePackage(portalId, path) { return fetchRawAssetByPath(portalId, path).then(resp => { return http.delete(portalId, { @@ -37,4 +43,5 @@ module.exports = { deletePackage, getFunctionByPath, getRoutes, + pollBuild, }; diff --git a/packages/cli/commands/functions/deploy.js b/packages/cli/commands/functions/deploy.js index 51d88f0b5..6e7d552c4 100644 --- a/packages/cli/commands/functions/deploy.js +++ b/packages/cli/commands/functions/deploy.js @@ -21,6 +21,7 @@ const { logger } = require('@hubspot/cli-lib/logger'); const { buildPackage, deletePackage, + pollBuild, } = require('@hubspot/cli-lib/api/functions'); const { validateAccount } = require('../../lib/validation'); @@ -30,6 +31,23 @@ const makeSpinner = (actionText, functionPath, accountIdentifier) => { ); }; +const pollBuildStatus = (accountId, buildId) => { + return new Promise((resolve, reject) => { + const pollInterval = setInterval(async () => { + const pollResp = await pollBuild(accountId, buildId); + const { status } = pollResp; + + if (status === 'SUCCESS') { + clearInterval(pollInterval); + resolve(pollResp); + } else if (status === 'ERROR') { + clearInterval(pollInterval); + reject(pollResp); + } + }, 1000); + }); +}; + const loadAndValidateOptions = async options => { setLogLevel(options); logDebugInfo(options); @@ -72,15 +90,19 @@ exports.handler = async options => { try { let successMessage; if (shouldDeletePackage) { - spinner = makeSpinner('Deleting', functionPath, accountId); - spinner.start(); + spinner = makeSpinner('Deleting', functionPath, accountId).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 = makeSpinner( + 'Building and deploying', + functionPath, + accountId + ).start(); + const buildId = await buildPackage(accountId, functionPath); + const successResp = await pollBuildStatus(accountId, buildId); + const buildTimeSeconds = (successResp.buildTime / 1000).toFixed(2); + successMessage = `Successfully built and deployed bundle from package.json for ${functionPath} on account ${accountId} in ${buildTimeSeconds}s.`; } spinner.stop(); logger.success(successMessage); From 801db28f56dea036b4d935c9951fc120c27b835d Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Tue, 5 Jan 2021 14:59:36 -0500 Subject: [PATCH 2/4] changed name of method from pollBuild to getBuildStatus for clarity --- packages/cli-lib/api/functions.js | 4 ++-- packages/cli/commands/functions/deploy.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/cli-lib/api/functions.js b/packages/cli-lib/api/functions.js index 40660f039..61b39ddf5 100644 --- a/packages/cli-lib/api/functions.js +++ b/packages/cli-lib/api/functions.js @@ -24,7 +24,7 @@ async function buildPackage(portalId, folderPath) { }); } -async function pollBuild(portalId, buildId) { +async function getBuildStatus(portalId, buildId) { return http.get(portalId, { uri: `${FUNCTION_API_PATH}/build/${buildId}/poll`, }); @@ -41,7 +41,7 @@ async function deletePackage(portalId, path) { module.exports = { buildPackage, deletePackage, + getBuildStatus, getFunctionByPath, getRoutes, - pollBuild, }; diff --git a/packages/cli/commands/functions/deploy.js b/packages/cli/commands/functions/deploy.js index 6e7d552c4..6002ff284 100644 --- a/packages/cli/commands/functions/deploy.js +++ b/packages/cli/commands/functions/deploy.js @@ -21,7 +21,7 @@ const { logger } = require('@hubspot/cli-lib/logger'); const { buildPackage, deletePackage, - pollBuild, + getBuildStatus, } = require('@hubspot/cli-lib/api/functions'); const { validateAccount } = require('../../lib/validation'); @@ -34,7 +34,7 @@ const makeSpinner = (actionText, functionPath, accountIdentifier) => { const pollBuildStatus = (accountId, buildId) => { return new Promise((resolve, reject) => { const pollInterval = setInterval(async () => { - const pollResp = await pollBuild(accountId, buildId); + const pollResp = await getBuildStatus(accountId, buildId); const { status } = pollResp; if (status === 'SUCCESS') { From 19ad14f5e1e9e9642ddb413f4f7ddc35e8075df1 Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Tue, 12 Jan 2021 10:07:54 -0500 Subject: [PATCH 3/4] removed --delete option as the BE handles this now --- packages/cli-lib/api/functions.js | 11 ------ packages/cli/commands/functions/deploy.js | 41 ++++++----------------- 2 files changed, 11 insertions(+), 41 deletions(-) diff --git a/packages/cli-lib/api/functions.js b/packages/cli-lib/api/functions.js index 61b39ddf5..8409ba538 100644 --- a/packages/cli-lib/api/functions.js +++ b/packages/cli-lib/api/functions.js @@ -1,6 +1,4 @@ const http = require('../http'); -const { fetchRawAssetByPath } = require('./designManager'); - const FUNCTION_API_PATH = 'cms/v3/functions'; async function getFunctionByPath(accountId, functionPath) { @@ -30,17 +28,8 @@ async function getBuildStatus(portalId, buildId) { }); } -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, getBuildStatus, getFunctionByPath, getRoutes, diff --git a/packages/cli/commands/functions/deploy.js b/packages/cli/commands/functions/deploy.js index 6002ff284..cd96de991 100644 --- a/packages/cli/commands/functions/deploy.js +++ b/packages/cli/commands/functions/deploy.js @@ -20,7 +20,6 @@ const { const { logger } = require('@hubspot/cli-lib/logger'); const { buildPackage, - deletePackage, getBuildStatus, } = require('@hubspot/cli-lib/api/functions'); const { validateAccount } = require('../../lib/validation'); @@ -62,14 +61,12 @@ const loadAndValidateOptions = async options => { exports.command = 'deploy '; exports.describe = false; -// Uncomment to unhide 'builds a new dependency bundle for the specified .functions folder'; exports.handler = async options => { loadAndValidateOptions(options); const { path: functionPath } = options; const accountId = getAccountId(options); - const { delete: shouldDeletePackage } = options; const splitFunctionPath = functionPath.split('.'); let spinner; @@ -88,24 +85,18 @@ exports.handler = async options => { ); try { - let successMessage; - if (shouldDeletePackage) { - spinner = makeSpinner('Deleting', functionPath, accountId).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 - ).start(); - const buildId = await buildPackage(accountId, functionPath); - const successResp = await pollBuildStatus(accountId, buildId); - const buildTimeSeconds = (successResp.buildTime / 1000).toFixed(2); - successMessage = `Successfully built and deployed bundle from package.json for ${functionPath} on account ${accountId} in ${buildTimeSeconds}s.`; - } + spinner = makeSpinner( + 'Building and deploying', + functionPath, + accountId + ).start(); + const buildId = await buildPackage(accountId, functionPath); + const successResp = await pollBuildStatus(accountId, buildId); + const buildTimeSeconds = (successResp.buildTime / 1000).toFixed(2); spinner.stop(); - logger.success(successMessage); + logger.success( + `Successfully built and deployed bundle from package.json for ${functionPath} on account ${accountId} in ${buildTimeSeconds}s.` + ); } catch (e) { spinner && spinner.stop && spinner.stop(); if (e.statusCode === 404) { @@ -128,21 +119,11 @@ exports.builder = yargs => { 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); From 2a6d21b209246c1fc7c8aefcec9c9c3709995b59 Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Tue, 12 Jan 2021 17:02:16 -0500 Subject: [PATCH 4/4] changed polling to 5000 --- packages/cli/commands/functions/deploy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/commands/functions/deploy.js b/packages/cli/commands/functions/deploy.js index cd96de991..cb3c04b61 100644 --- a/packages/cli/commands/functions/deploy.js +++ b/packages/cli/commands/functions/deploy.js @@ -43,7 +43,7 @@ const pollBuildStatus = (accountId, buildId) => { clearInterval(pollInterval); reject(pollResp); } - }, 1000); + }, 5000); }); };