-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hs open command #344
Merged
Merged
hs open command #344
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcef7fb
hs open command
drewjenkins cb5df4f
Code review changes
drewjenkins 52564f9
fix import
drewjenkins adf0497
Pull in inquirer for and remove indices
drewjenkins 4942d70
remove accidentally added file
drewjenkins 56d0b39
remove opn from yarn.lock
drewjenkins 6955e03
Merge branch 'master' of github.com:HubSpot/hubspot-cms-tools into hs…
drewjenkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const { | ||
addPortalOptions, | ||
addConfigOptions, | ||
getPortalId, | ||
addUseEnvironmentOptions, | ||
} = require('../lib/commonOpts'); | ||
const { trackCommandUsage } = require('../lib/usageTracking'); | ||
const { logSiteLinks, getSiteLinksAsArray, openLink } = require('../lib/links'); | ||
const inquirer = require('inquirer'); | ||
|
||
const separator = ' => '; | ||
const createListPrompt = async portalId => | ||
inquirer.prompt([ | ||
{ | ||
type: 'rawlist', | ||
look: false, | ||
name: 'open', | ||
pageSize: 20, | ||
message: 'Select a link to open', | ||
choices: getSiteLinksAsArray(portalId).map( | ||
l => `${l.shortcut}${separator}${l.url}` | ||
), | ||
filter: val => val.split(separator)[0], | ||
}, | ||
]); | ||
|
||
exports.command = 'open [shortcut]'; | ||
exports.describe = 'Quickly open a page to HubSpot in your browser'; | ||
|
||
exports.handler = async options => { | ||
const { shortcut, list } = options; | ||
const portalId = getPortalId(options); | ||
|
||
trackCommandUsage('open', { shortcut }, portalId); | ||
|
||
if (shortcut === undefined && !list) { | ||
const choice = await createListPrompt(portalId); | ||
openLink(portalId, choice.open); | ||
} else if (list) { | ||
logSiteLinks(portalId); | ||
return; | ||
} else { | ||
openLink(portalId, shortcut); | ||
} | ||
}; | ||
|
||
exports.builder = yargs => { | ||
yargs.positional('[shortcut]', { | ||
describe: "Shortcut of the link you'd like to open", | ||
type: 'string', | ||
}); | ||
|
||
yargs.option('list', { | ||
alias: 'l', | ||
describe: 'List all supported shortcuts', | ||
type: 'boolean', | ||
}); | ||
|
||
yargs.example([ | ||
['$0 open'], | ||
['$0 open --list'], | ||
['$0 open settings'], | ||
['$0 open settings/navigation'], | ||
['$0 open sn'], | ||
]); | ||
|
||
addConfigOptions(yargs, true); | ||
addPortalOptions(yargs, true); | ||
addUseEnvironmentOptions(yargs, true); | ||
|
||
return yargs; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
const { getEnv } = require('@hubspot/cms-lib/lib/config'); | ||
const { ENVIRONMENTS } = require('@hubspot/cms-lib/lib/constants'); | ||
const { getHubSpotWebsiteOrigin } = require('@hubspot/cms-lib/lib/urls'); | ||
const { logger } = require('@hubspot/cms-lib/logger'); | ||
const chalk = require('chalk'); | ||
const { table, getBorderCharacters } = require('table'); | ||
const open = require('open'); | ||
|
||
const logSiteLinks = portalId => { | ||
const linksAsArray = getSiteLinksAsArray(portalId).map(l => [ | ||
`${l.shortcut}${l.alias ? ` [alias: ${l.alias}]` : ''}`, | ||
'=>', | ||
l.url, | ||
]); | ||
|
||
linksAsArray.unshift([chalk.bold('Shortcut'), '', chalk.bold('Url')]); | ||
|
||
const tableConfig = { | ||
singleLine: true, | ||
border: getBorderCharacters(`void`), | ||
}; | ||
|
||
logger.log(table(linksAsArray, tableConfig)); | ||
}; | ||
|
||
const getSiteLinksAsArray = portalId => | ||
Object.values(getSiteLinks(portalId)).sort((a, b) => | ||
a.shortcut < b.shortcut ? -1 : 1 | ||
); | ||
|
||
const getSiteLinks = portalId => { | ||
const baseUrl = getHubSpotWebsiteOrigin( | ||
getEnv() === 'qa' ? ENVIRONMENTS.QA : ENVIRONMENTS.PROD | ||
); | ||
|
||
return { | ||
APPS_MARKETPLACE: { | ||
shortcut: 'apps-marketplace', | ||
alias: 'apm', | ||
url: `${baseUrl}/ecosystem/${portalId}/marketplace/apps`, | ||
}, | ||
ASSET_MARKETPLACE: { | ||
shortcut: 'asset-marketplace', | ||
alias: 'asm', | ||
url: `${baseUrl}/ecosystem/${portalId}/marketplace/products`, | ||
}, | ||
CONTENT_STAGING: { | ||
shortcut: 'content-staging', | ||
alias: 'cs', | ||
url: `${baseUrl}/content/${portalId}/staging`, | ||
}, | ||
DESIGN_MANAGER: { | ||
shortcut: 'design-manager', | ||
alias: 'dm', | ||
url: `${baseUrl}/design-manager/${portalId}`, | ||
}, | ||
DOCS: { | ||
shortcut: 'docs', | ||
url: 'https://developers.hubspot.com', | ||
}, | ||
FILE_MANAGER: { | ||
shortcut: 'file-manager', | ||
alias: 'fm', | ||
url: `${baseUrl}/files/${portalId}`, | ||
}, | ||
FORUMS: { | ||
shortcut: 'forums', | ||
url: 'https://community.hubspot.com', | ||
}, | ||
HUBDB: { | ||
shortcut: 'hubdb', | ||
alias: 'hdb', | ||
url: `${baseUrl}/hubdb/${portalId}`, | ||
}, | ||
SETTINGS: { | ||
shortcut: 'settings', | ||
alias: 's', | ||
url: `${baseUrl}/settings/${portalId}`, | ||
}, | ||
SETTINGS_NAVIGATION: { | ||
shortcut: 'settings/navigation', | ||
alias: 'sn', | ||
url: `${baseUrl}/menus/${portalId}/edit/`, | ||
}, | ||
SETTINGS_PAGE: { | ||
shortcut: 'settings/page', | ||
drewjenkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
alias: 'sp', | ||
url: `${baseUrl}/settings/${portalId}/website/pages/all-domains/page-templates`, | ||
}, | ||
SETTINGS_URL_REDIRECTS: { | ||
shortcut: 'settings/url-redirects', | ||
alias: 'sur', | ||
url: `${baseUrl}/domains/${portalId}/url-redirects`, | ||
}, | ||
PURCHASED_ASSETS: { | ||
shortcut: 'purchased-assets', | ||
alias: 'pa', | ||
url: `${baseUrl}/marketplace/${portalId}/manage-purchases`, | ||
}, | ||
|
||
WEBSITE_PAGES: { | ||
shortcut: 'website-pages', | ||
alias: 'wp', | ||
url: `${baseUrl}/website/${portalId}/pages/site`, | ||
}, | ||
}; | ||
}; | ||
|
||
const openLink = (portalId, shortcut) => { | ||
const match = Object.values(getSiteLinks(portalId)).find( | ||
l => l.shortcut === shortcut || (l.alias && l.alias === shortcut) | ||
); | ||
|
||
if (!match) { | ||
logger.error( | ||
`We couldn't find a shortcut matching ${shortcut}. Type 'hs open list' to see a list of available shortcuts` | ||
); | ||
return; | ||
} | ||
|
||
open(match.url, { url: true }); | ||
logger.success(`We opened ${match.url} in your browser`); | ||
}; | ||
|
||
module.exports = { | ||
getSiteLinks, | ||
getSiteLinksAsArray, | ||
logSiteLinks, | ||
openLink, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this is cool. We should use it more.