UNPKG

3.93 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/*
4 * pub command script
5 * copyright 2015-2020, Jürgen 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('-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
50cli.parse(process.argv);
51
52var opts = {};
53
54opts.cli = true;
55opts.dir = cli.args[0] || '.';
56
57if (cli.open) { opts.openBrowser = true; }
58if (cli.port) { opts.port = cli.port; }
59if (cli.theme.length) { opts.pkgs = cli.theme; }
60if (cli.root === '.') { opts.relPaths = true; }
61else if (cli.root) { opts.staticRoot = cli.root; }
62if (cli.outputPath) { opts.outputs = cli.outputPath; }
63if (cli.outputOnly) { opts.outputOnly = true; }
64if (cli.static.length) { opts.staticPaths = cli.static; }
65if (cli.staticOnly.length) { opts.staticOnly = cli.staticOnly; }
66if (cli.mdFragments) { opts.fragmentDelim = 'md-headings'; }
67if (cli.ignoreConfig) { opts.ignoreConfig = true; }
68if (cli.pages) { opts.logPages = true; }
69if (cli.watch && cli.watchPkgs) { opts.watchPkgs = true; }
70if (!cli.watch || cli.outputOnly) { opts['no-watch'] = true; }
71if (!cli.sockets || cli.outputOnly) { opts['no-sockets'] = true; }
72if (cli.editor && !cli.staticOnly.length) { opts.editor = true; }
73if (cli.dbg) { opts.dbg = process.env.DEBUG || '*'; opts['no-timeouts'] = true; }
74if (cli.minify && !cli.dbg) { opts.minify = true; }
75
76var server = require('../server')(opts);
77
78if (cli.config) return console.log(inspect(u.omit(opts, 'source$', 'output$'), {depth:2, colors:true}));
79
80server.run();
81
82//--//--//--//--//--//--//--//--//
83
84function collect(val, arr) {
85 arr.push(val);
86 return arr;
87}
88
89function 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}