Skip to content

Commit 238ba5f

Browse files
committed
feat: use plugins and framework names
1 parent b80e574 commit 238ba5f

File tree

6 files changed

+111
-75
lines changed

6 files changed

+111
-75
lines changed

package-lock.json

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@
7373
"dependencies": {
7474
"@netlify/build": "^9.0.0",
7575
"@netlify/config": "^4.0.0",
76-
"@netlify/framework-info": "^2.0.0",
76+
"@netlify/framework-info": "^3.0.0",
7777
"@netlify/plugin-edge-handlers": "^1.10.0",
78+
"@netlify/plugins-list": "^2.2.0",
7879
"@netlify/traffic-mesh-agent": "^0.27.10",
7980
"@netlify/zip-it-and-ship-it": "^2.0.0",
8081
"@oclif/command": "^1.6.1",

src/utils/init/frameworks.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { listFrameworks } = require('@netlify/framework-info')
2+
3+
const getFrameworkInfo = async ({ siteRoot, nodeVersion }) => {
4+
const frameworks = await listFrameworks({ projectDir: siteRoot, nodeVersion })
5+
if (frameworks.length !== 0) {
6+
const [
7+
{
8+
name,
9+
build: { directory, commands },
10+
plugins,
11+
},
12+
] = frameworks
13+
return {
14+
frameworkName: name,
15+
frameworkBuildCommand: commands[0],
16+
frameworkBuildDir: directory,
17+
frameworkPlugins: plugins,
18+
}
19+
}
20+
return {}
21+
}
22+
23+
module.exports = { getFrameworkInfo }

src/utils/init/node-version.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { get } = require('dot-prop')
2+
const locatePath = require('locate-path')
3+
const nodeVersionAlias = require('node-version-alias')
4+
5+
const { readFileAsync } = require('../../lib/fs')
6+
7+
const DEFAULT_NODE_VERSION = '12.18.0'
8+
const NVM_FLAG_PREFIX = '--'
9+
10+
// to support NODE_VERSION=--lts, etc.
11+
const normalizeConfiguredVersion = (version) =>
12+
version.startsWith(NVM_FLAG_PREFIX) ? version.slice(NVM_FLAG_PREFIX.length) : version
13+
14+
const detectNodeVersion = async ({ siteRoot, env, warn }) => {
15+
try {
16+
const nodeVersionFile = await locatePath(['.nvmrc', '.node-version'], { cwd: siteRoot })
17+
const configuredVersion =
18+
nodeVersionFile === undefined ? get(env, 'NODE_VERSION.value') : await readFileAsync(nodeVersionFile, 'utf8')
19+
20+
const version =
21+
configuredVersion === undefined
22+
? DEFAULT_NODE_VERSION
23+
: await nodeVersionAlias(normalizeConfiguredVersion(configuredVersion))
24+
25+
return version
26+
} catch (error) {
27+
warn(`Failed detecting Node.js version: ${error.message}`)
28+
return DEFAULT_NODE_VERSION
29+
}
30+
}
31+
32+
module.exports = { detectNodeVersion }

src/utils/init/plugins.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const pluginsList = require('@netlify/plugins-list')
2+
const fetch = require('node-fetch')
3+
4+
const PLUGINS_LIST_URL = 'https://netlify-plugins.netlify.app/plugins.json'
5+
// 1 minute
6+
const PLUGINS_LIST_TIMEOUT = 6e4
7+
8+
const getPluginsList = async () => {
9+
try {
10+
const response = await fetch(PLUGINS_LIST_URL, { timeout: PLUGINS_LIST_TIMEOUT })
11+
return await response.json()
12+
} catch {
13+
return pluginsList
14+
}
15+
}
16+
17+
const getPluginInfo = (list, packageName) => list.find(({ package }) => package === packageName)
18+
19+
const isPluginInstalled = (configPlugins, plugin) =>
20+
configPlugins.some(({ package: configPlugin }) => configPlugin === plugin)
21+
22+
const getRecommendPlugins = (frameworkPlugins, config) =>
23+
frameworkPlugins.filter((plugin) => !isPluginInstalled(config.plugins, plugin))
24+
25+
module.exports = { getPluginsList, getPluginInfo, getRecommendPlugins }

src/utils/init/utils.js

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
const { EOL } = require('os')
22
const path = require('path')
33

4-
const { listFrameworks } = require('@netlify/framework-info')
54
const chalk = require('chalk')
65
const cleanDeep = require('clean-deep')
7-
const { get } = require('dot-prop')
86
const inquirer = require('inquirer')
9-
const locatePath = require('locate-path')
107
const isEmpty = require('lodash/isEmpty')
11-
const nodeVersionAlias = require('node-version-alias')
128

13-
const { readFileAsync } = require('../../lib/fs')
149
const { fileExistsAsync, writeFileAsync } = require('../../lib/fs')
1510

11+
const { getFrameworkInfo } = require('./frameworks')
12+
const { detectNodeVersion } = require('./node-version')
13+
const { getPluginsList, getPluginInfo, getRecommendPlugins } = require('./plugins')
14+
1615
const normalizeDir = ({ siteRoot, dir, defaultValue }) => {
1716
if (dir === undefined) {
1817
return defaultValue
@@ -22,56 +21,8 @@ const normalizeDir = ({ siteRoot, dir, defaultValue }) => {
2221
return relativeDir || defaultValue
2322
}
2423

25-
const DEFAULT_NODE_VERSION = '12.18.0'
26-
const NVM_FLAG_PREFIX = '--'
27-
28-
// to support NODE_VERSION=--lts, etc.
29-
const normalizeConfiguredVersion = (version) =>
30-
version.startsWith(NVM_FLAG_PREFIX) ? version.slice(NVM_FLAG_PREFIX.length) : version
31-
32-
const detectNodeVersion = async ({ siteRoot, env, warn }) => {
33-
try {
34-
const nodeVersionFile = await locatePath(['.nvmrc', '.node-version'], { cwd: siteRoot })
35-
const configuredVersion =
36-
nodeVersionFile === undefined ? get(env, 'NODE_VERSION.value') : await readFileAsync(nodeVersionFile, 'utf8')
37-
38-
const version =
39-
configuredVersion === undefined
40-
? DEFAULT_NODE_VERSION
41-
: await nodeVersionAlias(normalizeConfiguredVersion(configuredVersion))
42-
43-
return version
44-
} catch (error) {
45-
warn(`Failed detecting Node.js version: ${error.message}`)
46-
return DEFAULT_NODE_VERSION
47-
}
48-
}
49-
50-
const getFrameworkInfo = async ({ siteRoot, nodeVersion }) => {
51-
const frameworks = await listFrameworks({ projectDir: siteRoot, nodeVersion })
52-
if (frameworks.length !== 0) {
53-
const [
54-
{
55-
title,
56-
build: { directory, commands },
57-
plugins,
58-
},
59-
] = frameworks
60-
return {
61-
frameworkTitle: title,
62-
frameworkBuildCommand: commands[0],
63-
frameworkBuildDir: directory,
64-
frameworkPlugins: plugins,
65-
}
66-
}
67-
return {}
68-
}
69-
70-
const isPluginInstalled = (configPlugins, plugin) =>
71-
configPlugins.some(({ package: configPlugin }) => configPlugin === plugin)
72-
7324
const getDefaultSettings = ({ siteRoot, config, frameworkPlugins, frameworkBuildCommand, frameworkBuildDir }) => {
74-
const recommendedPlugins = frameworkPlugins.filter((plugin) => !isPluginInstalled(config.plugins, plugin))
25+
const recommendedPlugins = getRecommendPlugins(frameworkPlugins, config)
7526
const {
7627
command: defaultBuildCmd = frameworkBuildCommand,
7728
publish: defaultBuildDir = frameworkBuildDir,
@@ -82,16 +33,16 @@ const getDefaultSettings = ({ siteRoot, config, frameworkPlugins, frameworkBuild
8233
defaultBuildCmd,
8334
defaultBuildDir: normalizeDir({ siteRoot, dir: defaultBuildDir, defaultValue: '.' }),
8435
defaultFunctionsDir: normalizeDir({ siteRoot, dir: defaultFunctionsDir, defaultValue: 'functions' }),
85-
recommendedPlugins,
36+
recommendedPlugins: [...recommendedPlugins, '@netlify/plugin-lighthouse'],
8637
}
8738
}
8839

89-
const getPromptInputs = ({
40+
const getPromptInputs = async ({
9041
defaultBuildCmd,
9142
defaultBuildDir,
9243
defaultFunctionsDir,
9344
recommendedPlugins,
94-
frameworkTitle,
45+
frameworkName,
9546
}) => {
9647
const inputs = [
9748
{
@@ -119,30 +70,33 @@ const getPromptInputs = ({
11970
return inputs
12071
}
12172

122-
const prefix = `Seems like this is a ${formatTitle(frameworkTitle)} site.${EOL} `
73+
const pluginsList = await getPluginsList()
74+
75+
const prefix = `Seems like this is a ${formatTitle(frameworkName)} site.${EOL} `
12376
if (recommendedPlugins.length === 1) {
77+
const { name } = getPluginInfo(pluginsList, recommendedPlugins[0])
12478
return [
12579
...inputs,
12680
{
12781
type: 'confirm',
12882
name: 'installSinglePlugin',
129-
message: `${prefix}Recommended Build Plugin: ${formatTitle(recommendedPlugins[0])}${EOL} Install ${
130-
recommendedPlugins[0]
131-
}?`,
83+
message: `${prefix}Recommended Build Plugin: ${formatTitle(`${name} plugin`)}${EOL} Install ${name} plugin?`,
13284
default: true,
13385
},
13486
]
13587
}
13688

89+
const infos = recommendedPlugins.map((packageName) => getPluginInfo(pluginsList, packageName))
13790
return [
13891
...inputs,
13992
{
14093
type: 'checkbox',
14194
name: 'plugins',
142-
message: `${prefix}Recommended Build Plugins: ${recommendedPlugins
95+
message: `${prefix}Recommended Build Plugins: ${infos
96+
.map(({ name }) => `${name} plugin`)
14397
.map(formatTitle)
14498
.join(', ')}${EOL} Which plugins to install?`,
145-
choices: recommendedPlugins,
99+
choices: infos.map(({ name, package }) => ({ name: `${name} plugin`, value: package })),
146100
},
147101
]
148102
}
@@ -157,7 +111,7 @@ const getPluginsToInstall = ({ plugins, installSinglePlugin, recommendedPlugins
157111

158112
const getBuildSettings = async ({ siteRoot, config, env, warn }) => {
159113
const nodeVersion = await detectNodeVersion({ siteRoot, env, warn })
160-
const { frameworkTitle, frameworkBuildCommand, frameworkBuildDir, frameworkPlugins } = await getFrameworkInfo({
114+
const { frameworkName, frameworkBuildCommand, frameworkBuildDir, frameworkPlugins } = await getFrameworkInfo({
161115
siteRoot,
162116
nodeVersion,
163117
})
@@ -169,12 +123,12 @@ const getBuildSettings = async ({ siteRoot, config, env, warn }) => {
169123
frameworkPlugins,
170124
})
171125
const { buildCmd, buildDir, functionsDir, plugins, installSinglePlugin } = await inquirer.prompt(
172-
getPromptInputs({
126+
await getPromptInputs({
173127
defaultBuildCmd,
174128
defaultBuildDir,
175129
defaultFunctionsDir,
176130
recommendedPlugins,
177-
frameworkTitle,
131+
frameworkName,
178132
}),
179133
)
180134
const pluginsToInstall = getPluginsToInstall({ plugins, installSinglePlugin, recommendedPlugins })

0 commit comments

Comments
 (0)