#!/usr/bin/env node 'use strict' const path = require('path') const mri = require('mri') const findUp = require('find-up') const pkg = require('./../package') const alias = { i: 'ignore', s: 'static', o: 'open', v: 'verbose', V: 'version' } const getRosid = function() { const routesPath = findUp.sync([ 'rosidfile.js', 'rosidfile.json' ]) if (routesPath==null) throw new Error('No rosidfile.js or rosidfile.json found') const rosid = require('./../src/index') const routes = require(routesPath) return rosid(routes) } const serve = function(srcPath, opts) { srcPath = path.resolve(srcPath || '') getRosid().serve(srcPath, { static: opts.static, open: opts.open, verbose: opts.verbose }) } const compile = function(srcPath, distPath, opts) { srcPath = path.resolve(srcPath || '') distPath = path.resolve(distPath || 'dist/') getRosid().compile(srcPath, distPath, { ignore: opts.ignore, verbose: opts.verbose }) } const version = function() { console.log(pkg.version) } const help = function() { console.log(` Usage: rosid [command] [options] Commands: serve [srcPath] [options] serve current or specified folder compile [srcPath] [distPath] [options] compile current or specified folder to static files Options: -i, --ignore ignore given files when copying -s, --static disable browser reload for given files -o, --open open default or given URL automatically in default browser -v, --verbose increase verbosity -V, --version output the version number -h, --help output usage information Examples: $ rosid serve src/ $ rosid serve src/ -o $ rosid serve src/ -o 'en/index.html' $ rosid serve src/ -s 'static.html' $ rosid compile src/ dist/ $ rosid compile src/ dist/ -i '**/_*' -i '**/includes' `.replace(/\t/g, ' ')) } const parse = function(argv) { // Output the version number if (argv.version===true) return version() // Output usage information if (argv.help===true) return help() // Ignore and static must be arrays of strings argv.ignore = (typeof argv.ignore==='string' ? [ argv.ignore ] : argv.ignore) argv.static = (typeof argv.static==='string' ? [ argv.static ] : argv.static) // Serve current or specified folder if (argv._[0]==='serve') return serve(argv._[1], { static: argv.static, open: argv.open, verbose: argv.verbose }) // Compile current or specified folder to static files if (argv._[0]==='compile') return compile(argv._[1], argv._[2], { ignore: argv.ignore, verbose: argv.verbose }) // Show usage information when no known arg was found help() } parse(mri(process.argv.slice(2), { alias }))