1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | const path = require('path')
|
6 | const minimist = require('minimist')
|
7 | const findUp = require('find-up')
|
8 | const pkg = require('./../package')
|
9 |
|
10 | const alias = {
|
11 | i: 'ignore',
|
12 | p: 'polling',
|
13 | v: 'verbose',
|
14 | V: 'version'
|
15 | }
|
16 |
|
17 | const getRosid = function() {
|
18 |
|
19 | const routesPath = findUp.sync([ 'rosidfile.js', 'rosidfile.json' ])
|
20 |
|
21 | const Rosid = require('./../src/index')
|
22 | const routes = require(routesPath)
|
23 |
|
24 | return Rosid(routes)
|
25 |
|
26 | }
|
27 |
|
28 | const serve = function(srcPath, opts) {
|
29 |
|
30 | srcPath = path.resolve(srcPath || '')
|
31 |
|
32 | getRosid().serve(srcPath, {
|
33 | polling : opts.polling,
|
34 | verbose : opts.verbose
|
35 | })
|
36 |
|
37 | }
|
38 |
|
39 | const compile = function(srcPath, distPath, opts) {
|
40 |
|
41 | srcPath = path.resolve(srcPath || '')
|
42 | distPath = path.resolve(distPath || 'dist/')
|
43 |
|
44 | getRosid().compile(srcPath, distPath, {
|
45 | ignore : opts.ignore,
|
46 | verbose : opts.verbose
|
47 | })
|
48 |
|
49 | }
|
50 |
|
51 | const version = function() {
|
52 |
|
53 | console.log(pkg.version)
|
54 |
|
55 | }
|
56 |
|
57 | const help = function() {
|
58 |
|
59 | console.log(`
|
60 | Usage:
|
61 |
|
62 | rosid [command] [options]
|
63 |
|
64 | Commands:
|
65 |
|
66 | serve [srcPath] [options] serve current or specified folder
|
67 | compile [srcPath] [distPath] [options] compile current or specified folder to static files
|
68 |
|
69 | Options:
|
70 |
|
71 | -p, --polling use polling to watch files over a network or in other non-standard situations
|
72 | -v, --verbose increase verbosity
|
73 | -V, --version output the version number
|
74 | -h, --help output usage information
|
75 | `.replace(/\t/g, ' '))
|
76 |
|
77 | }
|
78 |
|
79 | const parse = function(argv) {
|
80 |
|
81 | // Output the version number
|
82 | if (argv.version===true) return version()
|
83 |
|
84 | // Output usage information
|
85 | if (argv.help===true) return help()
|
86 |
|
87 | // Ignore must be an array of strings
|
88 | argv.ignore = (typeof argv.ignore==='string' ? [ argv.ignore ] : argv.ignore)
|
89 |
|
90 | // Serve current or specified folder
|
91 | if (argv._[0]==='serve') return serve(argv._[1], {
|
92 | polling : argv.polling,
|
93 | verbose : argv.verbose
|
94 | })
|
95 |
|
96 | // Compile current or specified folder to static files
|
97 | if (argv._[0]==='compile') return compile(argv._[1], argv._[2], {
|
98 | ignore : argv.ignore,
|
99 | verbose : argv.verbose
|
100 | })
|
101 |
|
102 | // Show usage information when no known arg was found
|
103 | help()
|
104 |
|
105 | }
|
106 |
|
107 | parse(minimist(process.argv.slice(2), { alias })) |
\ | No newline at end of file |