Skip to content

Commit

Permalink
Merge pull request #138 from HubSpot/file-management
Browse files Browse the repository at this point in the history
Adding File Management
  • Loading branch information
miketalley authored Dec 9, 2022
2 parents ff9942d + 4f98299 commit 042d403
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 53 deletions.
25 changes: 18 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"Linters"
],
"icon": "images/hubspot-logo.png",

"main": "./dist/extension.js",
"activationEvents": [
"onStartupFinished"
Expand Down Expand Up @@ -273,6 +272,15 @@
{
"id": "hubspot.viewsWelcome.accountAuth",
"name": "Authentication"
},
{
"id": "hubspot.viewsWelcome.tools",
"name": "Tools",
"when": "hubspot.versionChecksComplete && hubspot.terminal.versions.installed.npm && !hubspot.terminal.versions.installed.hs || hubspot.updateAvailableForCLI"
},
{
"id": "hubspot.treedata.helpAndFeedback",
"name": "Help & Feedback"
}
]
},
Expand All @@ -286,11 +294,22 @@
"view": "hubspot.viewsWelcome.accountAuth",
"contents": "In order to use HubSpot VSCode features, you need to authenticate your account(s). \n[Authenticate HubSpot Account](command:hubspot.auth.onClickAuthorize)\nTo learn more about authentication using personal access keys, [read our docs](https://developers.hubspot.com/docs/cms/personal-access-key).",
"when": "hubspot.configPath"
},
{
"view": "hubspot.viewsWelcome.tools",
"contents": "In order to use additional features that are not currently available within the HubSpot VS Code extension, you can install the HubSpot CLI (via npm) using the button below. To see other available installation options, [check out the documentation](https://developers.hubspot.com/docs/cms/developer-reference/local-development-cli). \n[Install CLI](command:hubspot.hs.install)",
"when": "!hubspot.terminal.versions.installed.hs"
},
{
"view": "hubspot.viewsWelcome.tools",
"contents": "An update is available for the [HubSpot CLI](https://developers.hubspot.com/docs/cms/developer-reference/local-development-cli). \n[Update CLI](command:hubspot.hs.update)",
"when": "hubspot.updateAvailableForCLI"
}
]
},
"dependencies": {
"@hubspot/cli-lib": "^4.1.4-beta.1",
"compare-versions": "^5.0.1",
"fs-extra": "^9.0.1",
"js-yaml": "^4.1.0",
"node-fetch": "^3.1.1",
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import { initializeStatusBar } from './lib/statusBar';
import { getRootPath } from './lib/helpers';
import { initializeProviders } from './lib/providers';
import { initializeConfig } from './lib/auth';
import { initializeTerminal } from './lib/terminal';
import { registerEvents } from './lib/events';

export const activate = async (context: vscode.ExtensionContext) => {
console.log('Activating Extension...');
const rootPath = getRootPath();

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

initializeProviders(context);
initializeTerminal(context);
initializeStatusBar(context);

initializeConfig(rootPath);
Expand Down
9 changes: 4 additions & 5 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from 'vscode';
import { Portal } from './types';
import { COMMANDS } from './constants';
import { COMMANDS, EVENTS } from './constants';

const { findConfig, loadConfig, validateConfig } = require('@hubspot/cli-lib');
const { setConfig, setConfigPath } = require('@hubspot/cli-lib/lib/config');
Expand All @@ -12,7 +11,7 @@ const onLoadPath = (configPath: string) => {
configPath
);
if (!configPath) {
vscode.commands.executeCommand(COMMANDS.CONFIG_SET_DEFAULT_ACCOUNT, null);
vscode.commands.executeCommand(COMMANDS.CONFIG.SET_DEFAULT_ACCOUNT, null);
setConfig(null);
setConfigPath(null);
}
Expand All @@ -39,7 +38,7 @@ export const loadHubspotConfigFile = (rootPath: string) => {
if (!validateConfig()) {
throw new Error(`Invalid config could not be loaded: ${path}`);
} else {
vscode.commands.executeCommand(COMMANDS.ON_CONFIG_UPDATED);
vscode.commands.executeCommand(EVENTS.ON_CONFIG_UPDATED);
return path;
}
};
Expand All @@ -50,7 +49,7 @@ export const initializeConfig = (rootPath: string) => {
if (configPath) {
console.log(`HubSpot config loaded from: ${configPath}`);
vscode.commands.executeCommand(
COMMANDS.ON_CONFIG_FOUND,
EVENTS.ON_CONFIG_FOUND,
rootPath,
configPath
);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/commands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as vscode from 'vscode';
import { registerCommands as registerConfigCommands } from './commands/config';
import { registerCommands as registerAuthCommands } from './commands/auth';
import { registerCommands as registerTerminalCommands } from './commands/terminal';
import { registerCommands as registerModuleCommands } from './commands/module';

export const registerCommands = (context: vscode.ExtensionContext) => {
registerConfigCommands(context);
registerAuthCommands(context);
registerTerminalCommands(context);
registerModuleCommands(context);
};
26 changes: 3 additions & 23 deletions src/lib/commands/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
setLintingEnabledState,
} from '../../lib/lint';
import { updateStatusBarItems } from '../../lib/statusBar';
import { COMMANDS } from '../constants';
import { COMMANDS, EVENTS } from '../constants';
import { loadHubspotConfigFile } from '../auth';

let configFoundAndLoaded = false;
Expand All @@ -14,11 +14,11 @@ let hubspotConfigWatcher: fs.FSWatcher | null;
export const registerCommands = (context: vscode.ExtensionContext) => {
context.subscriptions.push(
vscode.commands.registerCommand(
COMMANDS.ON_CONFIG_FOUND,
EVENTS.ON_CONFIG_FOUND,
(rootPath, configPath) => {
if (!configFoundAndLoaded) {
configFoundAndLoaded = true;
console.log(COMMANDS.ON_CONFIG_FOUND);
console.log(EVENTS.ON_CONFIG_FOUND);
setLintingEnabledState();
context.subscriptions.push(getUpdateLintingOnConfigChange());
updateStatusBarItems();
Expand Down Expand Up @@ -47,24 +47,4 @@ export const registerCommands = (context: vscode.ExtensionContext) => {
}
)
);

context.subscriptions.push(
vscode.commands.registerCommand(COMMANDS.ON_CONFIG_UPDATED, () => {
console.log(COMMANDS.ON_CONFIG_UPDATED);
vscode.commands.executeCommand(COMMANDS.ACCOUNTS_REFRESH);
updateStatusBarItems();
})
);

context.subscriptions.push(
vscode.commands.registerCommand(COMMANDS.AUTHORIZE_ACCOUNT, async () => {
const authUrl =
'https://app.hubspot.com/l/personal-access-key/auth/vscode';

const callableUri = await vscode.env.asExternalUri(
vscode.Uri.parse(authUrl)
);
await vscode.env.openExternal(callableUri);
})
);
};
8 changes: 4 additions & 4 deletions src/lib/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const showRenameAccountPrompt = (accountToRename: Portal) => {
export const registerCommands = (context: vscode.ExtensionContext) => {
context.subscriptions.push(
vscode.commands.registerCommand(
COMMANDS.CONFIG_SET_DEFAULT_ACCOUNT,
COMMANDS.CONFIG.SET_DEFAULT_ACCOUNT,
(defaultAccount, { silenceNotification = false } = {}) => {
if (!defaultAccount) return;
const newDefaultAccount =
Expand All @@ -60,7 +60,7 @@ export const registerCommands = (context: vscode.ExtensionContext) => {

context.subscriptions.push(
vscode.commands.registerCommand(
COMMANDS.CONFIG_SELECT_DEFAULT_ACCOUNT,
COMMANDS.CONFIG.SELECT_DEFAULT_ACCOUNT,
async () => {
const config = getConfig();
if (config && config.portals) {
Expand Down Expand Up @@ -99,7 +99,7 @@ export const registerCommands = (context: vscode.ExtensionContext) => {

context.subscriptions.push(
vscode.commands.registerCommand(
COMMANDS.CONFIG_RENAME_ACCOUNT,
COMMANDS.CONFIG.RENAME_ACCOUNT,
async (accountToRename) => {
showRenameAccountPrompt(accountToRename);
}
Expand All @@ -108,7 +108,7 @@ export const registerCommands = (context: vscode.ExtensionContext) => {

context.subscriptions.push(
vscode.commands.registerCommand(
COMMANDS.CONFIG_DELETE_ACCOUNT,
COMMANDS.CONFIG.DELETE_ACCOUNT,
async (accountToDelete) => {
const config = getConfig();
const accountIdentifier =
Expand Down
132 changes: 132 additions & 0 deletions src/lib/commands/terminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import * as vscode from 'vscode';
import { compare } from 'compare-versions';
import { checkTerminalCommandVersion, runTerminalCommand } from '../helpers';
import { COMMANDS, POLLING_INTERVALS } from '../constants';

export const registerCommands = (context: vscode.ExtensionContext) => {
context.subscriptions.push(
vscode.commands.registerCommand(COMMANDS.HUBSPOT_CLI.INSTALL, () => {
const terminal = vscode.window.createTerminal();

terminal.show();

const hubspotInstalledPoll = setInterval(async () => {
const hsVersion = await vscode.commands.executeCommand(
COMMANDS.VERSION_CHECK.HS
);

if (hsVersion) {
clearInterval(hubspotInstalledPoll);
vscode.window.showInformationMessage(
`HubSpot CLI version ${hsVersion} installed.`
);
}
}, POLLING_INTERVALS.FAST);

terminal.sendText("echo 'Installing the HubSpot CLI.'");
terminal.sendText('npm i -g @hubspot/cli@latest');
terminal.sendText(
"echo 'Installation complete. You can now close this terminal window.'"
);
})
);

context.subscriptions.push(
vscode.commands.registerCommand(COMMANDS.HUBSPOT_CLI.UPDATE, async () => {
const terminal = vscode.window.createTerminal();
const latestVersion = await vscode.commands.executeCommand(
COMMANDS.VERSION_CHECK.HS_LATEST
);
terminal.show();

const hubspotUpdatedPoll = setInterval(async () => {
const hsVersion = await vscode.commands.executeCommand(
COMMANDS.VERSION_CHECK.HS
);

if (hsVersion === latestVersion) {
clearInterval(hubspotUpdatedPoll);
vscode.window.showInformationMessage(
`HubSpot CLI updated to version ${latestVersion}.`
);
}
}, POLLING_INTERVALS.FAST);

terminal.sendText("echo 'Updating the HubSpot CLI.'");
terminal.sendText('npm i -g @hubspot/cli@latest');
terminal.sendText(
"echo 'Update complete. You can now close this terminal window.'"
);
})
);

context.subscriptions.push(
vscode.commands.registerCommand(
COMMANDS.VERSION_CHECK.HS_LATEST,
async () => {
const hsVersion = (await runTerminalCommand('hs --version')).trim();
const latestHsVersion = (
await runTerminalCommand(
`npm info @hubspot/cli@latest | grep 'latest: ' | sed -n -e 's/^latest: //p'`
)
).trim();
vscode.commands.executeCommand(
'setContext',
'hubspot.terminal.versions.latest.hs',
hsVersion
);
console.log('latestVersion: ', latestHsVersion);

console.log(
'Newer CLI Version Available: ',
compare(latestHsVersion, hsVersion, '>')
);

vscode.commands.executeCommand(
'setContext',
'hubspot.updateAvailableForCLI',
compare(latestHsVersion, hsVersion, '>')
);

return latestHsVersion;
}
)
);

context.subscriptions.push(
vscode.commands.registerCommand(COMMANDS.VERSION_CHECK.HS, async () => {
const hsVersion = await checkTerminalCommandVersion('hs');
vscode.commands.executeCommand(
'setContext',
'hubspot.terminal.versions.installed.hs',
hsVersion
);
vscode.commands.executeCommand(
'setContext',
'hubspot.versionChecksComplete',
true
);

console.log('hsVersion: ', hsVersion);
if (hsVersion) {
vscode.commands.executeCommand(COMMANDS.VERSION_CHECK.HS_LATEST);
}

return hsVersion;
})
);

context.subscriptions.push(
vscode.commands.registerCommand(COMMANDS.VERSION_CHECK.NPM, async () => {
const npmVersion = await checkTerminalCommandVersion('npm');
vscode.commands.executeCommand(
'setContext',
'hubspot.terminal.versions.installed.npm',
npmVersion
);

console.log('npmVersion: ', npmVersion);
return npmVersion;
})
);
};
Loading

0 comments on commit 042d403

Please sign in to comment.