5
5
*/
6
6
const { existsSync, readFileSync } = require ( 'fs' )
7
7
8
+ const { InternalCliError } = require ( '../../utils/error' )
9
+ const { NETLIFYDEVWARN } = require ( '../../utils/logo' )
10
+
8
11
let pkgJSON = null
9
12
let yarnExists = false
10
13
let warnedAboutEmptyScript = false
11
- const { NETLIFYDEVWARN } = require ( '../../utils/logo' )
12
14
13
15
/** hold package.json in a singleton so we dont do expensive parsing repeatedly */
14
16
const getPkgJSON = function ( ) {
@@ -28,9 +30,7 @@ const getYarnOrNPMCommand = function () {
28
30
29
31
/**
30
32
* real utiltiies are down here
31
- *
32
33
*/
33
-
34
34
const hasRequiredDeps = function ( requiredDepArray ) {
35
35
const { dependencies, devDependencies } = getPkgJSON ( )
36
36
for ( const depName of requiredDepArray ) {
@@ -53,9 +53,9 @@ const hasRequiredFiles = function (filenameArr) {
53
53
54
54
// preferredScriptsArr is in decreasing order of preference
55
55
const scanScripts = function ( { preferredScriptsArr, preferredCommand } ) {
56
- const { scripts } = getPkgJSON ( )
56
+ const packageJsonScripts = getPkgJSON ( ) . scripts
57
57
58
- if ( ! scripts && ! warnedAboutEmptyScript ) {
58
+ if ( ! packageJsonScripts && ! warnedAboutEmptyScript ) {
59
59
console . log ( `${ NETLIFYDEVWARN } You have a package.json without any npm scripts.` )
60
60
console . log (
61
61
`${ NETLIFYDEVWARN } Netlify Dev's detector system works best with a script, or you can specify a command to run in the netlify.toml [dev] block ` ,
@@ -65,26 +65,33 @@ const scanScripts = function ({ preferredScriptsArr, preferredCommand }) {
65
65
// not going to match any scripts anyway
66
66
return [ ]
67
67
}
68
- //
69
- //
70
- // NOTE: we return an array of arrays (args)
71
- // because we may want to supply extra args in some setups
72
- //
73
- // e.g. ['eleventy', '--serve', '--watch']
74
- //
75
- // array will in future be sorted by likelihood of what we want
76
- //
77
- //
78
- // this is very simplistic logic, we can offer far more intelligent logic later
79
- // eg make a dependency tree of npm scripts and offer the parentest node first
80
- return Object . entries ( scripts )
81
- . filter (
82
- ( [ scriptName , scriptCommand ] ) =>
83
- ( preferredScriptsArr . includes ( scriptName ) || scriptCommand . includes ( preferredCommand ) ) &&
84
- // prevent netlify dev calling netlify dev
85
- ! scriptCommand . includes ( 'netlify dev' ) ,
86
- )
87
- . map ( ( [ scriptName ] ) => [ scriptName ] )
68
+ /**
69
+ * NOTE: we return an array of arrays (args) because we may want to supply
70
+ * extra args in some setups, e.g.
71
+ *
72
+ * ['eleventy', '--serve', '--watch']
73
+ *
74
+ * array will be sorted by likelihood of what we want in the future. this is
75
+ * very simplistic logic, we can offer far more intelligent logic later, e.g.
76
+ * make a dependency tree of npm scripts and offer the parentest node first
77
+ */
78
+ const matchedScripts = [ ]
79
+ for ( const [ scriptName , scriptCommand ] of Object . entries ( packageJsonScripts ) ) {
80
+ /**
81
+ * Throw if trying to call Netlify dev from within Netlify dev. Include
82
+ * detailed information about the CLI setup in the error text.
83
+ */
84
+ if ( scriptCommand . includes ( 'netlify dev' ) ) {
85
+ throw new InternalCliError ( 'Cannot call `netlify dev` inside `netlify dev`.' , { packageJsonScripts } )
86
+ }
87
+ /**
88
+ * Otherwise, push the match.
89
+ */
90
+ if ( preferredScriptsArr . includes ( scriptName ) || scriptCommand . includes ( preferredCommand ) ) {
91
+ matchedScripts . push ( [ scriptName ] )
92
+ }
93
+ }
94
+ return matchedScripts
88
95
}
89
96
90
97
module . exports = {
0 commit comments