Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
update param checking with check-types, add proxy and proxy-rewrite o…
Browse files Browse the repository at this point in the history
…ptions
  • Loading branch information
farism committed Sep 1, 2017
1 parent 9dc8290 commit 8398cfe
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 141 deletions.
5 changes: 5 additions & 0 deletions bin/cmds/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module.exports = {
yargs.options(dev.options).config(config)
},
handler: function(argv) {
// convert single proxy option into an array
if (typeof argv.proxy === 'string') {
argv.proxy = [argv.proxy]
}

require('../../src/tasks').dev(argv)
},
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"browser-sync": "^2.18.13",
"check-types": "^7.3.0",
"cssnano": "^3.10.0",
"del": "^3.0.0",
"elm-css": "^0.6.1",
Expand Down
9 changes: 4 additions & 5 deletions src/cmds/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ module.exports = {
description: 'elm-reactor port',
default: defaults.reactorPort,
},
l: {
alias: 'lr-port',
description: 'livereload port',
default: defaults.lrPort,
},
x: {
alias: 'proxy',
description: 'additional proxies',
default: defaults.proxy,
},
'proxy-rewrite': {
description: 'rewrite proxy paths',
default: defaults.proxyRewrite,
},
},
}
4 changes: 2 additions & 2 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const dev = {
port,
reactorHost: host,
reactorPort: port + 1,
lrPort: 35729,
proxy: undefined,
proxy: [],
proxyRewrite: true,
}

const init = {}
Expand Down
108 changes: 64 additions & 44 deletions src/tasks/dev.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const anyTemplate = require('gulp-any-template')
const browserSync = require('browser-sync')
const chalk = require('chalk')
const check = require('check-types')
const elmCss = require('elm-css')
const execa = require('execa')
const findAllDependencies = require('find-elm-dependencies').findAllDependencies
Expand All @@ -17,28 +18,44 @@ const addTask = require('./').addTask

const spacer = () => console.info(`${chalk.grey('-'.repeat(50))}`)

const param = (type, name, value) => {
const typeErr = `parameter \`${name}\` must be a \`${type}\``
const invalidParam = (type, name) =>
`parameter \`${name}\` expected \`${type}\``

if (typeof value === 'undefined' || value === null) {
throw new TypeError(`parameter \`${name}\` is required`)
}
const validateParam = (type, name, value, required = true) => {
const checker = required ? check.assert : check.assert.maybe

if (type === 'object') {
if (typeof value !== type || Array.isArray(value)) {
throw new TypeError(typeErr)
}
} else if (type === 'array') {
if (!Array.isArray(value)) {
throw new TypeError(typeErr)
return checker[type](value, invalidParam(type, name))
}

const parseProxies = (delimiter, proxies) => {
validateParam('array', 'proxies', proxies)
check.assert.array.of.string(proxies)

return proxies.reduce((acc, proxy) => {
if (proxy.includes(delimiter)) {
const arr = proxy.split(delimiter)
acc.push({ from: arr[0], target: arr[1] })
}
} else if (typeof value !== type) {
throw new TypeError(typeErr)
}

return acc
}, [])
}

const createProxies = (rewrite = true, proxies) => {
validateParam('array', 'proxies', proxies)
check.assert.array.of.object(proxies)

return proxies.map(({ from, target }) =>
proxy(from, {
target,
pathRewrite: (path, req) => (rewrite ? path.replace(from, '') : path),
logLevel: 'silent',
})
)
}

const getDepTree = entry => {
param('string', 'entry', entry)
validateParam('string', 'entry', entry)

return findAllDependencies(entry).then(deps => [entry, ...deps])
}
Expand All @@ -47,7 +64,7 @@ const defaultHtmlCompiler = () =>
Promise.resolve('incompatible html template...')

const loadHtmlCompiler = file => {
param('string', 'file', file)
validateParam('string', 'file', file)

return new Promise((resolve, reject) => {
fs.readFile(file, (err, contents) => {
Expand All @@ -67,7 +84,7 @@ const loadHtmlCompiler = file => {
}

const installPackages = (cwd = process.cwd()) => {
param('string', 'cwd', cwd)
validateParam('string', 'cwd', cwd)

return new Promise((resolve, reject) => {
execa('elm-package', ['install', '--yes'], { cwd, stdio: 'inherit' })
Expand All @@ -82,8 +99,8 @@ const startReactor = (
/* istanbul ignore next */
exitParent = true
) => {
param('string', 'host', host)
param('number', 'port', port)
validateParam('string', 'host', host)
validateParam('number', 'port', port)

return new Promise((resolve, reject) => {
const reactor = execa(
Expand Down Expand Up @@ -133,23 +150,22 @@ const startBrowserSync = (
reactor,
html,
dir,
proxies = [],
proxies,
proxyRewrite = true,
logLevel = 'silent'
) => {
param('string', 'host', host)
param('number', 'port', port)
param('string', 'reactor', reactor)
param('string', 'html', html)
param('string', 'dir', dir)
validateParam('string', 'host', host)
validateParam('number', 'port', port)
validateParam('string', 'reactor', reactor)
validateParam('string', 'html', html)
validateParam('string', 'dir', dir)
validateParam('array', 'proxies', proxies, false)
validateParam('boolean', 'proxyRewrite', proxyRewrite, false)

return new Promise((resolve, reject) => {
const customProxies = proxies.map(prxy => prxy.split('=')).map(prxy =>
proxy(prxy[0], {
target: prxy[1],
pathRewrite: (path, req) => path.replace(prxy[0], ''),
logLevel,
})
)
const customProxies = proxies
? createProxies(proxyRewrite, parseProxies('=', proxies))
: []

const config = {
files: [html, `${dir}/*.css`],
Expand Down Expand Up @@ -211,11 +227,11 @@ const startBrowserSync = (
}

const createWatcher = (onChange, onDeps, filter, bs, entry) => {
param('function', 'onChange', onChange)
param('function', 'onDeps', onDeps)
param('function', 'filter', filter)
param('object', 'bs', bs)
param('string', 'entry', entry)
validateParam('function', 'onChange', onChange)
validateParam('function', 'onDeps', onDeps)
validateParam('function', 'filter', filter)
validateParam('object', 'bs', bs)
validateParam('string', 'entry', entry)

return new Promise((resolve, reject) =>
getDepTree(entry)
Expand Down Expand Up @@ -244,10 +260,10 @@ const createWatcher = (onChange, onDeps, filter, bs, entry) => {
}

const watch = (bs, main, stylesheets, dir) => {
param('object', 'bs', bs)
param('string', 'main', main)
param('string', 'stylesheets', stylesheets)
param('string', 'dir', dir)
validateParam('object', 'bs', bs)
validateParam('string', 'main', main)
validateParam('string', 'stylesheets', stylesheets)
validateParam('string', 'dir', dir)

let stylesheetsDeps

Expand Down Expand Up @@ -314,7 +330,8 @@ const dev = options => {
`http://${opts.reactorHost}:${opts.reactorPort}`,
opts.html,
tmpDir.name,
opts.proxies
opts.proxy,
opts.proxyRewrite
)
})
.then(({ bs, port }) => {
Expand Down Expand Up @@ -346,7 +363,10 @@ const dev = options => {

module.exports = {
spacer,
param,
invalidParam,
validateParam,
parseProxies,
createProxies,
getDepTree,
defaultHtmlCompiler,
loadHtmlCompiler,
Expand Down
11 changes: 5 additions & 6 deletions test/cmds/dev-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ describe('$ elm-factory dev', () => {
description: 'elm-reactor port',
default: defaults.reactorPort,
},
l: {
alias: 'lr-port',
description: 'livereload port',
default: defaults.lrPort,
},
x: {
alias: 'proxy',
description: 'additional proxies',
default: defaults.proxy
default: defaults.proxy,
},
'proxy-rewrite': {
description: 'rewrite proxy paths',
default: defaults.proxyRewrite
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/defaults-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('defaults', () => {
port: 8000,
reactorHost: '127.0.0.1',
reactorPort: 8001,
lrPort: 35729,
proxy: undefined,
proxy: [],
proxyRewrite: true,
})
})

Expand Down
Loading

0 comments on commit 8398cfe

Please sign in to comment.