Skip to content

Commit

Permalink
Merge pull request #152 from HubSpot/uri-handler-multiple-windows
Browse files Browse the repository at this point in the history
Uri handler multiple windows
  • Loading branch information
miketalley authored Dec 16, 2022
2 parents 25f8a71 + 20a0143 commit e05e71e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export const activate = async (context: ExtensionContext) => {
console.log('Activating Extension...');
const rootPath = getRootPath();

registerCommands(context);
registerCommands(context, rootPath);
registerEvents(context);
registerURIHandler(context, rootPath);
registerURIHandler(context);

initializeProviders(context);
initializeTerminal(context);
Expand Down
7 changes: 5 additions & 2 deletions src/lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { registerCommands as registerTerminalCommands } from './commands/termina
import { registerCommands as registerModuleCommands } from './commands/module';
import { registerCommands as registerServerlessFunctionCommands } from './commands/serverlessFunction';

export const registerCommands = (context: ExtensionContext) => {
export const registerCommands = (
context: ExtensionContext,
rootPath: string
) => {
registerConfigCommands(context);
registerAuthCommands(context);
registerAuthCommands(context, rootPath);
registerTerminalCommands(context);
registerModuleCommands(context);
registerServerlessFunctionCommands(context);
Expand Down
8 changes: 5 additions & 3 deletions src/lib/commands/auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { commands, env, ExtensionContext, Uri } from 'vscode';
import { COMMANDS } from '../constants';

export const registerCommands = (context: ExtensionContext) => {
export const registerCommands = (
context: ExtensionContext,
rootPath: string
) => {
context.subscriptions.push(
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?vsCodeExtensionRootPath=${rootPath}`;

const callableUri = await env.asExternalUri(Uri.parse(authUrl));
await env.openExternal(callableUri);
Expand Down
5 changes: 2 additions & 3 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -27,7 +26,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 (
Expand Down
33 changes: 20 additions & 13 deletions src/lib/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ const getQueryObject = (uri: Uri) => {
return new URLSearchParams(uri.query);
};

const handleAuthRequest = async (
rootPath: string,
authParams: URLSearchParams
) => {
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 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 {
Expand All @@ -49,17 +54,22 @@ const handleAuthRequest = async (
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
);
}
});

Expand All @@ -68,10 +78,7 @@ const handleAuthRequest = async (
return updatedConfig;
};

export const registerURIHandler = (
context: ExtensionContext,
rootPath: string
) => {
export const registerURIHandler = (context: ExtensionContext) => {
// https://github.com/microsoft/vscode-extension-samples/blob/main/uri-handler-sample/package.json
window.registerUriHandler({
handleUri(uri: Uri): ProviderResult<void> {
Expand All @@ -80,7 +87,7 @@ export const registerURIHandler = (
const queryObject = getQueryObject(uri);

console.log('queryObject: ', queryObject);
handleAuthRequest(rootPath, queryObject);
handleAuthRequest(queryObject);
}
},
});
Expand Down

0 comments on commit e05e71e

Please sign in to comment.