Skip to content

Commit

Permalink
feat: 🎸 added application import feature
Browse files Browse the repository at this point in the history
Now it is possible to import a list of applications with variants from a
json input file
  • Loading branch information
ziccardi committed Dec 16, 2020
1 parent e2dcdd4 commit 8e800e4
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/cmds/app-cmds/import.ts
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`);
};

0 comments on commit 8e800e4

Please sign in to comment.