1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | const path = require('path')
|
5 |
|
6 |
|
7 | const updateNotifier = require('update-notifier')
|
8 | const nodeVersion = require('node-version')
|
9 | const args = require('args')
|
10 | const isAsyncSupported = require('is-async-supported')
|
11 |
|
12 |
|
13 | const pkg = require('../package')
|
14 |
|
15 |
|
16 | if (nodeVersion.major < 6) {
|
17 | console.error(`Error! Micro requires at least version 6 of Node. Please upgrade!`)
|
18 | process.exit(1)
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 | if (!process.env.NOW && pkg.dist) {
|
24 | updateNotifier({pkg}).notify()
|
25 | }
|
26 |
|
27 | args
|
28 | .option('port', 'Port to listen on', process.env.PORT || 3000)
|
29 | .option(['H', 'host'], 'Host to listen on', '0.0.0.0')
|
30 |
|
31 | const flags = args.parse(process.argv)
|
32 | let file = args.sub[0]
|
33 |
|
34 | if (!file) {
|
35 | try {
|
36 |
|
37 | const packageJson = require(path.resolve(process.cwd(), 'package.json'))
|
38 | file = packageJson.main || 'index.js'
|
39 | } catch (err) {
|
40 | if (err.code !== 'MODULE_NOT_FOUND') {
|
41 | console.error(`micro: Could not read \`package.json\`: ${err.message}`)
|
42 | process.exit(1)
|
43 | }
|
44 | }
|
45 | }
|
46 |
|
47 | if (!file) {
|
48 | console.error('micro: Please supply a file.')
|
49 | args.showHelp()
|
50 | }
|
51 |
|
52 | if (file[0] !== '/') {
|
53 | file = path.resolve(process.cwd(), file)
|
54 | }
|
55 |
|
56 | if (!isAsyncSupported()) {
|
57 | const asyncToGen = require('async-to-gen/register')
|
58 |
|
59 | const pathSep = process.platform === 'win32' ? '\\\\' : '/'
|
60 | const directoryName = path.parse(path.join(__dirname, '..')).base
|
61 |
|
62 |
|
63 | const fileDirectoryPath = path.parse(file).dir.split(path.sep).join(pathSep)
|
64 |
|
65 | asyncToGen({
|
66 | includes: new RegExp(`.*${directoryName}?${pathSep}(lib|bin)|${fileDirectoryPath}.*`),
|
67 | excludes: null,
|
68 | sourceMaps: false
|
69 | })
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | require('../lib')(file, flags)
|