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

Commit

Permalink
Merge pull request #67 from netlify/makeDetectorsSafer
Browse files Browse the repository at this point in the history
refactor cmd to command, document, and make netlify.toml dev block overrides explicit
  • Loading branch information
biilmann authored Apr 2, 2019
2 parents fbf3926 + f510545 commit 9b364eb
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 29 deletions.
44 changes: 22 additions & 22 deletions src/detect-server.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
const gatsbyDetector = require('./detectors/gatsby')
const reactStaticDetector = require('./detectors/react-static')
const craDetector = require('./detectors/cra')
const hugoDetector = require('./detectors/hugo')
const eleventyDetector = require('./detectors/eleventy')
const jekyllDetector = require('./detectors/jekyll')
const vueDetector = require('./detectors/vue')

const detectors = [
gatsbyDetector,
reactStaticDetector,
hugoDetector,
jekyllDetector,
eleventyDetector,
craDetector,
vueDetector
]
const path = require('path')
const detectors = require('fs')
.readdirSync(path.join(__dirname, 'detectors'))
.filter(x => x.endsWith('.js')) // only accept .js detector files
.map(det => require(path.join(__dirname, `detectors/${det}`)))

module.exports.serverSettings = devConfig => {
let settings = null
Expand All @@ -27,16 +15,28 @@ module.exports.serverSettings = devConfig => {

if (devConfig) {
settings = settings || {}
if (devConfig.cmd) {
settings.cmd = devConfig.cmd.split(/\s/)[0]
settings.args = devConfig.cmd.split(/\s/).slice(1)
if (devConfig.command) {
settings.command = devConfig.command.split(/\s/)[0]
assignLoudly(devConfig, 'command', settings, 'command')
settings.args = devConfig.command.split(/\s/).slice(1)
assignLoudly(devConfig, 'args', settings, 'args')
}
if (devConfig.port) {
settings.proxyPort = devConfig.port
assignLoudly(devConfig, 'port', settings, 'proxyPort')
settings.urlRegexp = devConfig.urlRegexp || new RegExp(`(http://)([^:]+:)${devConfig.port}(/)?`, 'g')
}
settings.dist = devConfig.publish || settings.dist
assignLoudly(devConfig, 'publish', settings, 'dist')
}

return settings
}

// does assignAndTellUserIfNetlifyTomlDevBlockOverride
// mutates the settings field
function assignLoudly(devConfig, field, settings, settingsField) {
if (settings[settingsField] !== devConfig[field]) {
// silent if command is exactly same
console.log(`Using ${field} from netlify.toml [dev] block: `, devConfig[field])
settings[settingsField] === devConfig[field]
}
}
23 changes: 23 additions & 0 deletions src/detectors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## writing a detector

- write as many checks as possible to fit your project
- return false if its not your project
- if it definitely is, return an object with this shape:

```ts
{
cmd: String, // e.g. yarn, npm
port: Number, // e.g. 8888
proxyPort: Number, // e.g. 3000
env: Object, // env variables, see examples
args: String, // e.g 'run develop', so that the combined command is 'npm run develop'
urlRegexp: RegExp, // see examples
dist: String // e.g. 'dist' or 'build'
}
```

## things to note

- Dev block overrides will supercede anything you write in your detector: https://github.com/netlify/netlify-dev-plugin#project-detection
- detectors are language agnostic. don't assume npm or yarn.
- if default args (like 'develop') are missing, that means the user has configured it, best to tell them to use the -c flag.
2 changes: 1 addition & 1 deletion src/detectors/cra.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function() {

const yarnExists = existsSync('yarn.lock')
return {
cmd: yarnExists ? 'yarn' : 'npm',
command: yarnExists ? 'yarn' : 'npm',
port: 8888,
proxyPort: 3000,
env: { ...process.env, BROWSER: 'none', PORT: 3000 },
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function() {
port: 8888,
proxyPort: 8080,
env: { ...process.env },
cmd: 'npx',
command: 'npx',
args: ['eleventy', '--serve', '--watch'],
urlRegexp: new RegExp(`(http://)([^:]+:)${8080}(/)?`, 'g'),
dist: '_site'
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/gatsby.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function() {

const yarnExists = existsSync('yarn.lock')
return {
cmd: yarnExists ? 'yarn' : 'npm',
command: yarnExists ? 'yarn' : 'npm',
port: 8888,
proxyPort: 8000,
env: { ...process.env },
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/hugo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function() {
port: 8888,
proxyPort: 1313,
env: { ...process.env },
cmd: 'hugo',
command: 'hugo',
args: ['server', '-w'],
urlRegexp: new RegExp(`(http://)([^:]+:)${1313}(/)?`, 'g'),
dist: 'public'
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/jekyll.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function() {
port: 8888,
proxyPort: 4000,
env: { ...process.env },
cmd: 'bundle',
command: 'bundle',
args: ['exec', 'jekyll', 'serve', '-w', '-l'],
urlRegexp: new RegExp(`(http://)([^:]+:)${4000}(/)?`, 'g'),
dist: '_site'
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/react-static.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = function() {

const yarnExists = existsSync('yarn.lock')
return {
cmd: yarnExists ? 'yarn' : 'npm',
command: yarnExists ? 'yarn' : 'npm',
port: 8888,
proxyPort: 3000,
env: { ...process.env },
Expand Down
2 changes: 1 addition & 1 deletion src/detectors/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function() {

const yarnExists = existsSync('yarn.lock')
return {
cmd: yarnExists ? 'yarn' : 'npm',
command: yarnExists ? 'yarn' : 'npm',
port: 8888,
proxyPort: 8080,
env: { ...process.env },
Expand Down

0 comments on commit 9b364eb

Please sign in to comment.