137 lines
4.4 KiB
TypeScript
137 lines
4.4 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import { SeedRunner, type ISeedOptions, type TSeedScenario } from './classes.seedrunner.js';
|
|
|
|
export { SeedRunner } from './classes.seedrunner.js';
|
|
|
|
const defaults: ISeedOptions = {
|
|
scenario: 'workspace',
|
|
adminEmail: 'admin@idp.global',
|
|
adminPassword: 'idp.global',
|
|
adminName: 'IDP Global Admin',
|
|
organizationName: 'Lossless GmbH',
|
|
organizationSlug: 'lossless',
|
|
};
|
|
|
|
const scenarios: TSeedScenario[] = ['admin', 'workspace', 'globalApps'];
|
|
|
|
const getArgValue = (nameArg: string) => {
|
|
const prefix = `--${nameArg}=`;
|
|
const prefixedArg = plugins.process.argv.find((arg) => arg.startsWith(prefix));
|
|
if (prefixedArg) {
|
|
return prefixedArg.slice(prefix.length);
|
|
}
|
|
const argIndex = plugins.process.argv.indexOf(`--${nameArg}`);
|
|
return argIndex >= 0 ? plugins.process.argv[argIndex + 1] : undefined;
|
|
};
|
|
|
|
const getScenarioFromArgs = (): TSeedScenario | null => {
|
|
const scenarioArg = getArgValue('scenario') as TSeedScenario | undefined;
|
|
return scenarioArg && scenarios.includes(scenarioArg) ? scenarioArg : null;
|
|
};
|
|
|
|
export const runCli = async () => {
|
|
const skipPrompts = plugins.process.argv.includes('--yes') || plugins.process.argv.includes('-y');
|
|
if (skipPrompts) {
|
|
const scenario = getScenarioFromArgs() || defaults.scenario;
|
|
const runner = new SeedRunner();
|
|
await runner.start();
|
|
try {
|
|
await runner.seed({
|
|
...defaults,
|
|
scenario,
|
|
adminEmail: getArgValue('adminEmail') || plugins.process.env.IDP_DEMO_ADMIN_EMAIL || defaults.adminEmail,
|
|
adminPassword: getArgValue('adminPassword') || plugins.process.env.IDP_DEMO_ADMIN_PASSWORD || defaults.adminPassword,
|
|
adminName: getArgValue('adminName') || plugins.process.env.IDP_DEMO_ADMIN_NAME || defaults.adminName,
|
|
organizationName: getArgValue('organizationName') || plugins.process.env.IDP_DEMO_ORG_NAME || defaults.organizationName,
|
|
organizationSlug: getArgValue('organizationSlug') || plugins.process.env.IDP_DEMO_ORG_SLUG || defaults.organizationSlug,
|
|
});
|
|
console.log('Seed complete.');
|
|
} finally {
|
|
await runner.stop();
|
|
}
|
|
return;
|
|
}
|
|
|
|
const interact = new plugins.smartinteract.SmartInteract();
|
|
|
|
const scenarioAnswer = await interact.askQuestion({
|
|
name: 'scenario',
|
|
type: 'list',
|
|
message: 'Which seed scenario do you want to apply?',
|
|
default: defaults.scenario,
|
|
choices: [
|
|
{ name: 'Demo workspace (admin, org, demo users, global apps)', value: 'workspace' },
|
|
{ name: 'Admin only (admin, org, global apps)', value: 'admin' },
|
|
{ name: 'Global apps only', value: 'globalApps' },
|
|
],
|
|
});
|
|
|
|
const scenario = scenarioAnswer.value as TSeedScenario;
|
|
const options: ISeedOptions = {
|
|
...defaults,
|
|
scenario,
|
|
};
|
|
|
|
if (scenario !== 'globalApps') {
|
|
options.adminEmail = (await interact.askQuestion({
|
|
name: 'adminEmail',
|
|
type: 'input',
|
|
message: 'Admin email:',
|
|
default: defaults.adminEmail,
|
|
})).value as string;
|
|
|
|
options.adminPassword = (await interact.askQuestion({
|
|
name: 'adminPassword',
|
|
type: 'password',
|
|
message: 'Admin password:',
|
|
default: defaults.adminPassword,
|
|
})).value as string;
|
|
|
|
options.adminName = (await interact.askQuestion({
|
|
name: 'adminName',
|
|
type: 'input',
|
|
message: 'Admin display name:',
|
|
default: defaults.adminName,
|
|
})).value as string;
|
|
|
|
options.organizationName = (await interact.askQuestion({
|
|
name: 'organizationName',
|
|
type: 'input',
|
|
message: 'Organization name:',
|
|
default: defaults.organizationName,
|
|
})).value as string;
|
|
|
|
options.organizationSlug = (await interact.askQuestion({
|
|
name: 'organizationSlug',
|
|
type: 'input',
|
|
message: 'Organization slug:',
|
|
default: defaults.organizationSlug,
|
|
})).value as string;
|
|
}
|
|
|
|
const confirmAnswer = await interact.askQuestion({
|
|
name: 'confirm',
|
|
type: 'confirm',
|
|
message: `Apply ${scenario} seed data to the configured database?`,
|
|
default: false,
|
|
});
|
|
|
|
if (!confirmAnswer.value) {
|
|
console.log('Seed cancelled.');
|
|
return;
|
|
}
|
|
|
|
const runner = new SeedRunner();
|
|
await runner.start();
|
|
try {
|
|
await runner.seed(options);
|
|
console.log('Seed complete.');
|
|
if (scenario !== 'globalApps') {
|
|
console.log(`Admin email: ${options.adminEmail}`);
|
|
console.log(`Admin password: ${options.adminPassword}`);
|
|
}
|
|
} finally {
|
|
await runner.stop();
|
|
}
|
|
};
|