Skip to content

Commit 8535310

Browse files
feat: Update VS code to be compatible with the global config (#287)
* update VS code to be compatible with the global config * support config migrations
1 parent 0e1b3ea commit 8535310

File tree

9 files changed

+130
-71
lines changed

9 files changed

+130
-71
lines changed

package-lock.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@
587587
]
588588
},
589589
"dependencies": {
590-
"@hubspot/local-dev-lib": "^3.1.0",
590+
"@hubspot/local-dev-lib": "^3.7.0",
591591
"@hubspot/project-parsing-lib": "0.2.0",
592592
"dayjs": "^1.11.7",
593593
"debounce": "1.2.1",

src/lib/auth.ts

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { commands } from 'vscode';
1+
import { commands, window } from 'vscode';
22
import { COMMANDS, EVENTS } from './constants';
33
import {
44
findConfig,
55
loadConfig,
66
validateConfig,
77
setConfig,
88
setConfigPath,
9+
getConfigPath,
910
} from '@hubspot/local-dev-lib/config';
11+
import {
12+
getDeprecatedConfig,
13+
getGlobalConfig,
14+
mergeConfigProperties,
15+
mergeExistingConfigs,
16+
} from '@hubspot/local-dev-lib/config/migrate';
1017

1118
const onLoadPath = (configPath: string) => {
1219
commands.executeCommand('setContext', 'hubspot.configPath', configPath);
@@ -22,24 +29,66 @@ export const loadHubspotConfigFile = (rootPath: string) => {
2229
return;
2330
}
2431

25-
console.log(`rootPath: ${rootPath}`);
32+
const deprecatedConfigPath = findConfig(rootPath);
33+
const globalConfigPath = getConfigPath(undefined, true);
34+
35+
const resolvedConfigPath = globalConfigPath || deprecatedConfigPath;
36+
37+
if (!resolvedConfigPath) {
38+
return;
39+
}
40+
41+
// We need to call loadConfig to ensure the isActive() check returns true for global config
42+
loadConfig(resolvedConfigPath);
43+
44+
if (deprecatedConfigPath && globalConfigPath) {
45+
const mergeConfigCopy = 'Merge accounts';
46+
window
47+
.showWarningMessage(
48+
`Found both global and deprecated account configuration files. Click "Merge configuration files" to merge them automatically.`,
49+
mergeConfigCopy
50+
)
51+
.then((selection) => {
52+
if (selection === mergeConfigCopy) {
53+
window.showInformationMessage(
54+
`Merging accounts from ${deprecatedConfigPath} into ${globalConfigPath}. Your existing configuration file will be archived.`
55+
);
56+
57+
const deprecatedConfig = getDeprecatedConfig(deprecatedConfigPath);
58+
const globalConfig = getGlobalConfig();
2659

27-
const path = findConfig(rootPath);
60+
let success = false;
61+
try {
62+
const { initialConfig: GlobalConfigWithPropertiesMerged } =
63+
mergeConfigProperties(globalConfig!, deprecatedConfig!, true);
2864

29-
console.log(`path: ${path}`);
65+
mergeExistingConfigs(
66+
GlobalConfigWithPropertiesMerged,
67+
deprecatedConfig!
68+
);
69+
success = true;
70+
} catch (error) {
71+
throw new Error('Error merging configuration files: ' + error);
72+
}
3073

31-
if (!path) {
74+
if (success) {
75+
window.showInformationMessage(`Config files successfully merged.`);
76+
initializeConfig(rootPath);
77+
}
78+
}
79+
});
3280
return;
3381
}
34-
onLoadPath(path);
3582

36-
loadConfig(path);
83+
onLoadPath(resolvedConfigPath);
3784

3885
if (!validateConfig()) {
39-
throw new Error(`Invalid config could not be loaded: ${path}`);
86+
throw new Error(
87+
`Invalid config could not be loaded: ${resolvedConfigPath}`
88+
);
4089
} else {
4190
commands.executeCommand(EVENTS.ON_CONFIG_UPDATED);
42-
return path;
91+
return resolvedConfigPath;
4392
}
4493
};
4594

src/lib/commands/account.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { commands, ExtensionContext, Uri } from 'vscode';
22
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
3+
import { getAccountIdentifier } from '@hubspot/local-dev-lib/config/getAccountIdentifier';
34
import { COMMANDS } from '../constants';
45

56
export const registerCommands = (context: ExtensionContext) => {
@@ -9,7 +10,7 @@ export const registerCommands = (context: ExtensionContext) => {
910
async (hubspotAccount: CLIAccount_DEPRECATED) => {
1011
const pakUrl = `https://app.hubspot${
1112
hubspotAccount.env === 'qa' ? 'qa' : ''
12-
}.com/personal-access-key/${hubspotAccount.portalId}`;
13+
}.com/personal-access-key/${getAccountIdentifier(hubspotAccount)}`;
1314

1415
commands.executeCommand('vscode.open', Uri.parse(pakUrl));
1516
}
@@ -21,7 +22,7 @@ export const registerCommands = (context: ExtensionContext) => {
2122
async (hubspotAccount: CLIAccount_DEPRECATED) => {
2223
const designManagerUrl = `https://app.hubspot${
2324
hubspotAccount.env === 'qa' ? 'qa' : ''
24-
}.com/design-manager/${hubspotAccount.portalId}`;
25+
}.com/design-manager/${getAccountIdentifier(hubspotAccount)}`;
2526

2627
commands.executeCommand('vscode.open', Uri.parse(designManagerUrl));
2728
}

src/lib/commands/config.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { commands, window, ExtensionContext } from 'vscode';
2-
import { updateStatusBarItems } from '../statusBar';
3-
import { COMMANDS, TRACKED_EVENTS } from '../constants';
4-
import { getDisplayedHubspotPortalInfo } from '../helpers';
5-
import { portalNameInvalid } from '../validation';
6-
import { trackEvent } from '../tracking';
7-
import { showAutoDismissedStatusBarMessage } from '../messaging';
82
import {
93
getConfig,
104
deleteAccount,
@@ -15,7 +9,17 @@ import {
159
getConfigAccounts,
1610
} from '@hubspot/local-dev-lib/config';
1711
import { CLIConfig } from '@hubspot/local-dev-lib/types/Config';
18-
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
12+
import {
13+
CLIAccount,
14+
CLIAccount_DEPRECATED,
15+
} from '@hubspot/local-dev-lib/types/Accounts';
16+
import { getAccountIdentifier } from '@hubspot/local-dev-lib/config/getAccountIdentifier';
17+
import { updateStatusBarItems } from '../statusBar';
18+
import { COMMANDS, TRACKED_EVENTS } from '../constants';
19+
import { getDisplayedHubspotPortalInfo } from '../helpers';
20+
import { portalNameInvalid } from '../validation';
21+
import { trackEvent } from '../tracking';
22+
import { showAutoDismissedStatusBarMessage } from '../messaging';
1923

2024
const showRenameAccountPrompt = (accountToRename: CLIAccount_DEPRECATED) => {
2125
window
@@ -24,7 +28,8 @@ const showRenameAccountPrompt = (accountToRename: CLIAccount_DEPRECATED) => {
2428
})
2529
.then(async (newName: string | undefined) => {
2630
if (newName) {
27-
const oldName = accountToRename.name || accountToRename.portalId;
31+
const oldName =
32+
accountToRename.name || getAccountIdentifier(accountToRename);
2833
const config: CLIConfig | null = getConfig();
2934
let invalidReason = '';
3035
if (config) {
@@ -39,6 +44,7 @@ const showRenameAccountPrompt = (accountToRename: CLIAccount_DEPRECATED) => {
3944
return;
4045
}
4146
renameAccount(String(oldName), newName);
47+
commands.executeCommand(COMMANDS.ACCOUNTS_REFRESH);
4248
showAutoDismissedStatusBarMessage(
4349
`Successfully renamed default account from ${oldName} to ${newName}.`
4450
);
@@ -66,7 +72,7 @@ export const registerCommands = (context: ExtensionContext) => {
6672
typeof defaultAccount === 'string' ||
6773
typeof defaultAccount === 'number'
6874
? defaultAccount
69-
: defaultAccount.name || defaultAccount.portalId;
75+
: defaultAccount.name || getAccountIdentifier(defaultAccount);
7076
console.log('Setting default account to: ', newDefaultAccount);
7177
updateDefaultAccount(newDefaultAccount);
7278
await trackEvent(TRACKED_EVENTS.UPDATE_DEFAULT_ACCOUNT);
@@ -85,19 +91,20 @@ export const registerCommands = (context: ExtensionContext) => {
8591
COMMANDS.CONFIG.SELECT_DEFAULT_ACCOUNT,
8692
async () => {
8793
const defaultAccount = getConfigDefaultAccount();
88-
const portals: CLIAccount_DEPRECATED[] = getConfigAccounts() || [];
94+
const accounts: CLIAccount[] = getConfigAccounts() || [];
8995

90-
if (portals && portals.length !== 0) {
96+
if (accounts && accounts.length !== 0) {
9197
window
9298
.showQuickPick(
93-
portals.map((p: CLIAccount_DEPRECATED) => {
99+
accounts.map((a: CLIAccount) => {
94100
return {
95-
label: getDisplayedHubspotPortalInfo(p),
101+
label: getDisplayedHubspotPortalInfo(a),
96102
description:
97-
defaultAccount === p.portalId || defaultAccount === p.name
103+
defaultAccount === getAccountIdentifier(a) ||
104+
defaultAccount === a.name
98105
? '(default)'
99106
: '',
100-
portal: p,
107+
account: a,
101108
};
102109
}),
103110
{
@@ -107,7 +114,8 @@ export const registerCommands = (context: ExtensionContext) => {
107114
.then(async (selection) => {
108115
if (selection) {
109116
const newDefaultAccount =
110-
selection.portal.name || selection.portal.portalId;
117+
selection.account.name ||
118+
getAccountIdentifier(selection.account);
111119
if (!newDefaultAccount) {
112120
window.showErrorMessage(
113121
'No account selected; Choose an account to set as default'
@@ -140,9 +148,9 @@ export const registerCommands = (context: ExtensionContext) => {
140148
commands.registerCommand(
141149
COMMANDS.CONFIG.DELETE_ACCOUNT,
142150
async (accountToDelete) => {
143-
const portals: CLIAccount_DEPRECATED[] = getConfigAccounts() || [];
151+
const accounts: CLIAccount[] = getConfigAccounts() || [];
144152
const accountIdentifier =
145-
accountToDelete.name || accountToDelete.portalId;
153+
accountToDelete.name || getAccountIdentifier(accountToDelete);
146154

147155
await window
148156
.showInformationMessage(
@@ -152,7 +160,7 @@ export const registerCommands = (context: ExtensionContext) => {
152160
)
153161
.then(async (answer) => {
154162
if (answer === 'Yes') {
155-
if (portals && portals.length === 1) {
163+
if (accounts && accounts.length === 1) {
156164
deleteConfigFile();
157165
showAutoDismissedStatusBarMessage(
158166
`Successfully deleted account ${accountIdentifier}. The config file has been deleted because there are no more authenticated accounts.`
@@ -165,6 +173,7 @@ export const registerCommands = (context: ExtensionContext) => {
165173
}
166174
await trackEvent(TRACKED_EVENTS.DELETE_ACCOUNT);
167175
commands.executeCommand(COMMANDS.REMOTE_FS.HARD_REFRESH);
176+
commands.executeCommand(COMMANDS.ACCOUNTS_REFRESH);
168177
updateStatusBarItems();
169178
}
170179
});

src/lib/helpers.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { dirname } from 'path';
22
import { window, commands, workspace, StatusBarAlignment } from 'vscode';
33
import { getAccountId } from '@hubspot/local-dev-lib/config';
4-
5-
import { COMMANDS } from './constants';
6-
import { CLIConfig } from '@hubspot/local-dev-lib/types/Config';
74
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
5+
import { getAccountIdentifier } from '@hubspot/local-dev-lib/config/getAccountIdentifier';
6+
import { COMMANDS } from './constants';
87

98
const { exec } = require('node:child_process');
109

@@ -17,24 +16,13 @@ export const getRootPath = () => {
1716
return workspaceFolders[0].uri.fsPath;
1817
};
1918

20-
export const getDefaultPortalFromConfig = (config: CLIConfig) => {
21-
return (
22-
config &&
23-
'portals' in config &&
24-
config.portals &&
25-
config.portals.find(
26-
(p: CLIAccount_DEPRECATED) =>
27-
p.portalId === config.defaultPortal || p.name === config.defaultPortal
28-
)
29-
);
30-
};
31-
3219
export const getDisplayedHubspotPortalInfo = (
3320
portalData: CLIAccount_DEPRECATED
3421
) => {
22+
const accountIdentifier = getAccountIdentifier(portalData);
3523
return portalData.name
36-
? `${portalData.name} - ${portalData.portalId}`
37-
: `${portalData.portalId}`;
24+
? `${portalData.name} - ${accountIdentifier}`
25+
: `${accountIdentifier}`;
3826
};
3927

4028
export const runTerminalCommand = async (

src/lib/providers/treedata/accounts.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@ import {
66
TreeItem,
77
TreeItemCollapsibleState,
88
} from 'vscode';
9-
import { getDisplayedHubspotPortalInfo } from '../../helpers';
10-
11-
import { getConfig } from '@hubspot/local-dev-lib/config';
9+
import {
10+
getConfig,
11+
getConfigAccounts,
12+
getConfigDefaultAccount,
13+
} from '@hubspot/local-dev-lib/config';
14+
import { getAccountIdentifier } from '@hubspot/local-dev-lib/config/getAccountIdentifier';
1215
import { CLIConfig } from '@hubspot/local-dev-lib/types/Config';
13-
import { CLIAccount_DEPRECATED } from '@hubspot/local-dev-lib/types/Accounts';
16+
import {
17+
CLIAccount,
18+
CLIAccount_DEPRECATED,
19+
} from '@hubspot/local-dev-lib/types/Accounts';
20+
import { getDisplayedHubspotPortalInfo } from '../../helpers';
1421

15-
const isDefaultPortal = (
16-
portal: CLIAccount_DEPRECATED,
17-
config: CLIConfig | null
18-
) => {
22+
const isDefaultAccount = (account: CLIAccount, config: CLIConfig | null) => {
23+
if (!config) {
24+
return false;
25+
}
26+
const accountIdentifier = getAccountIdentifier(account);
27+
const defaultAccount = getConfigDefaultAccount();
1928
return (
20-
config &&
21-
'defaultPortal' in config &&
22-
(config.defaultPortal === portal.portalId ||
23-
config.defaultPortal === portal.name)
29+
accountIdentifier === defaultAccount || account.name === defaultAccount
2430
);
2531
};
2632

@@ -56,17 +62,18 @@ export class AccountsProvider
5662
return new AccountTreeItem(
5763
name,
5864
p,
59-
{ isDefault: isDefaultPortal(p, this.config) ?? false },
65+
{ isDefault: isDefaultAccount(p, this.config) ?? false },
6066
TreeItemCollapsibleState.None
6167
);
6268
}
6369

6470
getChildren(): Thenable<CLIAccount_DEPRECATED[] | undefined> {
6571
console.log('AccountsProvider:getChildren');
6672
this.config = getConfig();
73+
const accounts = getConfigAccounts();
6774

68-
if (this.config && 'portals' in this.config && this.config.portals) {
69-
return Promise.resolve(this.config.portals);
75+
if (accounts) {
76+
return Promise.resolve(accounts);
7077
}
7178

7279
return Promise.resolve([]);
@@ -89,7 +96,7 @@ export class AccountTreeItem extends TreeItem {
8996
}
9097
this.tooltip = `${options.isDefault ? '* Default Account\n' : ''}${
9198
portalData.name ? `Name: ${portalData.name}\n` : ''
92-
}ID: ${portalData.portalId}\n${
99+
}ID: ${getAccountIdentifier(portalData)}\n${
93100
portalData.env ? `Environment: ${portalData.env}\n` : ''
94101
}${
95102
portalData.sandboxAccountType

0 commit comments

Comments
 (0)