UNPKG

2.71 kBPlain TextView Raw
1#!/usr/bin/env node
2
3'use strict'
4
5const path = require('path')
6const mri = require('mri')
7const findUp = require('find-up')
8const pkg = require('./../package')
9
10const alias = {
11 i: 'ignore',
12 s: 'static',
13 o: 'open',
14 v: 'verbose',
15 V: 'version'
16}
17
18const getRosid = function() {
19
20 const routesPath = findUp.sync([ 'rosidfile.js', 'rosidfile.json' ])
21
22 if (routesPath==null) throw new Error('No rosidfile.js or rosidfile.json found')
23
24 const rosid = require('./../src/index')
25 const routes = require(routesPath)
26
27 return rosid(routes)
28
29}
30
31const serve = function(srcPath, opts) {
32
33 srcPath = path.resolve(srcPath || '')
34
35 getRosid().serve(srcPath, {
36 static: opts.static,
37 open: opts.open,
38 verbose: opts.verbose
39 })
40
41}
42
43const compile = function(srcPath, distPath, opts) {
44
45 srcPath = path.resolve(srcPath || '')
46 distPath = path.resolve(distPath || 'dist/')
47
48 getRosid().compile(srcPath, distPath, {
49 ignore: opts.ignore,
50 verbose: opts.verbose
51 })
52
53}
54
55const version = function() {
56
57 console.log(pkg.version)
58
59}
60
61const help = function() {
62
63 console.log(`
64 Usage:
65
66 rosid [command] [options]
67
68 Commands:
69
70 serve [srcPath] [options] serve current or specified folder
71 compile [srcPath] [distPath] [options] compile current or specified folder to static files
72
73 Options:
74
75 -i, --ignore ignore given files when copying
76 -s, --static disable browser reload for given files
77 -o, --open open default or given URL automatically in default browser
78 -v, --verbose increase verbosity
79 -V, --version output the version number
80 -h, --help output usage information
81
82 Examples:
83
84 $ rosid serve src/
85 $ rosid serve src/ -o
86 $ rosid serve src/ -o 'en/index.html'
87 $ rosid serve src/ -s 'static.html'
88 $ rosid compile src/ dist/
89 $ rosid compile src/ dist/ -i '**/_*' -i '**/includes'
90 `.replace(/\t/g, ' '))
91
92}
93
94const parse = function(argv) {
95
96 // Output the version number
97 if (argv.version===true) return version()
98
99 // Output usage information
100 if (argv.help===true) return help()
101
102 // Ignore and static must be arrays of strings
103 argv.ignore = (typeof argv.ignore==='string' ? [ argv.ignore ] : argv.ignore)
104 argv.static = (typeof argv.static==='string' ? [ argv.static ] : argv.static)
105
106 // Serve current or specified folder
107 if (argv._[0]==='serve') return serve(argv._[1], {
108 static: argv.static,
109 open: argv.open,
110 verbose: argv.verbose
111 })
112
113 // Compile current or specified folder to static files
114 if (argv._[0]==='compile') return compile(argv._[1], argv._[2], {
115 ignore: argv.ignore,
116 verbose: argv.verbose
117 })
118
119 // Show usage information when no known arg was found
120 help()
121
122}
123
124parse(mri(process.argv.slice(2), { alias }))
\No newline at end of file