/
livestyle
executable file
·101 lines (95 loc) · 3.64 KB
/
livestyle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
/*jslint nomen:false */
/*global require, __dirname */
var express = require('express'),
util = require('util'),
path = require('path'),
mappings = {},
optimist = require('optimist'),
commandLineOptions = optimist
.usage('Usage: $0 [--proxy <remotehost> [-m <directoryMapping>]] [-d] [-h <hostname>] [-p <port>] [--root directory]')
.boolean('d')
.options('root', {
alias: 'r',
describe: 'The directory to serve static files from. Defaults to the current working directory unless --proxy is specified.'
})
.options('compiless', {
type: 'boolean',
describe: 'Whether to add the express-compiless middleware to the stack (compiles less files and serves it them as text/css).',
default: false
})
.options('processimage', {
alias: 'processimages',
type: 'boolean',
describe: 'Whether to add the express-processimage middleware to the stack.',
default: false
})
.options('debug', {
alias: 'd',
describe: 'Turn on debug mode (server logs events to stderr, client logs to console).'
})
.options('proxy', {
describe: 'The remote host:port to serve non-CSS files from'
})
.options('map', {
alias: 'm',
describe: 'Directory mappings of the form: remoteDir=localDir where localDir is relative to the root directory unless prefixed with /'
})
.options('host', {
alias: 'h',
default: '0.0.0.0',
describe: 'The local hostname or IP-address to listen on'
})
.options('port', {
alias: 'p',
default: 3000,
describe: 'The local post number to listen on'
})
.options('watchfile', {
describe: 'force using fs.watchFile instead of fs.watch (works better on some OSes)'
})
.options('mtime', {
describe: 'suppress change events unless the mtime of a file has changed (experimental, fs.watch only)'
})
.check(function (argv) {
if (argv.map) {
(typeof argv.map === 'string' ? [argv.map] : argv.map).forEach(function (urlEqualsDir) {
var matchUrlEqualsDir = urlEqualsDir.match(/^([^=]+)=([^=]+)$/);
if (matchUrlEqualsDir) {
mappings[matchUrlEqualsDir[1]] = matchUrlEqualsDir[2];
} else {
throw 'Invalid --map syntax: ' + urlEqualsDir;
}
});
}
})
.argv,
root;
if (commandLineOptions.root) {
root = path.resolve(process.cwd(), commandLineOptions.root);
} else if (!commandLineOptions.proxy) {
root = process.cwd();
}
if (commandLineOptions.help) {
optimist.showHelp();
process.exit();
}
if (commandLineOptions.proxy) {
console.log('Proxying to ' + commandLineOptions.proxy);
if (root) {
console.log('Serving static CSS files from ' + root);
}
} else if (root) {
console.log('Serving static files from ' + root);
}
console.log('Listening to http://' + commandLineOptions.host + ':' + commandLineOptions.port + '/');
require('../lib/createLiveStyleApp')({
debug: commandLineOptions.debug,
mtime: commandLineOptions.mtime,
watchfile: commandLineOptions.watchfile,
root: root,
compiless: commandLineOptions.compiless,
processImage: commandLineOptions.processimage,
mappings: mappings,
proxy: commandLineOptions.proxy || null
}).listen(commandLineOptions.port, commandLineOptions.host);