File tree Expand file tree Collapse file tree 5 files changed +154
-0
lines changed Expand file tree Collapse file tree 5 files changed +154
-0
lines changed Original file line number Diff line number Diff line change
1
+ #! /usr/bin/env node
2
+
3
+ 'use strict'
4
+
5
+ const minimist = require ( 'minimist' )
6
+ const Fastify = require ( 'fastify' )
7
+ const path = require ( 'path' )
8
+ const fs = require ( 'fs' )
9
+ const help = fs . readFileSync ( './help.txt' , 'utf8' )
10
+
11
+ function start ( opts ) {
12
+ if ( opts . help ) {
13
+ console . log ( help )
14
+ process . exit ( 0 )
15
+ }
16
+
17
+ if ( opts . _ . length !== 1 ) {
18
+ console . log ( 'Missing the file parameter' )
19
+ process . exit ( 1 )
20
+ }
21
+
22
+ runFastify ( opts )
23
+ }
24
+
25
+ function stop ( code ) {
26
+ process . exit ( code )
27
+ }
28
+
29
+ function runFastify ( opts ) {
30
+ let file = null
31
+ try {
32
+ file = require ( path . resolve ( process . cwd ( ) , opts . _ [ 0 ] ) )
33
+ } catch ( e ) {
34
+ console . log ( 'Cannot find the specified file' )
35
+ process . exit ( 1 )
36
+ }
37
+
38
+ const fastify = Fastify ( {
39
+ logger : {
40
+ level : opts [ 'log-level' ]
41
+ }
42
+ } )
43
+
44
+ fastify . register ( file , function ( err ) {
45
+ if ( err ) {
46
+ console . log ( err )
47
+ process . exit ( 1 )
48
+ }
49
+ } )
50
+
51
+ fastify . listen ( opts . port , function ( err ) {
52
+ if ( err ) {
53
+ console . log ( err )
54
+ process . exit ( 1 )
55
+ }
56
+ console . log ( `Server listening on http://localhost:${ fastify . server . address ( ) . port } ` )
57
+ } )
58
+ }
59
+
60
+ if ( require . main === module ) {
61
+ start ( minimist ( process . argv . slice ( 2 ) , {
62
+ integer : [ 'port' ] ,
63
+ string : [ 'log-level' ] ,
64
+ alias : {
65
+ port : 'p' ,
66
+ help : 'h'
67
+ } ,
68
+ default : {
69
+ port : 3000 ,
70
+ 'log-level' : 'fatal'
71
+ }
72
+ } ) )
73
+ }
74
+
75
+ module . exports = { start, stop, runFastify }
Original file line number Diff line number Diff line change
1
+ Usage: fastify [opts] <file>
2
+ -p Port to listen on [3000]
3
+ --port
4
+
5
+ --log-level Log level [fatal]
6
+
7
+ -h Show this help message
8
+ --help
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " fastify-cli" ,
3
+ "version" : " 0.1.0" ,
4
+ "description" : " Run a fastify route with one command!" ,
5
+ "main" : " cli.js" ,
6
+ "bin" : {
7
+ "fastify" : " cli.js"
8
+ },
9
+ "scripts" : {
10
+ "test" : " standard && tap test.js"
11
+ },
12
+ "keywords" : [
13
+ " fastify" ,
14
+ " cli" ,
15
+ " one command"
16
+ ],
17
+ "author" : " Tomas Della Vedova - @delvedor (http://delved.org)" ,
18
+ "license" : " MIT" ,
19
+ "repository" : {
20
+ "type" : " git" ,
21
+ "url" : " git+https://github.com/delvedor/fastify-cli.git"
22
+ },
23
+ "bugs" : {
24
+ "url" : " https://github.com/delvedor/fastify-cli/issues"
25
+ },
26
+ "homepage" : " https://github.com/delvedor/fastify-cli#readme" ,
27
+ "engines" : {
28
+ "node" : " >= 4.0"
29
+ },
30
+ "dependencies" : {
31
+ "fastify" : " ^0.5.0" ,
32
+ "minimist" : " ^1.2.0"
33
+ },
34
+ "devDependencies" : {
35
+ "request" : " ^2.79.0" ,
36
+ "standard" : " ^8.6.0" ,
37
+ "tap" : " ^8.0.1"
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ module . exports = function ( fastify , options , next ) {
2
+ fastify . get ( '/' , function ( req , reply ) {
3
+ reply ( null , { hello : 'world' } )
4
+ } )
5
+ next ( )
6
+ }
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const t = require ( 'tap' )
4
+ const test = t . test
5
+ const request = require ( 'request' )
6
+ const cli = require ( './cli' )
7
+
8
+ test ( 'should start the server' , t => {
9
+ t . plan ( 4 )
10
+
11
+ cli . start ( {
12
+ port : 3000 ,
13
+ _ : [ './plugin.js' ]
14
+ } )
15
+
16
+ request ( {
17
+ method : 'GET' ,
18
+ uri : 'http://localhost:3000'
19
+ } , ( err , response , body ) => {
20
+ t . error ( err )
21
+ t . strictEqual ( response . statusCode , 200 )
22
+ t . strictEqual ( response . headers [ 'content-length' ] , '' + body . length )
23
+ t . deepEqual ( JSON . parse ( body ) , { hello : 'world' } )
24
+ cli . stop ( 0 )
25
+ } )
26
+ } )
You can’t perform that action at this time.
0 commit comments