UNPKG

2.37 kBJavaScriptView Raw
1#!/usr/bin/env node
2'use strict';
3
4const pkg = require('../package.json');
5const co = require('bluebird').coroutine;
6const path = require('path');
7const program = require('commander');
8const nconf = require('nconf');
9const ManagementClient = require('auth0').ManagementClient;
10const deploy = require('../lib/deploy');
11const token = require('../lib/token');
12
13const fail = (message) => {
14 console.error(message);
15 process.exit(1);
16};
17
18const hasToken = options => options.token;
19const hasClient = options => options.clientId && options.clientSecret;
20
21const 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
30const getClientInfo = (options) => {
31 return {
32 id: options.clientId,
33 secret: options.clientSecret
34 };
35};
36
37const AUTH0_DOMAIN = 'AUTH0_DOMAIN';
38
39const run = (type, options) => {
40 return co(function*() {
41
42 const defaults = {};
43 defaults[AUTH0_DOMAIN] = options.auth0Domain;
44
45 // make domain available in config
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
62const 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
73program
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
82setupProgram(program, 'resource', 'Create resource servers');
83setupProgram(program, 'connection', 'Create connections');
84setupProgram(program, 'client', 'Create clients');
85
86program.parse(process.argv);