Skip to content

Commit 7c311ab

Browse files
committed
feat(command-init): recommend build plugins
1 parent fc20aec commit 7c311ab

File tree

3 files changed

+87
-20
lines changed

3 files changed

+87
-20
lines changed

src/utils/init/config-github.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ module.exports = async function configGithub({ context, siteId, repoOwner, repoN
184184

185185
const token = await getGitHubToken({ log, globalConfig })
186186

187-
const { buildCmd, buildDir, functionsDir } = await getBuildSettings({ siteRoot, config })
188-
await saveNetlifyToml({ siteRoot, config, buildCmd, buildDir, functionsDir, warn })
187+
const { buildCmd, buildDir, functionsDir, plugins } = await getBuildSettings({ siteRoot, config })
188+
await saveNetlifyToml({ siteRoot, config, buildCmd, buildDir, functionsDir, plugins, warn })
189189

190190
const octokit = getGitHubClient({ token })
191191
const [deployKey, githubRepo] = await Promise.all([
@@ -204,7 +204,12 @@ module.exports = async function configGithub({ context, siteId, repoOwner, repoN
204204
...(buildCmd && { cmd: buildCmd }),
205205
}
206206

207-
await updateSite({ siteId, api, failAndExit, options: { repo } })
207+
await updateSite({
208+
siteId,
209+
api,
210+
failAndExit,
211+
options: { repo, plugins: plugins.map((plugin) => ({ package: plugin })) },
212+
})
208213
// calling updateSite with { repo } resets the functions dir so we need to sync it
209214
const updatedSite = await updateSite({
210215
siteId,

src/utils/init/config-manual.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ module.exports = async function configManual({ context, siteId, repoData }) {
5757
site: { root: siteRoot },
5858
} = netlify
5959

60-
const { buildCmd, buildDir, functionsDir } = await getBuildSettings({ siteRoot, config })
61-
await saveNetlifyToml({ siteRoot, config, buildCmd, buildDir, functionsDir, warn })
60+
const { buildCmd, buildDir, functionsDir, plugins } = await getBuildSettings({ siteRoot, config })
61+
await saveNetlifyToml({ siteRoot, config, buildCmd, buildDir, functionsDir, plugins, warn })
6262

6363
const deployKey = await createDeployKey({ api, failAndExit })
6464
await addDeployKey({ log, exit, deployKey })
@@ -74,7 +74,12 @@ module.exports = async function configManual({ context, siteId, repoData }) {
7474
...(buildCmd && { cmd: buildCmd }),
7575
}
7676

77-
await updateSite({ siteId, api, failAndExit, options: { repo } })
77+
await updateSite({
78+
siteId,
79+
api,
80+
failAndExit,
81+
options: { repo, plugins: plugins.map((plugin) => ({ package: plugin })) },
82+
})
7883
// calling updateSite with { repo } resets the functions dir so we need to sync it
7984
const updatedSite = await updateSite({
8085
siteId,

src/utils/init/utils.js

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { EOL } = require('os')
12
const path = require('path')
23

34
const { 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+
3338
const 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

Comments
 (0)