-
-
Notifications
You must be signed in to change notification settings - Fork 530
/
Copy pathmodel_generate.js
93 lines (83 loc) · 2.32 KB
/
model_generate.js
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
import { _baseOptions, _underscoreOption } from '../core/yargs';
import helpers from '../helpers';
import clc from 'cli-color';
exports.builder = (yargs) =>
_underscoreOption(
_baseOptions(yargs)
.option('name', {
describe: 'Defines the name of the new model',
type: 'string',
demandOption: true,
})
.option('attributes', {
describe: 'A list of attributes',
type: 'string',
demandOption: true,
})
.option('file-name', {
describe:
'Specify custom file name for the model created. Default name is model name in lowercase',
type: 'string',
demandOption: false,
})
.option('force', {
describe: 'Forcefully re-creates model with the same name',
type: 'string',
demandOption: false,
})
).argv;
exports.handler = function (args) {
ensureModelsFolder();
ensureMigrationsFolder();
checkModelFileExistence(args);
try {
helpers.model.generateFile(args);
} catch (err) {
helpers.view.error(err.message);
}
helpers.migration.generateTableCreationFile(args);
helpers.view.log(
'New model was created at',
clc.blueBright(helpers.path.getModelPath(args.name, args['file-name'])),
'.'
);
helpers.view.log(
'New migration was created at',
clc.blueBright(
helpers.path.getMigrationPath(
helpers.migration.generateMigrationName(args)
)
),
'.'
);
process.exit(0);
};
function ensureModelsFolder() {
if (!helpers.path.existsSync(helpers.path.getModelsPath())) {
helpers.view.error(
'Unable to find models path (' +
helpers.path.getModelsPath() +
'). Did you run ' +
clc.blueBright('sequelize init') +
'?'
);
}
}
function ensureMigrationsFolder() {
if (!helpers.path.existsSync(helpers.path.getPath('migration'))) {
helpers.view.error(
'Unable to find migrations path (' +
helpers.path.getPath('migration') +
'). Did you run ' +
clc.blueBright('sequelize init') +
'?'
);
}
}
function checkModelFileExistence(args) {
const modelPath = helpers.path.getModelPath(args.name, args['file-name']);
if (args.force === undefined && helpers.model.modelFileExists(modelPath)) {
helpers.view.notifyAboutExistingFile(modelPath);
process.exit(1);
}
}