UNPKG

2.6 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, name, 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 const authToken = (hasToken(options)) ? options.token : yield token.get(options.auth0Domain,
49 getClientInfo(options));
50 const client = new ManagementClient({
51 token: authToken,
52 domain: options.auth0Domain
53 });
54
55 const workingDir = options.workingDir ? path.resolve(options.workingDir) : process.cwd();
56
57 if (name) {
58 yield deploy.single(client, type, workingDir, name, nconf);
59 } else {
60 yield deploy.all(client, type, workingDir, nconf);
61 }
62
63 })().catch(err => {
64 fail(err.message);
65 });
66};
67
68const setupProgram = (program, type, description) => {
69 program
70 .command(`${type} [name]`)
71 .allowUnknownOption(true)
72 .description(description)
73 .action((name) => {
74 checkRequired(program);
75 run(type, name, program);
76 });
77};
78
79program
80 .version(pkg.version)
81 .allowUnknownOption(true)
82 .option('-t, --token <token>', 'Auth0 Management API token')
83 .option('-c, --client-id <id>', 'Auth0 Client ID')
84 .option('-s, --client-secret <secret>', 'Auth0 Client Secret')
85 .option('-d, --auth0-domain <domain>', 'Auth0 Domain')
86 .option('-w, --working-dir <workingdir>', 'working directory for auth0 components');
87
88setupProgram(program, 'resource', 'Create resource servers');
89setupProgram(program, 'connection', 'Create connections');
90setupProgram(program, 'client', 'Create clients');
91setupProgram(program, 'rule', 'Create rule');
92
93program.parse(process.argv);