11const { EOL } = require ( 'os' )
22const path = require ( 'path' )
33
4- const { listFrameworks } = require ( '@netlify/framework-info' )
54const chalk = require ( 'chalk' )
65const cleanDeep = require ( 'clean-deep' )
7- const { get } = require ( 'dot-prop' )
86const inquirer = require ( 'inquirer' )
9- const locatePath = require ( 'locate-path' )
107const isEmpty = require ( 'lodash/isEmpty' )
11- const nodeVersionAlias = require ( 'node-version-alias' )
128
13- const { readFileAsync } = require ( '../../lib/fs' )
149const { 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+
1615const 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-
7324const 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
158112const 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