UNPKG

5.72 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3/* eslint-disable no-console */ // this is a CLI entrypoint
4
5'use strict'
6
7const path = require('path')
8
9const meow = require('meow')
10const updateNotifier = require('update-notifier')
11const chalk = require('chalk')
12const execa = require('execa')
13
14const BlinkMobileIdentity = require('@blinkmobile/bm-identity')
15
16const pkg = require('../package.json')
17
18const blinkMobileIdentity = new BlinkMobileIdentity(pkg.name)
19
20updateNotifier({ pkg }).notify()
21
22const help = `
23Usage: blinkm server <command>
24
25Where command is one of:
26
27 info, serve, scope, deploy, logs, serverless
28
29Local development:
30
31 info => displays project information
32 --env <environment> => optionally sets the environment to display information for, defaults to 'dev'
33 --cwd <path> => optionally set the path to project, defaults to current working directory
34 serve => start a local development server using local API files
35 --env <environment> => optionally sets the environment to load environment variables from, defaults to 'dev'
36 --port <port> => optionally sets the port to use for server, defaults to 3000
37 --cwd <path> => optionally set the path to project, defaults to current working directory
38
39Initial settings:
40
41 scope => displays the current scope
42 <project> => sets the project id
43 --region <region> => optionally sets the region
44 --cwd <path> => optionally set the path to project, defaults to current working directory
45
46Deploying server side code:
47
48 The deploy command requires a login to BlinkMobile before use.
49 For help on the login and logout commands please see:
50 https://github.com/blinkmobile/identity-cli#usage
51
52 deploy => deploy the project
53 --force => deploy without confirmation
54 --env <environment> => optionally sets the environment to deploy to, defaults to 'dev'
55 --cwd <path> => optionally set the path to project, defaults to current working directory
56 --provision => force full deployment, only use this if told to by support
57
58Viewing server logs:
59
60 logs => view logs for project
61 --tail => keep listening for new logs in your terminal session
62 --filter <filterPattern> => optionally set a search filter, defaults to all logs
63 --start-time <startTime> => a unit in time to start fetching logs from (ie: 2010-10-20 or 1469705761), defaults to all logs
64 --env <environment> => optionally set the environment to view logs for, defaults to 'dev'
65 --cwd <path> => optionally set the path to project, defaults to current working directory
66
67Create serverless project:
68
69 serverless => create serverless project
70 --deployment-bucket <bucket> => set the deployment S3 bucket
71 --execution-role <role> => set the execution IAM Role ARN
72 --vpc-security-groups <groups> => comma separated list of VPC Security Group identifiers
73 --vpc-subnets <subnets> => comma separated list of VPC Subnet identifiers
74 --bm-server-version <version> => server version of @blinkmobile/sever-cli that the project was created with
75 --env <environment> => optionally set the environment, defaults to 'dev'
76 --cwd <path> => optionally set the path to project, defaults to current working directory
77 --analytics-collector-token <token> => optionally add an analytics collector token
78 --analytics-origin <origin> => optionally add an analytics origin
79`
80
81const cli = meow({
82 help,
83 flags: {
84 'force': {
85 type: 'boolean',
86 default: false
87 },
88 'tail': {
89 type: 'boolean',
90 default: false
91 },
92 'provision': {
93 type: 'boolean',
94 default: false
95 },
96 'bmServerVersion': {
97 type: 'string',
98 default: pkg.version
99 },
100 'cwd': {
101 type: 'string',
102 default: process.cwd()
103 },
104 'deploymentBucket': {
105 type: 'string'
106 },
107 'env': {
108 type: 'string',
109 default: 'dev'
110 },
111 'executionRole': {
112 type: 'string'
113 },
114 'filter': {
115 type: 'string'
116 },
117 'out': {
118 type: 'string'
119 },
120 'port': {
121 type: 'string'
122 },
123 'region': {
124 type: 'string',
125 default: 'ap-southeast-2'
126 },
127 'startTime': {
128 type: 'string'
129 },
130 'vpcSecurityGroups': {
131 type: 'string'
132 },
133 'vpcSubnets': {
134 type: 'string'
135 },
136 'analyticsCollectorToken': {
137 type: 'string'
138 },
139 'analyticsOrigin': {
140 type: 'string'
141 }
142 }
143})
144
145const command = cli.input[0]
146
147if (!command) {
148 cli.showHelp(0)
149}
150
151let main
152try {
153 main = require(path.join(__dirname, '..', 'commands', `${command}.js`))
154} catch (err) {
155 console.error(chalk.red(`
156Unknown command: ${command}`))
157 cli.showHelp(1)
158}
159
160if (typeof main !== 'function') {
161 console.error(chalk.red(`
162Command not implemented: ${command}`))
163 cli.showHelp(1)
164}
165
166const input = cli.input.slice(1)
167const options = {
168 blinkMobileIdentity
169}
170
171Promise.resolve()
172 .then(() => main(input, cli.flags, console, options))
173 .catch((err) => {
174 return execa('npm', ['-v'])
175 .then(({ stdout: npmVersion }) => {
176 console.error(`
177There was a problem executing '${command}':
178
179${chalk.red(err)}
180
181Please fix the error and try again.
182
183${chalk.grey(`Your Environment Information:
184 Server CLI Version: v${pkg.version}
185 Node Version: ${process.version}
186 NPM Version: v${npmVersion}`)}`)
187 process.exitCode = 1
188 })
189 })