Files
openstapps/examples/minimal-connector/src/cli.ts
2023-05-31 15:30:25 +02:00

68 lines
2.4 KiB
TypeScript

/*
* 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 {Command} from 'commander';
import {readFileSync} from 'fs';
import path from 'path';
import {executeConnector, isValidSCNamespace} from './common.js';
import {MinimalConnector} from './minimal-connector.js';
import * as url from 'url';
process.on('unhandledRejection', error => {
throw error;
});
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
const connectorVersion = JSON.parse(
readFileSync(path.join(dirname, '..', 'package.json')).toString(),
).version;
const commander = new Command();
/**
* Uses arguments to parameterize the connector execution
*
* backendURL - URL of the StApps backend deployment e.g. http://localhost:3000
* origin - Origin, where the data comes from.
* Typically, the name of the connector e.g., minimal-connector licensePlate - The license plate of your school.
* Must be matched to a SCNamespace e.g., f-u
*/
commander
.command('run <backendURL> <origin> <licensePlate>')
.version(connectorVersion)
.action(async (backendURL: string, origin: string, licensePlate: string) => {
if (backendURL.length === 0) {
throw new Error('Param "backend" needs to have a length greater zero.');
}
const originRegex = /^[a-z\-_0-9]+$/;
if (!originRegex.test(origin)) {
throw new Error(
'Origin name can only consist of lowercase letters from a-z, "-", "_" and integer numbers.',
);
}
if (!isValidSCNamespace(licensePlate)) {
throw new Error('Not a valid license plate. Please register a namespace with a unique-id in "core"');
}
// TODO for connector-developers: set your connector here
const connector = new MinimalConnector(licensePlate, origin);
await executeConnector(backendURL, connector);
Logger.ok('Done');
});
commander.parse(process.argv);