This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdev.js
68 lines (55 loc) · 2.38 KB
/
dev.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Retrieve framework's dev commands.
// We use, in priority order:
// - `package.json` `scripts` containing `framework.dev.command`
// - `package.json` `scripts` whose names are among `NPM_DEV_SCRIPTS`
// - `framework.dev.command`
export const getDevCommands = function ({ frameworkDevCommand, scripts, runScriptCommand }) {
if (frameworkDevCommand === undefined) {
return []
}
const scriptDevCommands = getScriptDevCommands(scripts, frameworkDevCommand).map(
(scriptName) => `${runScriptCommand} ${scriptName}`,
)
if (scriptDevCommands.length !== 0) {
return scriptDevCommands
}
return [frameworkDevCommand]
}
const getScriptDevCommands = function (scripts, frameworkDevCommand) {
const preferredScripts = getPreferredScripts(scripts, frameworkDevCommand)
if (preferredScripts.length !== 0) {
return preferredScripts
}
const devScripts = Object.keys(scripts).filter((script) => isNpmDevScript(script, scripts[script]))
return devScripts.sort(scriptsSorter)
}
const getSortIndex = (index) => (index === -1 ? Number.MAX_SAFE_INTEGER : index)
const scriptsSorter = (script1, script2) => {
const index1 = NPM_DEV_SCRIPTS.findIndex((devScriptName) => matchesNpmWDevScript(script1, devScriptName))
const index2 = NPM_DEV_SCRIPTS.findIndex((devScriptName) => matchesNpmWDevScript(script2, devScriptName))
return getSortIndex(index1) - getSortIndex(index2)
}
const getPreferredScripts = function (scripts, frameworkDevCommand) {
return Object.entries(scripts)
.filter(([, scriptValue]) => scriptValue.includes(frameworkDevCommand))
.map((script) => getEntryKey(script))
.sort(scriptsSorter)
}
const getEntryKey = function ([key]) {
return key
}
// Check if the npm script is likely to contain a dev command
const isNpmDevScript = function (scriptName, scriptValue) {
return NPM_DEV_SCRIPTS.some(
(devScriptName) => matchesNpmWDevScript(scriptName, devScriptName) && !isExcludedScript(scriptValue),
)
}
// We also match script names like `docs:dev`
const matchesNpmWDevScript = function (scriptName, devScriptName) {
return scriptName === devScriptName || scriptName.endsWith(`:${devScriptName}`)
}
const NPM_DEV_SCRIPTS = ['dev', 'serve', 'develop', 'start', 'run', 'build', 'web']
const isExcludedScript = function (scriptValue) {
return EXCLUDED_SCRIPTS.some((excluded) => scriptValue.includes(excluded))
}
const EXCLUDED_SCRIPTS = ['netlify dev']