@@ -8,28 +8,29 @@ const { track } = require('../telemetry')
8
8
module . exports = async function linkPrompts ( context ) {
9
9
const { api, state } = context . netlify
10
10
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'
13
14
14
- let GIT_REMOTE_PROMPT = ` Use a current git remote URL`
15
+ let GIT_REMOTE_PROMPT = ' Use the current git remote origin URL'
15
16
let site
16
17
// Get git remote data if exists
17
18
const repoInfo = await getRepoData ( )
18
19
19
- const LinkChoices = [ SITE_NAME_PROMPT , SITE_ID_PROMPT ]
20
+ const LinkChoices = [ SITE_NAME_PROMPT , SITE_LIST_PROMPT , SITE_ID_PROMPT ]
20
21
21
22
let repoUrl = ''
22
23
if ( ! repoInfo . error ) {
23
24
repoUrl = `https://${ repoInfo . host } /${ repoInfo . remoteData . repo } `
24
25
25
- GIT_REMOTE_PROMPT = `Use current git remote url ${ repoUrl } `
26
+ GIT_REMOTE_PROMPT = `Use current git remote origin ( ${ repoUrl } ) `
26
27
27
28
// Add git GIT_REMOTE_PROMPT if in a repo. TODO refactor to non mutating
28
29
LinkChoices . splice ( 0 , 0 , GIT_REMOTE_PROMPT )
29
30
}
30
31
31
32
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 ` )
33
34
context . log ( )
34
35
const { linkType } = await inquirer . prompt ( [
35
36
{
@@ -52,11 +53,12 @@ module.exports = async function linkPrompts(context) {
52
53
context . error ( new Error ( `No git remote found in this directory` ) )
53
54
}
54
55
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 ( )
56
58
const sites = await api . listSites ( )
57
59
58
60
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. ` ) )
60
62
}
61
63
62
64
const matchingSites = sites . filter ( s => {
@@ -70,9 +72,9 @@ module.exports = async function linkPrompts(context) {
70
72
context . log ( )
71
73
context . log ( `No site found with the remote ${ repoInfo . repo_path } .
72
74
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.
74
76
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.` )
76
78
77
79
context . exit ( )
78
80
}
@@ -81,34 +83,85 @@ Run ${chalk.cyanBright('`git remote -v`')} to see a list of your git remotes.`)
81
83
if ( matchingSites . length === 1 ) {
82
84
site = matchingSites [ 0 ]
83
85
} 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!` )
91
88
92
89
// Prompt which options
93
- const { siteToConnect } = await inquirer . prompt ( [
90
+ const { selectedSite } = await inquirer . prompt ( [
94
91
{
95
92
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
+ } ) )
99
99
}
100
100
] )
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
107
105
}
108
106
break
109
107
}
110
108
case SITE_NAME_PROMPT : {
111
109
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 ( )
112
165
113
166
let sites
114
167
try {
@@ -117,20 +170,23 @@ Run ${chalk.cyanBright('`git remote -v`')} to see a list of your git remotes.`)
117
170
context . error ( e )
118
171
}
119
172
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.` )
122
175
}
123
176
124
- const siteSelection = await inquirer . prompt ( [
177
+ const { selectedSite } = await inquirer . prompt ( [
125
178
{
126
179
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 ?' ,
129
182
paginated : true ,
130
183
choices : sites . map ( site => ( { name : site . name , value : site } ) )
131
184
}
132
185
] )
133
- site = siteSelection . siteName
186
+ if ( ! selectedSite ) {
187
+ context . error ( 'No site selected' )
188
+ }
189
+ site = selectedSite
134
190
break
135
191
}
136
192
case SITE_ID_PROMPT : {
@@ -139,15 +195,15 @@ Run ${chalk.cyanBright('`git remote -v`')} to see a list of your git remotes.`)
139
195
{
140
196
type : 'input' ,
141
197
name : 'siteId' ,
142
- message : 'What is the site-id of the site ?'
198
+ message : 'What is the site ID ?'
143
199
}
144
200
] )
145
201
146
202
try {
147
203
site = await api . getSite ( { siteId } )
148
204
} catch ( e ) {
149
205
if ( e . status === 404 ) {
150
- context . error ( new Error ( `Site id ${ siteId } not found` ) )
206
+ context . error ( new Error ( `Site ID ' ${ siteId } ' not found` ) )
151
207
} else {
152
208
context . error ( e )
153
209
}
0 commit comments