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