Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 9648d86

Browse files
authored
Merge pull request #69 from netlify/upgrateGatsbyDetector
bring gatsby and RS detectors up to par with CRA
2 parents 5d8b368 + 5d27b8c commit 9648d86

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/detectors/gatsby.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
const { existsSync } = require('fs')
1+
const { existsSync, readFileSync } = require('fs')
22

33
module.exports = function() {
4-
if (!existsSync('gatsby-config.js')) {
4+
if (!existsSync('gatsby-config.js') || !existsSync('package.json')) {
55
return false
66
}
77

8+
const packageSettings = JSON.parse(readFileSync('package.json', { encoding: 'utf8' }))
9+
const { dependencies, scripts } = packageSettings
10+
if (!(dependencies && dependencies['gatsby'])) {
11+
return false
12+
}
13+
14+
const npmCommand = scripts && ((scripts.develop && 'develop') || (scripts.dev && 'dev'))
15+
if (!npmCommand) {
16+
// search all the scripts for something that starts with 'gatsby develop'
17+
Object.entries(scripts).forEach(([k, v]) => {
18+
if (v.startsWith('gatsby develop')) {
19+
npmCommand = k
20+
}
21+
})
22+
if (!npmCommand) {
23+
console.error("Couldn't determine the package.json script to run for this Gatsby project. Use the -c flag.")
24+
process.exit(1)
25+
} else {
26+
console.log('using npm script starting with gatsby develop: ', k)
27+
}
28+
}
29+
830
const yarnExists = existsSync('yarn.lock')
931
return {
1032
command: yarnExists ? 'yarn' : 'npm',
1133
port: 8888,
1234
proxyPort: 8000,
1335
env: { ...process.env },
14-
args: yarnExists ? ['run', 'develop'] : ['develop'],
36+
args: yarnExists || npmCommand != 'start' ? ['run', npmCommand] : [npmCommand],
1537
urlRegexp: new RegExp(`(http://)([^:]+:)${8000}(/)?`, 'g'),
1638
dist: 'public'
1739
}

src/detectors/react-static.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
1-
const { existsSync } = require('fs')
1+
const { existsSync, readFileSync } = require('fs')
22

33
module.exports = function() {
4-
if (!existsSync('static.config.js')) {
4+
if (!existsSync('static.config.js') || !existsSync('package.json')) {
55
return false
66
}
77

8+
const packageSettings = JSON.parse(readFileSync('package.json', { encoding: 'utf8' }))
9+
const { dependencies, scripts } = packageSettings
10+
if (!(dependencies && dependencies['react-static'])) {
11+
return false
12+
}
13+
14+
const npmCommand = scripts && ((scripts.start && 'start') || (scripts.develop && 'develop') || (scripts.dev && 'dev'))
15+
if (!npmCommand) {
16+
// search all the scripts for something that starts with 'react-static start'
17+
Object.entries(scripts).forEach(([k, v]) => {
18+
if (v.startsWith('react-static start')) {
19+
npmCommand = k
20+
}
21+
})
22+
if (!npmCommand) {
23+
console.error("Couldn't determine the package.json script to run for this React-Static project. Use the -c flag.")
24+
process.exit(1)
25+
} else {
26+
console.log('using npm script starting with react-static start: ', k)
27+
}
28+
}
29+
830
const yarnExists = existsSync('yarn.lock')
931
return {
1032
command: yarnExists ? 'yarn' : 'npm',
1133
port: 8888,
1234
proxyPort: 3000,
1335
env: { ...process.env },
14-
args: yarnExists ? ['run', 'start'] : ['start'],
36+
args: yarnExists || npmCommand != 'start' ? ['run', npmCommand] : [npmCommand],
1537
urlRegexp: new RegExp(`(http://)([^:]+:)${3000}(/)?`, 'g'),
1638
dist: 'dist'
1739
}

0 commit comments

Comments
 (0)