Skip to content

Commit 91549a2

Browse files
committed
feat(RoleUtils): Add Bulk Load from Folder. Spelling Fixes
1 parent 53424ad commit 91549a2

File tree

4 files changed

+61
-54
lines changed

4 files changed

+61
-54
lines changed

api.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,14 @@ log.debug(`command line args: ${args}`);
194194

195195
break;
196196

197+
case 'bulkUpsertCustomRoleFolder':
198+
let roleFolder = args[1];
199+
result = await ldUtils.roles.bulkUpsertCustomRoleFolder(roleFolder);
200+
201+
break;
202+
197203
default:
198-
result = 'please supply a valid mode parameter';
204+
result = 'Please Supply a Valid Mode Parameter';
199205

200206
}
201207

package-lock.json

Lines changed: 30 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"bunyan": "^1.8.12",
3333
"bunyan-format": "^0.2.1",
3434
"fast-json-patch": "^2.0.6",
35+
"globule": "^1.2.0",
3536
"js-yaml": "^3.10.0",
3637
"ldclient-node": "^3.3.2",
3738
"swagger-client": "^3.4.7",

src/LaunchDarklyUtilsRoles.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { default as jsonPatch } from 'fast-json-patch';
22
import { default as fs } from 'fs';
3+
import { default as path } from 'path';
4+
import { default as globule } from 'globule';
35

46
export class LaunchDarklyUtilsRoles {
57
constructor(apiClient, log) {
@@ -66,7 +68,7 @@ export class LaunchDarklyUtilsRoles {
6668
return this.getCustomRole(customRoleKey)
6769

6870
.then(() => {
69-
that.log.info(`role '${customRoleKey}' found, updating..`);
71+
that.log.info(`Role '${customRoleKey}' Found, Updating...`);
7072
return this.updateCustomRole(
7173
customRoleKey,
7274
customRoleName,
@@ -75,8 +77,9 @@ export class LaunchDarklyUtilsRoles {
7577
);
7678
})
7779

78-
.catch(() => {
79-
that.log.info(`role '${customRoleKey}' not found, creating..`);
80+
.catch(e => {
81+
that.log.error(e);
82+
that.log.info(`Role '${customRoleKey}' Not Found, Creating...`);
8083
return this.createCustomRole(
8184
customRoleKey,
8285
customRoleName,
@@ -87,11 +90,11 @@ export class LaunchDarklyUtilsRoles {
8790
}
8891

8992
async bulkUpsertCustomRoles(roleBulkLoadFile) {
90-
let filePath = `${process.cwd()}/${roleBulkLoadFile}`;
93+
let filePath = path.resolve(roleBulkLoadFile);
9194
let roles = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
9295
let that = this;
9396

94-
this.log.info(`bulk upserting roles from file: ${filePath}`);
97+
this.log.info(`Bulk Upserting Roles from File: ${filePath}`);
9598

9699
return roles.reduce(function(acc, role) {
97100
return acc.then(function(results) {
@@ -102,4 +105,19 @@ export class LaunchDarklyUtilsRoles {
102105
});
103106
}, Promise.resolve([]));
104107
}
108+
109+
async bulkUpsertCustomRoleFolder(roleFolder) {
110+
let folderPath = path.normalize(path.resolve(roleFolder));
111+
let globMatch = folderPath + '/*.json';
112+
this.log.info(`Looking for Files with Pattern '${globMatch}'`);
113+
let fileArray = globule.find(globMatch);
114+
let results = [];
115+
let that = this;
116+
fileArray.forEach(async function(file) {
117+
that.log.info(`Found File '${file}'. Calling 'bulkUpsertCustomRoles'`);
118+
let result = await that.bulkUpsertCustomRoles(file);
119+
results.push(result);
120+
});
121+
return results;
122+
}
105123
}

0 commit comments

Comments
 (0)