1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 | const pkg = require('../package.json');
|
5 | const co = require('bluebird').coroutine;
|
6 | const path = require('path');
|
7 | const program = require('commander');
|
8 | const nconf = require('nconf');
|
9 | const ManagementClient = require('auth0').ManagementClient;
|
10 | const deploy = require('../lib/deploy');
|
11 | const token = require('../lib/token');
|
12 |
|
13 | const fail = (message) => {
|
14 | console.error(message);
|
15 | process.exit(1);
|
16 | };
|
17 |
|
18 | const hasToken = options => options.token;
|
19 | const hasClient = options => options.clientId && options.clientSecret;
|
20 |
|
21 | const checkRequired = options => {
|
22 | if (!options.auth0Domain) {
|
23 | fail('domain is required');
|
24 | }
|
25 | if (!hasToken(options) && !hasClient(options)) {
|
26 | fail('need to specify one of token or (client-id, client-secret)');
|
27 | }
|
28 | };
|
29 |
|
30 | const getClientInfo = (options) => {
|
31 | return {
|
32 | id: options.clientId,
|
33 | secret: options.clientSecret
|
34 | };
|
35 | };
|
36 |
|
37 | const AUTH0_DOMAIN = 'AUTH0_DOMAIN';
|
38 |
|
39 | const run = (type, options) => {
|
40 | return co(function*() {
|
41 |
|
42 | const defaults = {};
|
43 | defaults[AUTH0_DOMAIN] = options.auth0Domain;
|
44 |
|
45 |
|
46 | nconf.argv().env().defaults(defaults);
|
47 |
|
48 |
|
49 | const authToken = (hasToken(options)) ? options.token : yield token.get(options.auth0Domain,
|
50 | getClientInfo(options));
|
51 | const client = new ManagementClient({
|
52 | token: authToken,
|
53 | domain: options.auth0Domain
|
54 | });
|
55 |
|
56 | const workingDir = options.workingDir ? path.resolve(options.workingDir) : process.cwd();
|
57 |
|
58 | deploy(client, type, workingDir, nconf);
|
59 | })();
|
60 | };
|
61 |
|
62 | const setupProgram = (program, type, description) => {
|
63 | program
|
64 | .command(type)
|
65 | .allowUnknownOption(true)
|
66 | .description(description)
|
67 | .action(() => {
|
68 | checkRequired(program);
|
69 | run(type, program);
|
70 | });
|
71 | };
|
72 |
|
73 | program
|
74 | .version(pkg.version)
|
75 | .allowUnknownOption(true)
|
76 | .option('-t, --token <token>', 'Auth0 Management API token')
|
77 | .option('-c, --client-id <id>', 'Auth0 Client ID')
|
78 | .option('-s, --client-secret <secret>', 'Auth0 Client Secret')
|
79 | .option('-d, --auth0-domain <domain>', 'Auth0 Domain')
|
80 | .option('-w, --working-dir <workingdir>', 'working directory for auth0 components');
|
81 |
|
82 | setupProgram(program, 'resource', 'Create resource servers');
|
83 | setupProgram(program, 'connection', 'Create connections');
|
84 | setupProgram(program, 'client', 'Create clients');
|
85 |
|
86 | program.parse(process.argv);
|