UNPKG

3.79 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/*
4 * pub command script
5 * copyright 2015, Jurgen Leschner - github.com/jldec - MIT license
6 */
7
8var pkg = require('../package.json');
9console.log(pkg.name, 'v'+pkg.version);
10
11// https://github.com/tj/commander.js
12var cli = require('commander');
13
14cli.unknownOption = function(opt) {
15 process.stdout.write('\n unknown option: ' + opt + '\n' + cli.helpInformation());
16 process.exit(1);
17}
18
19// tweak usage not to include the first line with the incorrect command name
20cli.helpInformation = function() { return '\n' +
21 'usage: pub [opts] [dir]\n' +
22 'opts:\n' +
23 cli.optionHelp().replace(/^/gm, ' ') + '\n\n';
24}
25
26var u = require('pub-util');
27var inspect = require('util').inspect;
28
29cli
30 .option('-p, --port <port>', 'server port [3001]')
31 .option('-t, --theme <name>', 'theme module-name or dir, repeatable', collect, [])
32 .option('-o, --output-path <dir>', 'output dir [./out]')
33 .option('-O, --output-only', 'output html, scripts, static files and exit')
34 .option('-r, --root <prefix>', 'generate /prefix urls, "." means path relative')
35 .option('-s, --static <dir>', 'static dir, repeatable, supports <dir>,<route>', collectStaticPaths, [])
36 .option('-S, --static-only <dir>', 'serve only static files from <dir>', collectStaticPaths, [])
37 .option('-C, --config', 'show config and exit')
38 .option('-I, --ignore-config', 'ignore pub-config file')
39 .option('-P, --pages', 'show pages and templates and exit')
40 .option('-w, --watch-pkgs', 'also watch inside packages')
41 .option('-W, --no-watch', 'disable watcher entirely')
42 .option('-K, --no-sockets', 'no websockets')
43 .option('-E, --no-editor', 'website only, no editor or spa support')
44 .option('-m, --minify', 'minify scripts')
45 .option('-d, --dbg', 'enable scriptmaps and client-side debug traces')
46 .option('-D, --debug', 'node --debug (server and client-side)')
47 .option('-B, --debug-brk', 'node --debug-brk (server and client-side)');
48
49cli.parse(process.argv);
50
51var opts = {};
52
53opts.cli = true;
54opts.dir = cli.args[0] || '.';
55
56if (cli.port) { opts.port = cli.port; }
57if (cli.theme.length) { opts.pkgs = cli.theme; }
58if (cli.root === '.') { opts.relPaths = true; }
59else if (cli.root) { opts.staticRoot = cli.root; }
60if (cli.outputPath) { opts.outputs = cli.outputPath; }
61if (cli.outputOnly) { opts.outputOnly = true; }
62if (cli.static.length) { opts.staticPaths = cli.static; }
63if (cli.staticOnly.length) { opts.staticOnly = cli.staticOnly; }
64if (cli.mdFragments) { opts.fragmentDelim = 'md-headings'; }
65if (cli.ignoreConfig) { opts.ignoreConfig = true; }
66if (cli.pages) { opts.logPages = true; }
67if (cli.watch && cli.watchPkgs) { opts.watchPkgs = true; }
68if (!cli.watch || cli.outputOnly) { opts['no-watch'] = true; }
69if (!cli.sockets || cli.outputOnly) { opts['no-sockets'] = true; }
70if (cli.editor && !cli.staticOnly.length) { opts.editor = true; }
71if (cli.dbg) { opts.dbg = process.env.DEBUG || '*'; opts['no-timeouts'] = true; }
72if (cli.minify && !cli.dbg) { opts.minify = true; }
73
74var server = require('../server')(opts);
75
76if (cli.config) return console.log(inspect(u.omit(opts, 'source$', 'output$'), {depth:2, colors:true}));
77
78server.run();
79
80//--//--//--//--//--//--//--//--//
81
82function collect(val, arr) {
83 arr.push(val);
84 return arr;
85}
86
87function collectStaticPaths(val, arr) {
88 var pair = val.split(',');
89
90 if (pair.length > 1) {
91 arr.push( { path: pair[0], route: pair[1] } );
92 }
93 else {
94 arr.push( { path: pair[0] } );
95 }
96
97 return arr;
98}