From dbd79419678dacc11f4671cc222de29d413511fc Mon Sep 17 00:00:00 2001 From: sw-yx Date: Tue, 2 Apr 2019 03:19:40 -0400 Subject: [PATCH 1/3] bring gatsby and RS detectors up to par with CRA --- src/detectors/gatsby.js | 28 +++++++++++++++++++++++++--- src/detectors/react-static.js | 28 +++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/detectors/gatsby.js b/src/detectors/gatsby.js index 23f39c0..144193f 100644 --- a/src/detectors/gatsby.js +++ b/src/detectors/gatsby.js @@ -1,17 +1,39 @@ -const { existsSync } = require('fs') +const { existsSync, readFileSync } = require('fs') module.exports = function() { - if (!existsSync('gatsby-config.js')) { + if (!existsSync('gatsby-config.js') || !existsSync('package.json')) { return false } + const packageSettings = JSON.parse(readFileSync('package.json', { encoding: 'utf8' })) + const { dependencies, scripts } = packageSettings + if (!(dependencies && dependencies['gatsby'])) { + return false + } + + const npmCommand = scripts && ((scripts.start && 'develop') || (scripts.serve && 'dev') || (scripts.run && 'start')) + if (!npmCommand) { + // search all the scripts for something that starts with 'gatsby develop' + Object.entries(scripts).forEach(([k, v]) => { + if (v.startsWith('gatsby develop')) { + npmCommand = k + } + }) + if (!npmCommand) { + console.error("Couldn't determine the package.json script to run for this Gatsby project. Use the -c flag.") + process.exit(1) + } else { + console.log('using npm script starting with gatsby develop: ', k) + } + } + const yarnExists = existsSync('yarn.lock') return { command: yarnExists ? 'yarn' : 'npm', port: 8888, proxyPort: 8000, env: { ...process.env }, - args: yarnExists ? ['run', 'develop'] : ['develop'], + args: yarnExists || npmCommand != 'start' ? ['run', npmCommand] : [npmCommand], urlRegexp: new RegExp(`(http://)([^:]+:)${8000}(/)?`, 'g'), dist: 'public' } diff --git a/src/detectors/react-static.js b/src/detectors/react-static.js index b0ce197..f40cc19 100644 --- a/src/detectors/react-static.js +++ b/src/detectors/react-static.js @@ -1,17 +1,39 @@ -const { existsSync } = require('fs') +const { existsSync, readFileSync } = require('fs') module.exports = function() { - if (!existsSync('static.config.js')) { + if (!existsSync('static.config.js') || !existsSync('package.json')) { return false } + const packageSettings = JSON.parse(readFileSync('package.json', { encoding: 'utf8' })) + const { dependencies, scripts } = packageSettings + if (!(dependencies && dependencies['react-static'])) { + return false + } + + const npmCommand = scripts && ((scripts.start && 'start') || (scripts.dev && 'dev')) + if (!npmCommand) { + // search all the scripts for something that starts with 'gatsby develop' + Object.entries(scripts).forEach(([k, v]) => { + if (v.startsWith('react-static start')) { + npmCommand = k + } + }) + if (!npmCommand) { + console.error("Couldn't determine the package.json script to run for this React-Static project. Use the -c flag.") + process.exit(1) + } else { + console.log('using npm script starting with react-static start: ', k) + } + } + const yarnExists = existsSync('yarn.lock') return { command: yarnExists ? 'yarn' : 'npm', port: 8888, proxyPort: 3000, env: { ...process.env }, - args: yarnExists ? ['run', 'start'] : ['start'], + args: yarnExists || npmCommand != 'start' ? ['run', npmCommand] : [npmCommand], urlRegexp: new RegExp(`(http://)([^:]+:)${3000}(/)?`, 'g'), dist: 'dist' } From 04eb7e1095cf70ec04c1d957a70d67ee1173248d Mon Sep 17 00:00:00 2001 From: sw-yx Date: Tue, 2 Apr 2019 03:29:00 -0400 Subject: [PATCH 2/3] fix bugs --- src/detectors/gatsby.js | 3 ++- src/detectors/react-static.js | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/detectors/gatsby.js b/src/detectors/gatsby.js index 144193f..f25e6d9 100644 --- a/src/detectors/gatsby.js +++ b/src/detectors/gatsby.js @@ -11,7 +11,7 @@ module.exports = function() { return false } - const npmCommand = scripts && ((scripts.start && 'develop') || (scripts.serve && 'dev') || (scripts.run && 'start')) + const npmCommand = scripts && ((scripts.develop && 'develop') || (scripts.dev && 'dev')) if (!npmCommand) { // search all the scripts for something that starts with 'gatsby develop' Object.entries(scripts).forEach(([k, v]) => { @@ -27,6 +27,7 @@ module.exports = function() { } } + console.log({ npmCommand, scripts }) const yarnExists = existsSync('yarn.lock') return { command: yarnExists ? 'yarn' : 'npm', diff --git a/src/detectors/react-static.js b/src/detectors/react-static.js index f40cc19..82cde3d 100644 --- a/src/detectors/react-static.js +++ b/src/detectors/react-static.js @@ -11,9 +11,9 @@ module.exports = function() { return false } - const npmCommand = scripts && ((scripts.start && 'start') || (scripts.dev && 'dev')) + const npmCommand = scripts && ((scripts.start && 'start') || (scripts.develop && 'develop') || (scripts.dev && 'dev')) if (!npmCommand) { - // search all the scripts for something that starts with 'gatsby develop' + // search all the scripts for something that starts with 'react-static start' Object.entries(scripts).forEach(([k, v]) => { if (v.startsWith('react-static start')) { npmCommand = k From 5d27b8c73803738917d2d1a71b6af41275c862df Mon Sep 17 00:00:00 2001 From: sw-yx Date: Tue, 2 Apr 2019 03:36:10 -0400 Subject: [PATCH 3/3] remove console.log after testing on kentcdodds.com --- src/detectors/gatsby.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/detectors/gatsby.js b/src/detectors/gatsby.js index f25e6d9..e3e6b07 100644 --- a/src/detectors/gatsby.js +++ b/src/detectors/gatsby.js @@ -27,7 +27,6 @@ module.exports = function() { } } - console.log({ npmCommand, scripts }) const yarnExists = existsSync('yarn.lock') return { command: yarnExists ? 'yarn' : 'npm',