mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 08:02:55 +00:00
127 lines
3.6 KiB
TypeScript
Executable File
127 lines
3.6 KiB
TypeScript
Executable File
/*
|
|
* Copyright (C) 2018, 2019 StApps
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
* Software Foundation, version 3.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
import {Logger} from '@openstapps/logger';
|
|
import * as commander from 'commander';
|
|
import {readFileSync} from 'fs';
|
|
import {join} from 'path';
|
|
import {URL} from 'url';
|
|
import {copy} from './copy';
|
|
import {indexSamples} from './e2e';
|
|
import {HttpClient} from './http-client';
|
|
|
|
const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'))
|
|
.toString());
|
|
|
|
const client = new HttpClient();
|
|
|
|
let actionDone = false;
|
|
|
|
process.on('unhandledRejection', async (error) => {
|
|
await Logger.error('unhandledRejection', error);
|
|
});
|
|
|
|
commander
|
|
.command('e2e <to>')
|
|
.description('Run in end to end test mode. Indexing all test files from @openstapp/core to the backend')
|
|
.option('-s --samples [path]', 'Path to @openstapp/core test files', './node_modules/@openstapps/core/test/resources')
|
|
.action(async (to, e2eCommand) => {
|
|
let toURL = '';
|
|
// validate url
|
|
try {
|
|
toURL = (new URL(to)).toString();
|
|
} catch (err) {
|
|
await Logger.error('expected parameter "to" to be valid url', err);
|
|
e2eCommand.outputHelp();
|
|
process.exit(-1);
|
|
}
|
|
|
|
actionDone = true;
|
|
|
|
indexSamples(client, {to: toURL, samples: e2eCommand.samples})
|
|
.then(() => {
|
|
Logger.ok('Done');
|
|
})
|
|
.catch(async (reason) => {
|
|
await Logger.error(reason);
|
|
});
|
|
});
|
|
|
|
commander
|
|
.command('copy <type> <from> <to> <batchSize>')
|
|
.version(pkgJson.version)
|
|
.description('Copy data from one instance to another')
|
|
.option(
|
|
'-s, --bulkSource <bulkSource>',
|
|
'The source identifier for the bulk to use with the target instance [copy]',
|
|
'copy',
|
|
)
|
|
// TODO: remove
|
|
.option('-a, --appVersion <version>', 'The App version to use [unset by default]')
|
|
.allowUnknownOption(false)
|
|
.action(async (type, from, to, batchSize, copyCommand) => {
|
|
|
|
// validate type
|
|
if (typeof type !== 'string') {
|
|
await Logger.error('expected parameter "type" to be of type: string');
|
|
copyCommand.outputHelp();
|
|
process.exit(-1);
|
|
}
|
|
|
|
let fromURL = '';
|
|
let toURL = '';
|
|
|
|
// validate urls
|
|
try {
|
|
fromURL = (new URL(from)).toString();
|
|
toURL = (new URL(to)).toString();
|
|
} catch (err) {
|
|
await Logger.error('expected parameters "from" and "to" to be valid urls', err);
|
|
copyCommand.outputHelp();
|
|
process.exit(-1);
|
|
}
|
|
|
|
// validate batchSize
|
|
if (isNaN(parseInt(batchSize, 10))) {
|
|
await Logger.error('expected parameter "batchSize" to be of type: number');
|
|
copyCommand.outputHelp();
|
|
process.exit(-1);
|
|
}
|
|
|
|
actionDone = true;
|
|
|
|
Logger.info(`Copying ${type} objects from ${fromURL} to ${toURL}`);
|
|
|
|
copy(client, {
|
|
batchSize: parseInt(batchSize, 10),
|
|
from: fromURL,
|
|
source: copyCommand.bulkSource,
|
|
to: toURL,
|
|
type: type,
|
|
version: copyCommand.appVersion,
|
|
})
|
|
.then(() => {
|
|
Logger.ok('Done');
|
|
}, (err) => {
|
|
throw err;
|
|
});
|
|
});
|
|
|
|
commander
|
|
.parse(process.argv);
|
|
|
|
if (!actionDone) {
|
|
commander.outputHelp();
|
|
}
|