1 | #!/usr/bin/env node
|
2 |
|
3 | var program = require('commander');
|
4 | var chalk = require('chalk');
|
5 | var inquirer = require('inquirer');
|
6 | var osenv = require('osenv');
|
7 | var path = require('path');
|
8 | var fs = require('fs');
|
9 | var _ = require('lodash');
|
10 | var async = require('async');
|
11 | var log = require('../lib/log');
|
12 | var updateNotifier = require('update-notifier');
|
13 | var shortid = require('shortid');
|
14 | var debug = require('debug')('4front:cli');
|
15 | var cliInit = require('../lib/cli-init');
|
16 | var pkg = require('../package.json');
|
17 |
|
18 | require('simple-errors');
|
19 |
|
20 |
|
21 | shortid.characters(
|
22 | '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$-');
|
23 |
|
24 | updateNotifier({
|
25 | packageName: pkg.name,
|
26 | packageVersion: pkg.version,
|
27 | updateCheckInterval: 1000 * 60 * 60 * 2
|
28 | }).notify();
|
29 |
|
30 | program.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 |
|
37 | program
|
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 |
|
48 | program
|
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 |
|
58 | program
|
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 |
|
68 | program
|
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 |
|
79 | program
|
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 |
|
90 | program
|
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 |
|
100 | program
|
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 |
|
108 | program
|
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 |
|
123 | program
|
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 |
|
134 | program
|
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 |
|
148 | program
|
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 |
|
161 | program
|
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 |
|
172 | program
|
173 | .command('default-profile')
|
174 | .action(commandAction('default-profile'));
|
175 |
|
176 | program.command('*')
|
177 | .action(function() {
|
178 | log.error("Invalid command " + process.argv.slice(2));
|
179 | });
|
180 |
|
181 | program.parse(process.argv);
|
182 |
|
183 | if (!process.argv.slice(2).length) {
|
184 | program.outputHelp();
|
185 | }
|
186 |
|
187 | process.on('exit', function(code) {
|
188 | log.debug("Exiting");
|
189 | });
|
190 |
|
191 | function commandAction(name, commandOptions) {
|
192 |
|
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 |
|
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 |
|
228 |
|
229 | if (!_.isFunction(onKill))
|
230 | process.exit();
|
231 | else {
|
232 |
|
233 | process.on('exit', function() {
|
234 | onKill();
|
235 | });
|
236 | }
|
237 | });
|
238 | });
|
239 | };
|
240 | }
|