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

refactor cmd to command, document, and make netlify.toml dev block overrides explicit #67

Merged
merged 1 commit into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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