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

Commit

Permalink
Clean up detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Benaiah committed Mar 14, 2019
1 parent 5c76812 commit 1a02b60
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 59 deletions.
40 changes: 14 additions & 26 deletions src/detectors/cra.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,27 @@ module.exports = function() {
return false
}

const package = JSON.parse(readFileSync('package.json', { encoding: 'utf8' }))
if (!(package.dependencies && package.dependencies['react-scripts'])) {
const packageSettings = JSON.parse(readFileSync('package.json', { encoding: 'utf8' }))
const { dependencies, scripts } = packageSettings
if (!(dependencies && dependencies['react-scripts'])) {
return false
}

const settings = {
const npmCommand = scripts && ((scripts.start && 'start') || (scripts.serve && 'serve') || (scripts.run && 'run'))

if (!npmCommand) {
console.error("Couldn't determine the script to run. Use the -c flag.")
process.exit(1)
}

const yarnExists = existsSync('yarn.lock')
return {
cmd: yarnExists ? 'yarn' : 'npm',
port: 8888,
proxyPort: 3000,
env: { ...process.env, BROWSER: 'none', PORT: 3000 },
args: [],
args: yarnExists || npmCommand != 'start' ? ['run', npmCommand] : [npmCommand],
urlRegexp: new RegExp(`(http://)([^:]+:)${3000}(/)?`, 'g'),
dist: 'dist'
}

if (package) {
if (existsSync('yarn.lock')) {
settings.cmd = 'yarn'
} else {
settings.cmd = 'npm'
settings.args.push('run')
}

if (package.scripts.start) {
settings.args.push('start')
} else if (package.scripts.serve) {
settings.args.push('serve')
} else if (package.scripts.run) {
settings.args.push('run')
} else {
console.error("Couldn't determine the script to run. Use the -c flag.")
process.exit(1)
}

return settings
}
}
4 changes: 1 addition & 3 deletions src/detectors/eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function() {
return false
}

const settings = {
return {
port: 8888,
proxyPort: 8080,
env: { ...process.env },
Expand All @@ -14,6 +14,4 @@ module.exports = function() {
urlRegexp: new RegExp(`(http://)([^:]+:)${8080}(/)?`, 'g'),
dist: '_site'
}

return settings
}
16 changes: 4 additions & 12 deletions src/detectors/gatsby.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ module.exports = function() {
return false
}

const settings = {
const yarnExists = existsSync('yarn.lock')
return {
cmd: yarnExists ? 'yarn' : 'npm',
port: 8888,
proxyPort: 8000,
env: { ...process.env },
args: [],
args: yarnExists ? ['run', 'dev'] : ['dev'],
urlRegexp: new RegExp(`(http://)([^:]+:)${8000}(/)?`, 'g'),
dist: 'public'
}

if (existsSync('yarn.lock')) {
settings.cmd = 'yarn'
} else {
settings.cmd = 'npm'
settings.args.push('run')
}
settings.args.push('dev')

return settings
}
4 changes: 1 addition & 3 deletions src/detectors/hugo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function() {
return false
}

const settings = {
return {
port: 8888,
proxyPort: 1313,
env: { ...process.env },
Expand All @@ -14,6 +14,4 @@ module.exports = function() {
urlRegexp: new RegExp(`(http://)([^:]+:)${1313}(/)?`, 'g'),
dist: 'public'
}

return settings
}
4 changes: 1 addition & 3 deletions src/detectors/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function() {
return false
}

const settings = {
return {
port: 8888,
proxyPort: 4000,
env: { ...process.env },
Expand All @@ -14,6 +14,4 @@ module.exports = function() {
urlRegexp: new RegExp(`(http://)([^:]+:)${4000}(/)?`, 'g'),
dist: '_site'
}

return settings
}
16 changes: 4 additions & 12 deletions src/detectors/react-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,14 @@ module.exports = function() {
return false
}

const settings = {
const yarnExists = existsSync('yarn.lock')
return {
cmd: yarnExists ? 'yarn' : 'npm',
port: 8888,
proxyPort: 3000,
env: { ...process.env },
args: [],
args: yarnExists ? ['run', 'start'] : ['start'],
urlRegexp: new RegExp(`(http://)([^:]+:)${3000}(/)?`, 'g'),
dist: 'dist'
}

if (existsSync('yarn.lock')) {
settings.cmd = 'yarn'
} else {
settings.cmd = 'npm'
settings.args.push('run')
}
settings.args.push('start')

return settings
}

0 comments on commit 1a02b60

Please sign in to comment.