11import { default as jsonPatch } from 'fast-json-patch' ;
22import { default as fs } from 'fs' ;
3+ import { default as path } from 'path' ;
4+ import { default as globule } from 'globule' ;
35
46export 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