-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 added application import feature
Now it is possible to import a list of applications with variants from a json input file
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {Arguments, Argv} from 'yargs'; | ||
import {UPSAdminClientFactory} from '../../utils/UPSAdminClientFactory'; | ||
import * as fs from 'fs'; | ||
import {PushApplication, Variant} from '@aerogear/unifiedpush-admin-client'; | ||
export const command = 'import'; | ||
|
||
export const describe = | ||
'Import a list of applications from a json file together with their variants. Such file can be the json output of the list command'; | ||
|
||
export const builder = (yargs: Argv) => { | ||
return yargs | ||
.group(['in', 'output'], 'Import applications:') | ||
.option('in', { | ||
required: true, | ||
type: 'string', | ||
describe: | ||
'Path to the json file containing the applications to be imported.', | ||
requiresArg: true, | ||
}) | ||
.help(); | ||
}; | ||
|
||
export const handler = async (argv: Arguments) => { | ||
const fileToString = (filePath: string): string => | ||
fs.readFileSync(filePath, 'utf8'); | ||
|
||
const appList: PushApplication[] = JSON.parse( | ||
fileToString(argv.in as string) | ||
); | ||
|
||
const newApps: PushApplication[] = []; | ||
|
||
await Promise.all( | ||
appList.map(async oldApp => { | ||
const variants: Variant[] = oldApp.variants || []; | ||
oldApp.variants = []; | ||
variants.forEach(v => { | ||
const vtmp = v as Record<string, string>; | ||
delete vtmp.id; | ||
delete vtmp.variantID; | ||
}); | ||
|
||
const newAppDefinition = ({...oldApp} as unknown) as Record< | ||
string, | ||
string | ||
>; | ||
delete newAppDefinition['pushApplicationID']; | ||
delete newAppDefinition['id']; | ||
|
||
try { | ||
// creating the apps | ||
const newApp = await UPSAdminClientFactory.getUpsAdminInstance(argv) | ||
.applications.create('') | ||
.withDefinition(newAppDefinition) | ||
.execute(); | ||
newApps.push(newApp); | ||
|
||
for (const v of variants) { | ||
await UPSAdminClientFactory.getUpsAdminInstance(argv) | ||
.variants[v.type].create(newApp.pushApplicationID) | ||
.withDefinition((v as unknown) as never) | ||
.execute(); | ||
} | ||
} catch (err) { | ||
77; | ||
console.log( | ||
'Failed importing app with ID', | ||
oldApp.pushApplicationID, | ||
err | ||
); | ||
} | ||
}) | ||
); | ||
console.log(`${newApps.length} application(s) imported`); | ||
}; |