/
cli.ts
131 lines (108 loc) 路 4.06 KB
/
cli.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
import 'reflect-metadata';
import * as path from 'path';
import * as fs from 'fs';
import * as cliProgress from 'cli-progress';
import * as commander from 'commander';
import * as resolveFrom from 'resolve-from';
import * as yargsParser from 'yargs-parser';
import chalk from 'chalk';
import { Loader } from './Loader';
import { createConnection, fixturesIterator } from './util';
import { Resolver } from './Resolver';
import { getConnection, getRepository } from 'typeorm';
import { Builder } from './Builder';
import { Parser } from './Parser';
commander
.version(require('../package.json').version, '-v, --version')
.usage('[options] <path> Fixtures folder/file path')
.arguments('<path> [otherPaths...]')
.action((fixturesPath: string, otherPaths: Array<string>, options) => {
options.paths = [fixturesPath, ...otherPaths];
})
.option('--require <package>', 'A list of additional modules. e.g. ts-node/register')
.option('-c, --config <path>', 'TypeORM config path', 'ormconfig.yml')
.option('-cn, --connection [value]', 'TypeORM connection name', 'default')
.option('-s --sync', 'Database schema sync')
.option('-d --debug', 'Enable debug')
.option('--no-color', 'Disable color');
commander.parse(process.argv);
const argv = yargsParser(process.argv.slice(2));
if (argv.require) {
const requires = Array.isArray(argv.require) ? argv.require : [argv.require];
for (const req of requires) {
require(resolveFrom.silent(process.cwd(), req) || req);
}
}
if (!commander.paths) {
console.error('Path to fixtureConfigs folder is not passed.\n');
commander.outputHelp();
process.exit(1);
}
const debug = (message: string) => {
if (commander.debug) {
console.log(chalk.grey(message)); // tslint:disable-line
}
};
const error = (message: string) => {
console.log(chalk.red(message)); // tslint:disable-line
};
const typeOrmConfigPath = path.resolve(commander.config);
if (!fs.existsSync(typeOrmConfigPath)) {
throw new Error(`TypeOrm config ${typeOrmConfigPath} not found`);
}
debug('Connection to database...');
createConnection(
{
root: path.dirname(typeOrmConfigPath),
configName: path.basename(typeOrmConfigPath, path.extname(typeOrmConfigPath)),
},
commander.connection,
)
.then(async connection => {
debug('Database is connected');
if (commander.sync) {
debug('Synchronize database schema');
await connection.synchronize(true);
}
debug('Loading fixtureConfigs');
const loader = new Loader();
commander.paths.forEach((fixturePath: string) => {
loader.load(path.resolve(fixturePath));
});
debug('Resolving fixtureConfigs');
const resolver = new Resolver();
const fixtures = resolver.resolve(loader.fixtureConfigs);
const builder = new Builder(connection, new Parser());
const bar = new cliProgress.Bar({
format: `${chalk.yellow('Progress')} ${chalk.green('[{bar}]')} ${chalk.yellow(
'{percentage}% | ETA: {eta}s | {value}/{total} {name}',
)} `,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
fps: 5,
stream: process.stdout,
barsize: 50,
});
bar.start(fixtures.length, 0, { name: '' });
for (const fixture of fixturesIterator(fixtures)) {
const entity: any = await builder.build(fixture);
try {
bar.increment(1, { name: fixture.name });
await connection.getRepository(entity.constructor.name).save(entity);
} catch (e) {
bar.stop();
throw e;
}
}
bar.update(fixtures.length, { name: '' });
bar.stop();
debug('\nDatabase disconnect');
await connection.close();
})
.catch(async e => {
error('Fail fixture loading: ' + e.message);
await getConnection().close();
console.log(e); // tslint:disable-line
process.exit(1);
});