Browse Source

middleware-list command is now --default-stack

master
Lloyd Brookes 6 years ago
parent
commit
5d5c902cba
  1. 4
      README.md
  2. 11
      bin/cli.js
  3. 3
      index.js
  4. 67
      lib/cli-app.js
  5. 11
      lib/command/middleware-list.js
  6. 44
      lib/command/serve.js
  7. 1308
      package-lock.json
  8. 3
      package.json
  9. 26
      test/cli.js

4
README.md

@ -160,7 +160,7 @@ See [the tutorials](https://github.com/lwsjs/local-web-server/wiki#tutorials) fo
```sh ```sh
$ npm install -g local-web-server $ npm install -g local-web-server
``` ```
* * * * * *
© 2013-19
Lloyd Brookes <75pound@gmail.com>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
&copy; 2013-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).

11
bin/cli.js

@ -1,8 +1,15 @@
#!/usr/bin/env node #!/usr/bin/env node
const nodeVersionMatches = require('node-version-matches') const nodeVersionMatches = require('node-version-matches')
if (nodeVersionMatches('>=8.0.0')) {
require('../lib/cli-app').run()
if (nodeVersionMatches('>=8')) {
const WsCli = require('../lib/cli-app')
const cli = new WsCli()
try {
cli.start()
} catch (err) {
console.error(require('util').inspect(err, { depth: 6, colors: true }))
process.exitCode = 1
}
} else { } else {
console.log('Sorry, this app requires node v8.0.0 or above. Please upgrade https://nodejs.org/en/') console.log('Sorry, this app requires node v8.0.0 or above. Please upgrade https://nodejs.org/en/')
} }

3
index.js

@ -15,6 +15,9 @@ const path = require('path')
* websocket: 'src/websocket-server.js' * websocket: 'src/websocket-server.js'
* }) * })
* // secure, SPA server with listening websocket now ready on port 8050 * // secure, SPA server with listening websocket now ready on port 8050
*
* // shut down the server
* server.close()
*/ */
/** /**

67
lib/cli-app.js

@ -1,13 +1,62 @@
const LwsCliApp = require('lws/lib/cli-app')
const LwsCli = require('lws/lib/cli-app')
const path = require('path')
class WsCliApp extends LwsCliApp {
constructor (options) {
super(options)
/* override default serve command */
this.commands.add(null, require('./command/serve'))
/* add middleware-list command */
this.commands.add('middleware-list', require('./command/middleware-list'))
class WsCli extends LwsCli {
execute (options, argv) {
const commandLineArgs = require('command-line-args')
const cliOptions = commandLineArgs(this.partialDefinitions(), { camelCase: true, partial: true })
if (cliOptions.defaultStack) {
const list = require('./default-stack')
this.log(list)
} else {
options = {
stack: require('./default-stack').slice(),
moduleDir: path.resolve(__dirname, `../node_modules`),
modulePrefix: 'lws-'
}
return super.execute(options, argv)
}
}
partialDefinitions () {
return super.partialDefinitions().concat([
{
name: 'default-stack',
type: Boolean,
description: 'Print the default middleware stack. Any of these built-in middlewares are available to use in a custom stack.',
section: 'core'
}
])
}
usage () {
const sections = super.usage()
sections.shift()
sections.shift()
sections.pop()
sections.unshift(
{
header: 'local-web-server',
content: 'The modular web server for productive full-stack development.'
},
{
header: 'Synopsis',
content: [
'$ ws <options>',
'$ ws {underline command} <options>'
]
}
)
sections.push({
content: 'Project home: {underline https://github.com/lwsjs/local-web-server}'
})
return sections
}
showVersion () {
const pkg = require(path.resolve(__dirname, '..', 'package.json'))
this.log(pkg.version)
} }
} }
module.exports = WsCliApp
module.exports = WsCli

11
lib/command/middleware-list.js

@ -1,11 +0,0 @@
class MiddlewareList {
description () {
return 'Print available middleware'
}
execute (options) {
const list = require('../default-stack')
console.log(list)
}
}
module.exports = MiddlewareList

44
lib/command/serve.js

@ -1,44 +0,0 @@
const ServeCommand = require('lws/lib/command/serve')
const path = require('path')
class WsServe extends ServeCommand {
execute (options, argv) {
options = {
stack: require('../default-stack'),
moduleDir: path.resolve(__dirname, `../../node_modules`),
modulePrefix: 'lws-'
}
return super.execute(options, argv)
}
usage () {
const sections = super.usage()
sections.shift()
sections.shift()
sections.pop()
sections.unshift(
{
header: 'local-web-server',
content: 'The modular web server for productive full-stack development.'
},
{
header: 'Synopsis',
content: [
'$ ws <options>',
'$ ws {underline command} <options>'
]
}
)
sections.push({
content: 'Project home: {underline https://github.com/lwsjs/local-web-server}'
})
return sections
}
showVersion () {
const pkg = require(path.resolve(__dirname, '..', '..', 'package.json'))
console.log(pkg.version)
}
}
module.exports = WsServe

1308
package-lock.json
File diff suppressed because it is too large
View File

3
package.json

@ -36,7 +36,7 @@
"index.js" "index.js"
], ],
"dependencies": { "dependencies": {
"lws": "^1.3.2",
"lws": "^2.0.0-3",
"lws-basic-auth": "^0.1.1", "lws-basic-auth": "^0.1.1",
"lws-blacklist": "^1.0.0", "lws-blacklist": "^1.0.0",
"lws-body-parser": "^0.2.4", "lws-body-parser": "^0.2.4",
@ -58,6 +58,7 @@
"coveralls": "^3.0.3", "coveralls": "^3.0.3",
"jsdoc-to-markdown": "^5.0.0", "jsdoc-to-markdown": "^5.0.0",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"nyc": "^14.1.1",
"test-runner": "^0.6.0" "test-runner": "^0.6.0"
} }
} }

26
test/cli.js

@ -1,6 +1,6 @@
const Tom = require('test-runner').Tom const Tom = require('test-runner').Tom
const a = require('assert') const a = require('assert')
const CliApp = require('../lib/cli-app')
const WsCli = require('../lib/cli-app')
const fetch = require('node-fetch') const fetch = require('node-fetch')
const tom = module.exports = new Tom('cli') const tom = module.exports = new Tom('cli')
@ -9,7 +9,8 @@ tom.test('cli.run', async function () {
const port = 7500 + this.index const port = 7500 + this.index
const origArgv = process.argv.slice() const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--port', `${port}` ] process.argv = [ 'node', 'something', '--port', `${port}` ]
const server = CliApp.run()
const cli = new WsCli({ logError: function () {} })
const server = cli.start()
process.argv = origArgv process.argv = origArgv
const response = await fetch(`http://127.0.0.1:${port}/`) const response = await fetch(`http://127.0.0.1:${port}/`)
server.close() server.close()
@ -20,7 +21,8 @@ tom.test('cli.run: bad option', async function () {
const origArgv = process.argv.slice() const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--should-fail' ] process.argv = [ 'node', 'something', '--should-fail' ]
const exitCode = process.exitCode const exitCode = process.exitCode
const server = CliApp.run()
const cli = new WsCli({ logError: function () {} })
const server = cli.start()
if (!exitCode) process.exitCode = 0 if (!exitCode) process.exitCode = 0
process.argv = origArgv process.argv = origArgv
a.strictEqual(server, undefined) a.strictEqual(server, undefined)
@ -29,20 +31,28 @@ tom.test('cli.run: bad option', async function () {
tom.test('cli.run: --help', async function () { tom.test('cli.run: --help', async function () {
const origArgv = process.argv.slice() const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--help' ] process.argv = [ 'node', 'something', '--help' ]
CliApp.run()
const cli = new WsCli({ log: function () {} })
cli.start()
process.argv = origArgv process.argv = origArgv
}) })
tom.test('cli.run: --version', async function () { tom.test('cli.run: --version', async function () {
const origArgv = process.argv.slice() const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', '--version' ] process.argv = [ 'node', 'something', '--version' ]
CliApp.run()
let logMsg = ''
const cli = new WsCli({ log: function (msg) { logMsg = msg } })
cli.start()
const pkg = require('../package.json')
a.strictEqual(logMsg.trim(), pkg.version)
process.argv = origArgv process.argv = origArgv
}) })
tom.test('cli.run: middleware-list', async function () {
tom.test('cli.run: default-stack', async function () {
const origArgv = process.argv.slice() const origArgv = process.argv.slice()
process.argv = [ 'node', 'something', 'middleware-list' ]
CliApp.run()
process.argv = [ 'node', 'something', '--default-stack' ]
let logMsg = ''
const cli = new WsCli({ log: function (msg) { logMsg = msg } })
cli.start()
a.ok(/lws-rewrite/.test(logMsg))
process.argv = origArgv process.argv = origArgv
}) })
Loading…
Cancel
Save