1
1
const Command = require ( '../../base' )
2
- const { getAddons, createAddon } = require ( 'netlify/src/addons' )
2
+ const {
3
+ getAddons,
4
+ createAddon
5
+ } = require ( 'netlify/src/addons' )
3
6
const parseRawFlags = require ( '../../utils/parseRawFlags' )
7
+ const getAddonManifest = require ( '../../utils/addons/api' )
8
+ const {
9
+ requiredConfigValues,
10
+ missingConfigValues,
11
+ updateConfigValues
12
+ } = require ( '../../utils/addons/validation' )
13
+ const generatePrompts = require ( '../../utils/addons/prompts' )
14
+ const render = require ( '../../utils/addons/render' )
15
+ const chalk = require ( 'chalk' )
16
+ const inquirer = require ( 'inquirer' )
4
17
5
18
class addonsCreateCommand extends Command {
6
19
async run ( ) {
7
20
const accessToken = await this . authenticate ( )
8
- const { args, raw } = this . parse ( addonsCreateCommand )
9
- const { api, site } = this . netlify
21
+ const {
22
+ args,
23
+ raw
24
+ } = this . parse ( addonsCreateCommand )
25
+ const {
26
+ api,
27
+ site
28
+ } = this . netlify
10
29
11
30
const addonName = args . name
12
31
13
32
if ( ! addonName ) {
14
- this . log ( 'Please provide an addon name to provision' )
33
+ this . log ( 'Please provide an add-on name to provision' )
15
34
// this.log(util.inspect(myObject, false, null, true /* enable colors */))
16
35
this . exit ( )
17
36
}
@@ -23,56 +42,135 @@ class addonsCreateCommand extends Command {
23
42
return false
24
43
}
25
44
45
+ const siteData = await api . getSite ( {
46
+ siteId
47
+ } )
48
+ // this.log(site)
26
49
const addons = await getAddons ( siteId , accessToken )
27
50
28
51
if ( typeof addons === 'object' && addons . error ) {
29
52
this . log ( 'API Error' , addons )
30
53
return false
31
54
}
32
55
33
- const siteData = await api . getSite ( { siteId } )
34
-
35
56
// Filter down addons to current args.name
36
57
const currentAddon = addons . find ( ( addon ) => addon . service_path === `/.netlify/${ addonName } ` )
37
58
38
- if ( currentAddon . id ) {
39
- this . log ( `Addon ${ addonName } already exists for ${ siteData . name } ` )
40
- this . log ( `> Run \`netlify addons:update ${ addonName } \` to update settings` )
41
- this . log ( `> Run \`netlify addons:delete ${ addonName } \` to delete this addon` )
59
+ // GET flags from `raw` data
60
+ const rawFlags = parseRawFlags ( raw )
61
+
62
+ if ( currentAddon && currentAddon . id ) {
63
+ this . log ( `The "${ addonName } add-on" already exists for ${ siteData . name } ` )
64
+ this . log ( )
65
+ const cmd = chalk . cyan ( `\`netlify addons:config ${ addonName } \`` )
66
+ this . log ( `- To update this add-on run: ${ cmd } ` )
67
+ const deleteCmd = chalk . cyan ( `\`netlify addons:delete ${ addonName } \`` )
68
+ this . log ( `- To remove this add-on run: ${ deleteCmd } ` )
69
+ this . log ( )
42
70
return false
43
71
}
44
72
45
- const settings = {
46
- siteId : siteId ,
47
- addon : addonName ,
48
- config : parseRawFlags ( raw )
73
+ const manifest = await getAddonManifest ( addonName , accessToken )
74
+
75
+ let configValues = rawFlags
76
+ if ( manifest . config ) {
77
+ const required = requiredConfigValues ( manifest . config )
78
+ const missingValues = missingConfigValues ( required , rawFlags )
79
+ console . log ( `Starting the setup for "${ addonName } add-on"` )
80
+ console . log ( )
81
+
82
+ if ( Object . keys ( rawFlags ) . length ) {
83
+ const newConfig = updateConfigValues ( manifest . config , { } , rawFlags )
84
+
85
+ if ( missingValues . length ) {
86
+ /* Warn user of missing required values */
87
+ console . log ( `${ chalk . redBright . underline . bold ( `Error: Missing required configuration for "${ addonName } add-on"` ) } ` )
88
+ console . log ( `Please supply the configuration values as CLI flags` )
89
+ console . log ( )
90
+ render . missingValues ( missingValues , manifest )
91
+ console . log ( )
92
+ }
93
+
94
+
95
+ await createSiteAddon ( {
96
+ addonName,
97
+ settings : {
98
+ siteId : siteId ,
99
+ addon : addonName ,
100
+ config : newConfig
101
+ } ,
102
+ accessToken,
103
+ siteData
104
+ } )
105
+ return false
106
+ }
107
+
108
+ const words = `The ${ addonName } add-on has the following configurable options:`
109
+ console . log ( ` ${ chalk . yellowBright . bold ( words ) } ` )
110
+ render . configValues ( addonName , manifest . config )
111
+ console . log ( )
112
+ console . log ( ` ${ chalk . greenBright . bold ( 'Lets configure those!' ) } ` )
113
+
114
+ console . log ( )
115
+ console . log ( ` - Hit ${ chalk . white . bold ( 'enter' ) } to confirm value or set empty value` )
116
+ console . log ( ` - Hit ${ chalk . white . bold ( 'ctrl + C' ) } to cancel & exit configuration` )
117
+ console . log ( )
118
+
119
+ const prompts = generatePrompts ( {
120
+ config : manifest . config ,
121
+ configValues : rawFlags ,
122
+ } )
123
+
124
+ const userInput = await inquirer . prompt ( prompts )
125
+ // Merge user input with the flags specified
126
+ configValues = updateConfigValues ( manifest . config , rawFlags , userInput )
49
127
}
50
- const addonResponse = await createAddon ( settings , accessToken )
51
128
52
- if ( addonResponse . code === 404 ) {
53
- this . log ( `No addon "${ addonName } " found. Please double check your addon name and try again` )
54
- return false
55
- }
56
- this . log ( `Addon "${ addonName } " created for ${ siteData . name } ` )
129
+ await createSiteAddon ( {
130
+ addonName,
131
+ settings : {
132
+ siteId : siteId ,
133
+ addon : addonName ,
134
+ config : configValues
135
+ } ,
136
+ accessToken,
137
+ siteData
138
+ } )
57
139
}
58
140
}
59
141
60
- addonsCreateCommand . description = `Add an addon extension to your site
142
+ async function createSiteAddon ( {
143
+ addonName,
144
+ settings,
145
+ accessToken,
146
+ siteData
147
+ } ) {
148
+ const addonResponse = await createAddon ( settings , accessToken )
149
+
150
+ if ( addonResponse . code === 404 ) {
151
+ console . log ( `No add-on "${ addonName } " found. Please double check your add-on name and try again` )
152
+ return false
153
+ }
154
+ console . log ( `Add-on "${ addonName } " created for ${ siteData . name } ` )
155
+ if ( addonResponse . config && addonResponse . config . message ) {
156
+ console . log ( )
157
+ console . log ( `${ addonResponse . config . message } ` )
158
+ }
159
+ return addonResponse
160
+ }
161
+
162
+ addonsCreateCommand . description = `Add an add-on extension to your site
61
163
...
62
- Addons are a way to extend the functionality of your Netlify site
164
+ Add-ons are a way to extend the functionality of your Netlify site
63
165
`
64
166
65
- addonsCreateCommand . args = [
66
- {
67
- name : 'name' ,
68
- required : true ,
69
- description : 'addon namespace'
70
- }
71
- ]
167
+ addonsCreateCommand . args = [ {
168
+ name : 'name' ,
169
+ required : true ,
170
+ description : 'Add-on namespace'
171
+ } ]
72
172
73
173
// allow for any flags. Handy for variadic configuration options
74
174
addonsCreateCommand . strict = false
75
175
76
- addonsCreateCommand . hidden = true
77
-
78
176
module . exports = addonsCreateCommand
0 commit comments