-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.ts
49 lines (41 loc) 路 1.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Enable debug output for Mongo Seeding
process.env.DEBUG = 'mongo-seeding';
import * as commandLineArgs from 'command-line-args';
import * as extend from 'extend';
import { seedDatabase } from 'mongo-seeding';
import {
populateCommandLineOptions,
optionsDefinition,
shouldShowHelp,
CommandLineOptions,
validateOptions,
populateEnvOptions,
} from './options';
import { showHelp } from './help';
import { AppConfig, DeepPartial } from 'mongo-seeding/dist/common';
export const run = async () => {
const options: CommandLineOptions = commandLineArgs(
optionsDefinition,
) as CommandLineOptions;
if (shouldShowHelp(options)) {
showHelp();
return;
}
const config = getConfig(options);
try {
validateOptions(options);
await seedDatabase(config);
} catch (err) {
printError(err);
}
process.exit(0);
};
const getConfig = (options: CommandLineOptions): DeepPartial<AppConfig> => {
const commandLineConfig = populateCommandLineOptions(options);
const envConfig = populateEnvOptions();
const config = {};
return extend(true, config, envConfig, commandLineConfig);
};
const printError = (err: Error) => {
console.error(`Error ${err.name}: ${err.message}`);
};