UNPKG

6.28 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var program = require('commander');
4var chalk = require('chalk');
5var inquirer = require('inquirer');
6var osenv = require('osenv');
7var path = require('path');
8var fs = require('fs');
9var _ = require('lodash');
10var async = require('async');
11var log = require('../lib/log');
12var updateNotifier = require('update-notifier');
13var shortid = require('shortid');
14var debug = require('debug')('4front:cli');
15var cliInit = require('../lib/cli-init');
16var pkg = require('../package.json');
17
18require('simple-errors');
19
20// Limit any generated IDs to alpha-numeric characters
21shortid.characters(
22 '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$-');
23
24updateNotifier({
25 packageName: pkg.name,
26 packageVersion: pkg.version,
27 updateCheckInterval: 1000 * 60 * 60 * 2 // Check for updates every 2 hours
28}).notify();
29
30program.version(pkg.version)
31 .option('--debug', 'Emit debug messages')
32 .option('--token [token]', 'JSON web token')
33 .option('--profile [profileName]', 'Name of the profile')
34 .option('--app-id [appId]', 'Set appId (in place of the one defined in package.json)')
35
36// Create new application
37program
38 .option('--template-url [templateUrl]',
39 'URL to a zip file containing the code to scaffold the new app from.')
40 .command('create-app')
41 .description('Create a new 4front app')
42 .action(commandAction('create-app', {
43 requireAuth: true,
44 loadVirtualApp: false,
45 loadManifest: false
46 }));
47
48program
49 .command('delete-app')
50 .description('Delete an existing 4front app')
51 .action(commandAction('delete-app', {
52 requireAuth: true,
53 loadVirtualApp: true,
54 loadManifest: true
55 }));
56
57// List the applications for an organization
58program
59 .command('list-apps')
60 .description('List the applications for an organization')
61 .action(commandAction('list-apps', {
62 requireAuth: true,
63 loadVirtualApp: false,
64 loadManifest: false
65 }));
66
67// Create a new organization
68program
69 .option('--org-name [orgName]', "The name of the organization")
70 .command('create-organization')
71 .description("Create a new organization")
72 .action(commandAction("create-org", {
73 requireAuth: true,
74 loadManifest: false,
75 loadVirtualApp: false
76 }));
77
78// Add a new profile
79program
80 .option('--endpoint [endpoint]', "The url endpoint for the 4front instance")
81 .option('--profile-name [name]', "The name of the 4front profile")
82 .command('add-profile')
83 .description("Register a new profile")
84 .action(commandAction('add-profile', {
85 requireAuth: false,
86 loadManifest: false,
87 loadVirtualApp: false
88 }));
89
90program
91 .option('--profile-name [profileName]', "The name of the profile to remove")
92 .command('remove-profile')
93 .description('Remove a profile from the 4front config')
94 .action(commandAction('remove-profile', {
95 requireAuth: false,
96 loadManifest: false,
97 loadVirtualApp: false
98 }));
99
100program
101 .command('bind-app')
102 .description('Bind the current directory to an existing 4front app')
103 .action(commandAction('appBind', {
104 loadManifest: false
105 }));
106
107// Set an environment variable
108program
109 .option('--key [key]')
110 .option('--value [value]')
111 .option('--virtual-env [virtualEnv]')
112 .option('--encrypt')
113 .command('set-env')
114 .description('Set an environment variable')
115 .action(commandAction('env', {
116 requireAuth: true,
117 loadManifest: true,
118 loadVirtualApp: true,
119 subCommand: 'set'
120 }));
121
122// List the environment variables
123program
124 .command('list-env')
125 .description('List the environment variables')
126 .action(commandAction('env', {
127 requireAuth: true,
128 loadManifest: true,
129 loadVirtualApp: true,
130 subCommand: 'list'
131 }));
132
133// Launch the developer sandbox
134program
135 .option('-o, --open', 'Open a browser to the local server')
136 .option('--release', 'Run in release mode')
137 .option('--port <n>', 'Port number to listen on', parseInt)
138 .option('-l, --liveReload', 'Inject livereload script into html pages')
139 .command('dev')
140 .description("Start the developer sandbox environment")
141 .action(commandAction('dev-sandbox', {
142 requireAuth: true,
143 loadVirtualApp: true,
144 loadManifest: true
145 }));
146
147// Deploy app
148program
149 .option('--unattended', 'Run in unattended mode')
150 .option('--version-name [versionName]', 'Version name')
151 .option('-m, --message [message]', 'Version message')
152 .option('-f, --force', 'Force all production traffic to the new version')
153 .command('deploy')
154 .description('Deploy a new version of the app')
155 .action(commandAction('deploy', {
156 requireAuth: true,
157 loadVirtualApp: true,
158 loadManifest: true
159 }));
160
161program
162 .command('login')
163 .description("Login to 4front to generate a new JWT in the .4front.json file")
164 .action(commandAction('login', {
165 requireAuth: true,
166 forceLogin: true,
167 loadVirtualApp: false,
168 loadManifest: false
169 }));
170
171// Set the default profile
172program
173 .command('default-profile')
174 .action(commandAction('default-profile'));
175
176program.command('*')
177 .action(function() {
178 log.error("Invalid command " + process.argv.slice(2));
179 });
180
181program.parse(process.argv);
182
183if (!process.argv.slice(2).length) {
184 program.outputHelp();
185}
186
187process.on('exit', function(code) {
188 log.debug("Exiting");
189});
190
191function commandAction(name, commandOptions) {
192 // Extend any options from program to options.
193 return function() {
194 _.defaults(program, {
195 globalConfigPath: path.join(osenv.home(), '.4front.json'),
196 build: 'debug',
197 cwd: process.cwd(),
198 subCommand: commandOptions.subCommand
199 });
200
201 if (program.release)
202 program.build = 'release';
203
204 cliInit(program, commandOptions, function(err) {
205 if (err) {
206 if (err.status === 401)
207 log.error("Invalid credentials");
208 else
209 log.error(err);
210
211 return process.exit(1);
212 }
213
214 // Run the command
215 debug("run command %s", name);
216 require('../commands/' + name)(program, function(err, onKill) {
217 if (err) {
218 debug("error returned from command %o", err);
219 if (err instanceof Error)
220 log.error(err);
221 else if (_.isString(err))
222 log.error(err);
223
224 process.exit(1);
225 }
226
227 // Don't shutdown the process if keepAlive is specified.
228 //TODO: Could we print a message in the bottom bar with a message for how to stop?
229 if (!_.isFunction(onKill))
230 process.exit();
231 else {
232 // Wait for SIGINT to cleanup the command
233 process.on('exit', function() {
234 onKill();
235 });
236 }
237 });
238 });
239 };
240}