1 | const co = require('bluebird').coroutine;
|
2 | const fs = require('mz/fs');
|
3 | const path = require('path');
|
4 | const _ = require('lodash');
|
5 | const confProcessor = require('./utils/config.processor.js');
|
6 | const fsUtils = require('./utils/file.utils.js');
|
7 |
|
8 | const SCRIPTS = ['login.js', 'create.js', 'verify.js', 'change_password.js', 'delete.js', 'get_user.js'];
|
9 | const CONFIG = 'config.json';
|
10 |
|
11 | const baseConnectionConfig = {
|
12 | strategy: 'auth0',
|
13 | options: {
|
14 | brute_force_protection: true,
|
15 | enabledDatabaseCustomization: true,
|
16 | customScripts: {}
|
17 | }
|
18 | };
|
19 |
|
20 | const create = (client, dir, config) => {
|
21 | return co(function* () {
|
22 |
|
23 | const connections = yield fsUtils.getDirectories(dir);
|
24 |
|
25 | for (let i in connections) {
|
26 | const connectionData = yield getConnectionData(connections[i], config);
|
27 |
|
28 | yield createConnection(client, connectionData);
|
29 | }
|
30 | })();
|
31 | };
|
32 |
|
33 | const getConnectionData = (dir, config) => {
|
34 | return co(function* () {
|
35 | const files = yield fs.readdir(dir);
|
36 |
|
37 | const scripts = files.filter(f => SCRIPTS.indexOf(f) > -1);
|
38 | const configFile = files.find(f => f === CONFIG);
|
39 |
|
40 | const scriptData = {};
|
41 | for (let i in scripts) {
|
42 | const script = scripts[i];
|
43 | const scriptKey = path.basename(script, '.js');
|
44 | scriptData[scriptKey] = yield fsUtils.getFileContent(path.join(dir, script));
|
45 | }
|
46 |
|
47 | const configData = (configFile) ? yield fsUtils.getFileContent(path.join(dir, configFile)) : '{}';
|
48 |
|
49 | return {
|
50 | name: path.basename(dir),
|
51 | config: confProcessor.process(confProcessor.matcher.basic, configData, config),
|
52 | data: scriptData
|
53 | };
|
54 |
|
55 | })();
|
56 | };
|
57 |
|
58 | const createConnection = (client, connectionInfo) => {
|
59 | return co(function* () {
|
60 |
|
61 | const connections = yield client.connections.getAll();
|
62 |
|
63 | const existingConnection = connections.find(conn => {
|
64 | return conn.name === connectionInfo.name;
|
65 | });
|
66 |
|
67 | if (existingConnection) {
|
68 | console.log(`Updating connection: ${connectionInfo.name}`);
|
69 |
|
70 | const delta = _.merge({}, baseConnectionConfig, connectionInfo.config);
|
71 |
|
72 | delta.options.enabledDatabaseCustomization = true;
|
73 | delete delta.strategy;
|
74 |
|
75 |
|
76 | delta.options.customScripts = connectionInfo.data;
|
77 |
|
78 | yield client.connections.update({id: existingConnection.id}, delta);
|
79 |
|
80 | console.log('Updated connection');
|
81 |
|
82 | } else {
|
83 | console.log(`Creating connection: ${connectionInfo.name}`);
|
84 | const connection = _.merge({}, baseConnectionConfig, connectionInfo.config);
|
85 | connection.name = connectionInfo.name;
|
86 | connection.options.customScripts = connectionInfo.data;
|
87 |
|
88 | yield client.connections.create(connection);
|
89 |
|
90 | console.log('Created connection');
|
91 | }
|
92 |
|
93 | })();
|
94 | };
|
95 |
|
96 | module.exports = {
|
97 | create
|
98 | };
|