1+ const { EOL } = require ( 'os' )
12const path = require ( 'path' )
23
34const { listFrameworks } = require ( '@netlify/framework-info' )
@@ -23,15 +24,20 @@ const getFrameworkDefaults = async ({ siteRoot }) => {
2324 const [
2425 {
2526 build : { directory, commands } ,
27+ plugins,
2628 } ,
2729 ] = frameworks
28- return { frameworkBuildCommand : commands [ 0 ] , frameworkBuildDir : directory }
30+ return { frameworkBuildCommand : commands [ 0 ] , frameworkBuildDir : directory , frameworkPlugins : plugins }
2931 }
3032 return { }
3133}
3234
35+ const isPluginInstalled = ( configPlugins , plugin ) =>
36+ configPlugins . some ( ( { package : configPlugin } ) => configPlugin === plugin )
37+
3338const getDefaultSettings = async ( { siteRoot, config } ) => {
34- const { frameworkBuildCommand, frameworkBuildDir } = await getFrameworkDefaults ( { siteRoot } )
39+ const { frameworkBuildCommand, frameworkBuildDir, frameworkPlugins } = await getFrameworkDefaults ( { siteRoot } )
40+ const recommendedPlugins = frameworkPlugins . filter ( ( plugin ) => ! isPluginInstalled ( config . plugins , plugin ) )
3541 const {
3642 command : defaultBuildCmd = frameworkBuildCommand ,
3743 publish : defaultBuildDir = frameworkBuildDir ,
@@ -42,12 +48,12 @@ const getDefaultSettings = async ({ siteRoot, config }) => {
4248 defaultBuildCmd,
4349 defaultBuildDir : normalizeDir ( { siteRoot, dir : defaultBuildDir , defaultValue : '.' } ) ,
4450 defaultFunctionsDir : normalizeDir ( { siteRoot, dir : defaultFunctionsDir , defaultValue : 'functions' } ) ,
51+ recommendedPlugins,
4552 }
4653}
4754
48- const getBuildSettings = async ( { siteRoot, config } ) => {
49- const { defaultBuildCmd, defaultBuildDir, defaultFunctionsDir } = await getDefaultSettings ( { siteRoot, config } )
50- const { buildCmd, buildDir, functionsDir } = await inquirer . prompt ( [
55+ const getPromptInputs = ( { defaultBuildCmd, defaultBuildDir, defaultFunctionsDir, recommendedPlugins } ) => {
56+ const inputs = [
5157 {
5258 type : 'input' ,
5359 name : 'buildCmd' ,
@@ -67,16 +73,58 @@ const getBuildSettings = async ({ siteRoot, config }) => {
6773 message : 'Netlify functions folder:' ,
6874 default : defaultFunctionsDir ,
6975 } ,
70- ] )
76+ ]
7177
72- return { buildCmd, buildDir, functionsDir }
78+ if ( recommendedPlugins . length === 0 ) {
79+ return inputs
80+ }
81+
82+ if ( recommendedPlugins . length === 1 ) {
83+ return [
84+ ...inputs ,
85+ {
86+ type : 'confirm' ,
87+ name : 'installSinglePlugin' ,
88+ message : `Install ${ recommendedPlugins [ 0 ] } ?` ,
89+ default : true ,
90+ } ,
91+ ]
92+ }
93+
94+ return [
95+ ...inputs ,
96+ {
97+ type : 'checkbox' ,
98+ name : 'plugins' ,
99+ message : 'Which build plugins to install:' ,
100+ choices : recommendedPlugins ,
101+ } ,
102+ ]
73103}
74104
75- const getNetlifyToml = ( {
76- command = '# no build command' ,
77- publish = '.' ,
78- functions = 'functions' ,
79- } ) => `# example netlify.toml
105+ const getPluginsToInstall = ( { plugins, installSinglePlugin, recommendedPlugins } ) => {
106+ if ( Array . isArray ( plugins ) ) {
107+ return plugins
108+ }
109+
110+ return installSinglePlugin === true ? [ recommendedPlugins [ 0 ] ] : [ ]
111+ }
112+
113+ const getBuildSettings = async ( { siteRoot, config } ) => {
114+ const { defaultBuildCmd, defaultBuildDir, defaultFunctionsDir, recommendedPlugins } = await getDefaultSettings ( {
115+ siteRoot,
116+ config,
117+ } )
118+ const { buildCmd, buildDir, functionsDir, plugins, installSinglePlugin } = await inquirer . prompt (
119+ getPromptInputs ( { defaultBuildCmd, defaultBuildDir, defaultFunctionsDir, recommendedPlugins } ) ,
120+ )
121+
122+ const pluginsToInstall = getPluginsToInstall ( { plugins, installSinglePlugin, recommendedPlugins } )
123+ return { buildCmd, buildDir, functionsDir, plugins : pluginsToInstall }
124+ }
125+
126+ const getNetlifyToml = ( { command = '# no build command' , publish = '.' , functions = 'functions' , plugins = [ ] } ) => {
127+ const content = `# example netlify.toml
80128[build]
81129 command = "${ command } "
82130 functions = "${ functions } "
@@ -98,8 +146,14 @@ const getNetlifyToml = ({
98146
99147 ## more info on configuring this file: https://www.netlify.com/docs/netlify-toml-reference/
100148`
149+ if ( plugins . length === 0 ) {
150+ return content
151+ }
152+
153+ return `${ content } ${ EOL } ${ plugins . map ( ( plugin ) => `[[plugins]]${ EOL } package = "${ plugin } "` ) . join ( EOL ) } `
154+ }
101155
102- const saveNetlifyToml = async ( { siteRoot, config, buildCmd, buildDir, functionsDir, warn } ) => {
156+ const saveNetlifyToml = async ( { siteRoot, config, buildCmd, buildDir, functionsDir, plugins , warn } ) => {
103157 const tomlPath = path . join ( siteRoot , 'netlify.toml' )
104158 const exists = await fileExistsAsync ( tomlPath )
105159 const cleanedConfig = cleanDeep ( config )
@@ -117,7 +171,10 @@ const saveNetlifyToml = async ({ siteRoot, config, buildCmd, buildDir, functions
117171 ] )
118172 if ( makeNetlifyTOML ) {
119173 try {
120- await writeFileAsync ( tomlPath , getNetlifyToml ( { command : buildCmd , publish : buildDir , functions : functionsDir } ) )
174+ await writeFileAsync (
175+ tomlPath ,
176+ getNetlifyToml ( { command : buildCmd , publish : buildDir , functions : functionsDir , plugins } ) ,
177+ )
121178 } catch ( error ) {
122179 warn ( `Failed saving Netlify toml file: ${ error . message } ` )
123180 }
0 commit comments