3
require('core-js/stable');
4
require('regenerator-runtime/runtime');
6
const path = require('path');
7
const isElectron = require('is-electron');
8
const program = require('commander');
9
const pkg = require('./package.json');
12
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
14
const increaseVerbosityLevel = (val, total) => {
18
const parseMountPoint = (val, acc) => {
26
if (val.indexOf(':') >= 0) {
27
const r = val.match(/(?:([^:]*)(?::(.*)))/);
35
mount.route = path.posix.join('/', mount.route || '').trim();
36
mount.target = (mount.target || '').trim();
43
const parseController = (val) => {
44
val = val ? (val + '').toLowerCase() : '';
46
if (['grbl', 'marlin', 'smoothie', 'tinyg', 'g2core'].includes(val)) {
53
const defaultHost = isElectron() ? '127.0.0.1' : '0.0.0.0';
54
const defaultPort = isElectron() ? 0 : 8000;
57
.version(pkg.version, '--version', 'output the current version')
59
.option('-p, --port <port>', `Set listen port (default: ${defaultPort})`, defaultPort)
60
.option('-H, --host <host>', `Set listen address or hostname (default: ${defaultHost})`, defaultHost)
61
.option('-b, --backlog <backlog>', 'Set listen backlog (default: 511)', 511)
62
.option('-c, --config <filename>', 'Set config file (default: ~/.cncrc)')
63
.option('-v, --verbose', 'Increase the verbosity level (-v, -vv, -vvv)', increaseVerbosityLevel, 0)
64
.option('-m, --mount <route-path>:<target>', 'Add a mount point for serving static files', parseMountPoint, [])
65
.option('-w, --watch-directory <path>', 'Watch a directory for changes')
66
.option('--access-token-lifetime <lifetime>', 'Access token lifetime in seconds or a time span string (default: 30d)')
67
.option('--allow-remote-access', 'Allow remote access to the server (default: false)')
68
.option('--controller <type>', 'Specify CNC controller: Grbl|Marlin|Smoothie|TinyG|g2core (default: \'\')', parseController, '');
70
program.on('--help', () => {
72
console.log(' Examples:');
74
console.log(' $ cncjs -vv');
75
console.log(' $ cncjs --mount /pendant:/home/pi/tinyweb');
76
console.log(' $ cncjs --mount /widget:~+/widget --mount /pendant:~/pendant');
77
console.log(' $ cncjs --mount /widget:https://cncjs.github.io/cncjs-widget-boilerplate/v1/');
78
console.log(' $ cncjs --watch-directory /home/pi/watch');
79
console.log(' $ cncjs --access-token-lifetime 60d # e.g. 3600, 30m, 12h, 30d');
80
console.log(' $ cncjs --allow-remote-access');
81
console.log(' $ cncjs --controller Grbl');
87
const normalizedArgv = ('' + process.argv[0]).indexOf(pkg.name) >= 0
88
? ['node', pkg.name, ...process.argv.slice(1)]
90
if (normalizedArgv.length > 1) {
91
program.parse(normalizedArgv);
94
const options = program.opts();
96
module.exports = () => new Promise((resolve, reject) => {
98
process.chdir(path.resolve(__dirname, 'server'));
100
require('./server').createServer({
103
backlog: options.backlog,
104
configFile: options.config,
105
verbosity: options.verbose,
106
mountPoints: options.mount,
107
watchDirectory: options.watchDirectory,
108
accessTokenLifetime: options.accessTokenLifetime,
109
allowRemoteAccess: !!options.allowRemoteAccess,
110
controller: options.controller