UNPKG

2.03 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// Native
4const path = require('path')
5
6// Packages
7const updateNotifier = require('update-notifier')
8const nodeVersion = require('node-version')
9const args = require('args')
10const isAsyncSupported = require('is-async-supported')
11
12// Ours
13const pkg = require('../package')
14
15// Throw an error if node version is too low
16if (nodeVersion.major < 6) {
17 console.error(`Error! Micro requires at least version 6 of Node. Please upgrade!`)
18 process.exit(1)
19}
20
21// Let user know if there's an update
22// This isn't important when deployed to Now
23if (!process.env.NOW && pkg.dist) {
24 updateNotifier({pkg}).notify()
25}
26
27args
28 .option('port', 'Port to listen on', process.env.PORT || 3000)
29 .option(['H', 'host'], 'Host to listen on', '0.0.0.0')
30
31const flags = args.parse(process.argv)
32let file = args.sub[0]
33
34if (!file) {
35 try {
36 // eslint-disable-next-line import/no-dynamic-require
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
47if (!file) {
48 console.error('micro: Please supply a file.')
49 args.showHelp()
50}
51
52if (file[0] !== '/') {
53 file = path.resolve(process.cwd(), file)
54}
55
56if (!isAsyncSupported()) {
57 const asyncToGen = require('async-to-gen/register')
58 // Support for keywords "async" and "await"
59 const pathSep = process.platform === 'win32' ? '\\\\' : '/'
60 const directoryName = path.parse(path.join(__dirname, '..')).base
61
62 // This is required to make transpilation work on Windows
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// Load package core with async/await support
73// If needed... Otherwise use the native implementation
74require('../lib')(file, flags)