From 855b833eca38aa6995ca8bf788906b3c8c24a092 Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Wed, 7 Dec 2022 10:45:46 -0500 Subject: [PATCH 1/6] rootPath is passed in url to PAK and parsed in response --- src/extension.ts | 4 ++-- src/lib/commands.ts | 7 +++++-- src/lib/commands/auth.ts | 8 +++++--- src/lib/uri.ts | 13 ++++--------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 1146c2ee..bc3ab67b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -11,8 +11,8 @@ export const activate = async (context: vscode.ExtensionContext) => { console.log('Activating Extension...'); const rootPath = getRootPath(); - registerCommands(context); - registerURIHandler(context, rootPath); + registerCommands(context, rootPath); + registerURIHandler(context); initializeProviders(context); initializeStatusBar(context); diff --git a/src/lib/commands.ts b/src/lib/commands.ts index 37440cee..a4a6cc8b 100644 --- a/src/lib/commands.ts +++ b/src/lib/commands.ts @@ -3,8 +3,11 @@ import { registerCommands as registerConfigCommands } from './commands/config'; import { registerCommands as registerAuthCommands } from './commands/auth'; import { registerCommands as registerModuleCommands } from './commands/module'; -export const registerCommands = (context: vscode.ExtensionContext) => { +export const registerCommands = ( + context: vscode.ExtensionContext, + rootPath: string +) => { registerConfigCommands(context); - registerAuthCommands(context); + registerAuthCommands(context, rootPath); registerModuleCommands(context); }; diff --git a/src/lib/commands/auth.ts b/src/lib/commands/auth.ts index aab47cc0..6cb2dca2 100644 --- a/src/lib/commands/auth.ts +++ b/src/lib/commands/auth.ts @@ -11,7 +11,10 @@ import { loadHubspotConfigFile } from '../auth'; let configFoundAndLoaded = false; let hubspotConfigWatcher: fs.FSWatcher | null; -export const registerCommands = (context: vscode.ExtensionContext) => { +export const registerCommands = ( + context: vscode.ExtensionContext, + rootPath: string +) => { context.subscriptions.push( vscode.commands.registerCommand( COMMANDS.ON_CONFIG_FOUND, @@ -58,8 +61,7 @@ export const registerCommands = (context: vscode.ExtensionContext) => { context.subscriptions.push( vscode.commands.registerCommand(COMMANDS.AUTHORIZE_ACCOUNT, async () => { - const authUrl = - 'https://app.hubspot.com/l/personal-access-key/auth/vscode'; + const authUrl = `https://app.hubspot.com/l/personal-access-key/auth/vscode${rootPath}`; const callableUri = await vscode.env.asExternalUri( vscode.Uri.parse(authUrl) diff --git a/src/lib/uri.ts b/src/lib/uri.ts index 5722ec24..e57ef0a6 100644 --- a/src/lib/uri.ts +++ b/src/lib/uri.ts @@ -17,10 +17,8 @@ const getQueryObject = (uri: vscode.Uri) => { return new URLSearchParams(uri.query); }; -const handleAuthRequest = async ( - rootPath: string, - authParams: URLSearchParams -) => { +const handleAuthRequest = async (authParams: URLSearchParams) => { + const rootPath = authParams.get('rootPath') || ''; const personalAccessKeyResp = authParams.get('personalAccessKeyResp') || ''; const env = authParams.get('env') || 'prod'; const name = authParams.get('name'); @@ -75,10 +73,7 @@ const handleAuthRequest = async ( return updatedConfig; }; -export const registerURIHandler = ( - context: vscode.ExtensionContext, - rootPath: string -) => { +export const registerURIHandler = (context: vscode.ExtensionContext) => { // https://github.com/microsoft/vscode-extension-samples/blob/main/uri-handler-sample/package.json vscode.window.registerUriHandler({ handleUri(uri: vscode.Uri): vscode.ProviderResult { @@ -87,7 +82,7 @@ export const registerURIHandler = ( const queryObject = getQueryObject(uri); console.log('queryObject: ', queryObject); - handleAuthRequest(rootPath, queryObject); + handleAuthRequest(queryObject); } }, }); From 68c0b60ecaf238d062659eb901b1e029ea2be4fb Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Mon, 12 Dec 2022 16:44:41 -0500 Subject: [PATCH 2/6] fixed default account selector if an account has no name --- src/lib/helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 2f3540e6..f5625bf0 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -27,7 +27,7 @@ export const getDefaultPortalFromConfig = (config: HubspotConfig) => { export const getDisplayedHubspotPortalInfo = (portalData: Portal) => { return portalData.name ? `${portalData.name} - ${portalData.portalId}` - : portalData.portalId; + : `${portalData.portalId}`; }; export const runTerminalCommand = async ( From b174aca23bb8b6a8ef6e9ef306555cc2dee7119c Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Mon, 12 Dec 2022 16:45:22 -0500 Subject: [PATCH 3/6] handleAuthRequest now properly handles accounts with no name --- src/lib/uri.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib/uri.ts b/src/lib/uri.ts index 9763d5b5..d64666e0 100644 --- a/src/lib/uri.ts +++ b/src/lib/uri.ts @@ -28,7 +28,9 @@ const handleAuthRequest = async (authParams: URLSearchParams) => { const personalAccessKeyResp = authParams.get('personalAccessKeyResp') || ''; const env = authParams.get('env') || 'prod'; const name = authParams.get('name'); + const portalId = authParams.get('portalId'); const { key: personalAccessKey } = JSON.parse(personalAccessKeyResp); + const accountIdentifier = name || portalId; let configPath = loadHubspotConfigFile(rootPath); if (configPath) { @@ -47,17 +49,22 @@ const handleAuthRequest = async (authParams: URLSearchParams) => { commands.executeCommand(EVENTS.ON_CONFIG_FOUND, rootPath, configPath); commands.executeCommand('setContext', 'hubspot.auth.isAuthenticating', false); - window.showInformationMessage(`Successfully added ${name} to the config.`); + window.showInformationMessage( + `Successfully added ${accountIdentifier} to the config.` + ); window .showInformationMessage( - `Do you want to set ${name} as the default account?`, + `Do you want to set ${accountIdentifier} as the default account?`, 'Yes', 'No' ) .then((answer: string | undefined) => { if (answer === 'Yes') { - console.log(`Updating defaultPortal to ${name}.`); - commands.executeCommand(COMMANDS.CONFIG.SET_DEFAULT_ACCOUNT, name); + console.log(`Updating defaultPortal to ${accountIdentifier}.`); + commands.executeCommand( + COMMANDS.CONFIG.SET_DEFAULT_ACCOUNT, + accountIdentifier + ); } }); From 9867237af6beab96da0d52735c1e67ab64be2a05 Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Tue, 13 Dec 2022 14:48:20 -0500 Subject: [PATCH 4/6] updated to using path instead of fsPath to help fix issues with urls containing paths in windows --- src/lib/helpers.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index f5625bf0..626f869e 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -9,8 +9,7 @@ export const getRootPath = () => { if (!workspaceFolders || workspaceFolders.length < 1) { throw new Error('No workspace folder found.'); } - - return workspaceFolders[0].uri.fsPath; + return workspaceFolders[0].uri.path; }; export const getDefaultPortalFromConfig = (config: HubspotConfig) => { From f0bb77baf2e4e986309387e11df5d9acf0eb9d3a Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Wed, 14 Dec 2022 13:54:37 -0500 Subject: [PATCH 5/6] added fix for windows --- src/lib/uri.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/uri.ts b/src/lib/uri.ts index d64666e0..b9f7fb29 100644 --- a/src/lib/uri.ts +++ b/src/lib/uri.ts @@ -24,15 +24,20 @@ const getQueryObject = (uri: Uri) => { }; const handleAuthRequest = async (authParams: URLSearchParams) => { - const rootPath = authParams.get('rootPath') || ''; const personalAccessKeyResp = authParams.get('personalAccessKeyResp') || ''; const env = authParams.get('env') || 'prod'; const name = authParams.get('name'); const portalId = authParams.get('portalId'); const { key: personalAccessKey } = JSON.parse(personalAccessKeyResp); const accountIdentifier = name || portalId; + let rootPath = authParams.get('rootPath') || ''; let configPath = loadHubspotConfigFile(rootPath); + // handle windows paths, which look something like /C:/Some/path + if (/^\/\w:\/.*$/.test(rootPath)) { + rootPath = rootPath.slice(1); + } + if (configPath) { setConfigPath(configPath); } else { From e2a80d785d58017c2fe3ee4259bf57c7e1840c94 Mon Sep 17 00:00:00 2001 From: Mike Talley Date: Wed, 14 Dec 2022 14:29:53 -0500 Subject: [PATCH 6/6] auth URL now uses vsCodeExtensionRootpath query param instead of /auth/vscode path --- src/lib/commands/auth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/commands/auth.ts b/src/lib/commands/auth.ts index 737a7308..6f161ef9 100644 --- a/src/lib/commands/auth.ts +++ b/src/lib/commands/auth.ts @@ -7,7 +7,7 @@ export const registerCommands = ( ) => { context.subscriptions.push( commands.registerCommand(COMMANDS.AUTHORIZE_ACCOUNT, async () => { - const authUrl = `https://app.hubspot.com/l/personal-access-key/auth/vscode${rootPath}`; + const authUrl = `https://app.hubspot.com/l/personal-access-key?vsCodeExtensionRootPath=${rootPath}`; const callableUri = await env.asExternalUri(Uri.parse(authUrl)); await env.openExternal(callableUri);