Skip to content

Commit 41023fe

Browse files
authored
Merge pull request #502 from netlify/verythorough/improve-link-prompts
Improve prompts for `link` command
2 parents f569721 + 8208703 commit 41023fe

File tree

1 file changed

+91
-35
lines changed

1 file changed

+91
-35
lines changed

src/utils/link/link-by-prompt.js

Lines changed: 91 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,29 @@ const { track } = require('../telemetry')
88
module.exports = async function linkPrompts(context) {
99
const { api, state } = context.netlify
1010

11-
const SITE_NAME_PROMPT = 'Choose from a list of your sites'
12-
const SITE_ID_PROMPT = `Use a site ID`
11+
const SITE_NAME_PROMPT = 'Search by full or partial site name'
12+
const SITE_LIST_PROMPT = 'Choose from a list of your recently updated sites'
13+
const SITE_ID_PROMPT = 'Enter a site ID'
1314

14-
let GIT_REMOTE_PROMPT = `Use a current git remote URL`
15+
let GIT_REMOTE_PROMPT = 'Use the current git remote origin URL'
1516
let site
1617
// Get git remote data if exists
1718
const repoInfo = await getRepoData()
1819

19-
const LinkChoices = [SITE_NAME_PROMPT, SITE_ID_PROMPT]
20+
const LinkChoices = [SITE_NAME_PROMPT, SITE_LIST_PROMPT, SITE_ID_PROMPT]
2021

2122
let repoUrl = ''
2223
if (!repoInfo.error) {
2324
repoUrl = `https://${repoInfo.host}/${repoInfo.remoteData.repo}`
2425

25-
GIT_REMOTE_PROMPT = `Use current git remote url ${repoUrl}`
26+
GIT_REMOTE_PROMPT = `Use current git remote origin (${repoUrl})`
2627

2728
// Add git GIT_REMOTE_PROMPT if in a repo. TODO refactor to non mutating
2829
LinkChoices.splice(0, 0, GIT_REMOTE_PROMPT)
2930
}
3031

3132
context.log()
32-
context.log(`${chalk.cyanBright('netlify link')} will connect a site in app.netlify.com to this folder`)
33+
context.log(`${chalk.cyanBright('netlify link')} will connect this folder to a site on Netlify`)
3334
context.log()
3435
const { linkType } = await inquirer.prompt([
3536
{
@@ -52,11 +53,12 @@ module.exports = async function linkPrompts(context) {
5253
context.error(new Error(`No git remote found in this directory`))
5354
}
5455
context.log()
55-
context.log(`Fetching sites and looking for site connected to "${repoUrl}" repo`)
56+
context.log(`Looking for sites connected to '${repoUrl}'...`)
57+
context.log()
5658
const sites = await api.listSites()
5759

5860
if (isEmpty(sites)) {
59-
context.error(new Error(`No sites found in your netlify account`))
61+
context.error(new Error(`You don't have any sites yet. Run ${chalk.cyanBright('netlify sites:create')} to create a site.`))
6062
}
6163

6264
const matchingSites = sites.filter(s => {
@@ -70,9 +72,9 @@ module.exports = async function linkPrompts(context) {
7072
context.log()
7173
context.log(`No site found with the remote ${repoInfo.repo_path}.
7274
73-
Double check you are in the correct working directory & a remote git repo is configured.
75+
Double check you are in the correct working directory and a remote origin repo is configured.
7476
75-
Run ${chalk.cyanBright('`git remote -v`')} to see a list of your git remotes.`)
77+
Run ${chalk.cyanBright('git remote -v')} to see a list of your git remotes.`)
7678

7779
context.exit()
7880
}
@@ -81,34 +83,85 @@ Run ${chalk.cyanBright('`git remote -v`')} to see a list of your git remotes.`)
8183
if (matchingSites.length === 1) {
8284
site = matchingSites[0]
8385
} else if (matchingSites.length > 1) {
84-
// Matches multiple sites. Users much choose which to link.
85-
console.log()
86-
console.log(`Found ${matchingSites.length} matching sites! Please choose one:`)
87-
88-
const siteChoices = matchingSites.map(site => {
89-
return `${site.ssl_url} - ${site.name} - ${site.id}`
90-
})
86+
// Matches multiple sites. Users must choose which to link.
87+
console.log(`Found ${matchingSites.length} matching sites!`)
9188

9289
// Prompt which options
93-
const { siteToConnect } = await inquirer.prompt([
90+
const { selectedSite } = await inquirer.prompt([
9491
{
9592
type: 'list',
96-
name: 'siteToConnect',
97-
message: 'Which site do you want to link to?',
98-
choices: siteChoices
93+
name: 'selectedSite',
94+
message: 'Which site do you want to link?',
95+
choices: matchingSites.map(site => ({
96+
name: `${site.name} - ${site.ssl_url}`,
97+
value: site
98+
}))
9999
}
100100
])
101-
102-
const siteName = siteToConnect.split(' ')[0]
103-
site = matchingSites.filter(site => {
104-
const url = site.ssl_url || site.url
105-
return siteName === url
106-
})[0]
101+
if (!selectedSite) {
102+
context.error('No site selected')
103+
}
104+
site = selectedSite
107105
}
108106
break
109107
}
110108
case SITE_NAME_PROMPT: {
111109
kind = 'byName'
110+
const { searchTerm } = await inquirer.prompt([
111+
{
112+
type: 'input',
113+
name: 'searchTerm',
114+
message: 'Enter the site name (or just part of it):'
115+
}
116+
])
117+
context.log(`Looking for sites with names containing '${searchTerm}'...`)
118+
context.log()
119+
120+
let matchingSites
121+
try {
122+
matchingSites = await api.listSites({
123+
name: searchTerm,
124+
filter: 'all'
125+
})
126+
} catch (e) {
127+
if (e.status === 404) {
128+
context.error(`'${searchTerm}' not found`)
129+
} else {
130+
context.error(e)
131+
}
132+
}
133+
134+
if (isEmpty(matchingSites)) {
135+
context.error(`No site names found containing '${searchTerm}'.
136+
137+
Run ${chalk.cyanBright('netlify link')} again to try a new search,
138+
or run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
139+
}
140+
141+
if (matchingSites.length > 1) {
142+
console.log(`Found ${matchingSites.length} matching sites!`)
143+
const { selectedSite } = await inquirer.prompt([
144+
{
145+
type: 'list',
146+
name: 'selectedSite',
147+
message: 'Which site do you want to link?',
148+
paginated: true,
149+
choices: matchingSites.map(site => ({ name: site.name, value: site }))
150+
}
151+
])
152+
if (!selectedSite) {
153+
context.error('No site selected')
154+
}
155+
site = selectedSite
156+
} else {
157+
site = matchingSites[0]
158+
}
159+
break
160+
}
161+
case SITE_LIST_PROMPT: {
162+
kind = 'fromList'
163+
context.log(`Fetching recently updated sites...`)
164+
context.log()
112165

113166
let sites
114167
try {
@@ -117,20 +170,23 @@ Run ${chalk.cyanBright('`git remote -v`')} to see a list of your git remotes.`)
117170
context.error(e)
118171
}
119172

120-
if (sites.length === 0) {
121-
context.error(`You don't have any sites. Use netlify 'sites:create' to create a site.`)
173+
if (isEmpty(sites)) {
174+
context.error(`You don't have any sites yet. Run ${chalk.cyanBright('netlify sites:create')} to create a site.`)
122175
}
123176

124-
const siteSelection = await inquirer.prompt([
177+
const { selectedSite } = await inquirer.prompt([
125178
{
126179
type: 'list',
127-
name: 'siteName',
128-
message: 'What is the name of the site?',
180+
name: 'selectedSite',
181+
message: 'Which site do you want to link?',
129182
paginated: true,
130183
choices: sites.map(site => ({ name: site.name, value: site }))
131184
}
132185
])
133-
site = siteSelection.siteName
186+
if (!selectedSite) {
187+
context.error('No site selected')
188+
}
189+
site = selectedSite
134190
break
135191
}
136192
case SITE_ID_PROMPT: {
@@ -139,15 +195,15 @@ Run ${chalk.cyanBright('`git remote -v`')} to see a list of your git remotes.`)
139195
{
140196
type: 'input',
141197
name: 'siteId',
142-
message: 'What is the site-id of the site?'
198+
message: 'What is the site ID?'
143199
}
144200
])
145201

146202
try {
147203
site = await api.getSite({ siteId })
148204
} catch (e) {
149205
if (e.status === 404) {
150-
context.error(new Error(`Site id ${siteId} not found`))
206+
context.error(new Error(`Site ID '${siteId}' not found`))
151207
} else {
152208
context.error(e)
153209
}

0 commit comments

Comments
 (0)