Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit 602acd6

Browse files
authored
Merge pull request #27 from segemun/patch-3
Try to load models and migrations path from sequelizerc file
2 parents 753c9d7 + d0ae56a commit 602acd6

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

bin/makemigration.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const commandLineArgs = require('command-line-args');
44
const beautify = require('js-beautify').js_beautify;
55

66
let migrate = require("../lib/migrate");
7+
let pathConfig = require('../lib/pathconfig');
78

89
const fs = require("fs");
910
const path = require("path");
@@ -31,13 +32,7 @@ if (options.help)
3132
process.exit(0);
3233
}
3334

34-
if(!process.env.PWD){
35-
process.env.PWD = process.cwd()
36-
}
37-
38-
let migrationsDir = path.join(process.env.PWD, options['migrations-path'] || 'migrations'),
39-
modelsDir = path.join(process.env.PWD, options['models-path'] || 'models');
40-
35+
let {migrationsDir, modelsDir} = pathConfig(options);
4136

4237
// current state
4338
const currentState = {
@@ -56,7 +51,6 @@ try {
5651
} catch (e) { }
5752

5853
//console.log(path.join(migrationsDir, '_current.json'), JSON.parse(fs.readFileSync(path.join(migrationsDir, '_current.json') )))
59-
6054
let sequelize = require(modelsDir).sequelize;
6155

6256
let models = sequelize.models;

bin/runmigration.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fs = require("fs");
66
const Async = require("async");
77

88
const migrate = require("../lib/migrate");
9+
const pathConfig = require('../lib/pathconfig');
910

1011
const optionDefinitions = [
1112
{ name: 'rev', alias: 'r', type: Number, description: 'Set migration revision (default: 0)', defaultValue: 0 },
@@ -19,12 +20,7 @@ const optionDefinitions = [
1920

2021
const options = commandLineArgs(optionDefinitions);
2122

22-
if(!process.env.PWD){
23-
process.env.PWD = process.cwd()
24-
}
25-
26-
let migrationsDir = path.join(process.env.PWD, options['migrations-path'] || 'migrations'),
27-
modelsDir = path.join(process.env.PWD, options['models-path'] || 'models');
23+
let {migrationsDir, modelsDir} = pathConfig(options);
2824

2925
if (options.help)
3026
{

lib/pathconfig.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require('path'),
2+
fs = require('fs');
3+
4+
module.exports = function(options){
5+
let sequelizercConfigs = [],
6+
sequelizercPath = path.join(process.env.PWD, '.sequelizerc');
7+
8+
if (fs.existsSync(sequelizercPath)){
9+
sequelizercConfigs = require(sequelizercPath);
10+
}
11+
12+
if(!process.env.PWD){
13+
process.env.PWD = process.cwd()
14+
}
15+
16+
let migrationsDir = path.join(process.env.PWD, 'migrations'),
17+
modelsDir = path.join(process.env.PWD, 'models');
18+
19+
if (options['migrations-path']) {
20+
migrationsDir = path.join(process.env.PWD, options['migrations-path']);
21+
} else if (sequelizercConfigs['migrations-path']) {
22+
migrationsDir = sequelizercConfigs['migrations-path'];
23+
}
24+
25+
if (options['models-path']) {
26+
modelsDir = path.join(process.env.PWD, options['models-path']);
27+
} else if (sequelizercConfigs['models-path']) {
28+
modelsDir = sequelizercConfigs['models-path'];
29+
}
30+
31+
return {
32+
migrationsDir: migrationsDir,
33+
modelsDir: modelsDir
34+
}
35+
}

0 commit comments

Comments
 (0)