Skip to content

Commit ca087a2

Browse files
committed
Adding config file support (klei-migrate.json)
1 parent 3d54a2d commit ca087a2

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

lib/cli.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ exports.init = function (args) {
5858
this._showVersion = parsed.version;
5959
this._showHelp = parsed.help || this.command() === 'help';
6060

61+
migrate.loadConfig();
62+
6163
migrate.args(argv);
6264

6365
migrate.limit(parsed.limit);
6466

65-
migrate.coffee(!!parsed.coffee);
67+
migrate.coffee(parsed.coffee);
6668

6769
migrate.env(parsed.env);
6870

lib/migrate.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ exports.coffee = function (newMode) {
172172
return !!this._coffee;
173173
};
174174

175+
/**
176+
* Getter for config file location
177+
*/
178+
exports.config = function () {
179+
return join(this.cwd(), 'klei-migrate.json');
180+
};
181+
182+
exports.loadConfig = function () {
183+
if (fs.existsSync(this.config())) {
184+
var config = require(this.config());
185+
this.timeout(config.timeout * 1000);
186+
this.templatePath(config.template);
187+
this.env(config.env);
188+
this.directory(config.directory);
189+
this.coffee(!!config.coffee);
190+
}
191+
return this;
192+
};
193+
175194
/**
176195
* Getter/Setter for migration timeout limit in ms
177196
*

tests/cli.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ var should = require('chai').should(),
44
join = require('path').join;
55

66
describe('klei-migrate cli', function () {
7+
beforeEach(function (done) {
8+
// Reset some options:
9+
cli.migrate.limit(0);
10+
cli.migrate.coffee(false);
11+
cli.migrate.env('development');
12+
cli.migrate.timeout(30000);
13+
done();
14+
});
15+
716
describe('init()', function () {
817
it('should set limit to 1 if --one is provided', function (done) {
918
cli.init(['--one']).migrate.limit().should.equal(1);
@@ -20,6 +29,17 @@ describe('klei-migrate cli', function () {
2029
done();
2130
});
2231

32+
it('should load options from config file if it exists', function (done) {
33+
cli.migrate.cwd(join(__dirname, 'config-migrations'));
34+
cli.init([]);
35+
cli.migrate.coffee().should.equal(true);
36+
cli.migrate.templatePath().should.equal(join(__dirname, 'config-migrations', '.migration.tpl.coffee'));
37+
cli.migrate.timeout().should.equal(600000);
38+
cli.migrate.env().should.equal('stage');
39+
cli.migrate.directory().should.equal(join(__dirname, 'config-migrations', 'migs'));
40+
done();
41+
});
42+
2343
it('should set limit to the provided --limit or -l', function (done) {
2444
cli.init(['--limit', '10']).migrate.limit().should.equal(10);
2545
cli.init(['-l', '5']).migrate.limit().should.equal(5);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"directory": "migs",
3+
"template": ".migration.tpl.coffee",
4+
"coffee": true,
5+
"env": "stage",
6+
"timeout": 600
7+
}

tests/migrate.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ describe('klei-migrate module', function () {
7474
});
7575
});
7676

77+
describe('config()', function () {
78+
beforeEach(loadMigrate);
79+
80+
it('should use process.cwd()+"/klei-migrate.json" by default', function (done) {
81+
migrate.config().should.equal(join(process.cwd(), 'klei-migrate.json'));
82+
done();
83+
});
84+
});
85+
86+
describe('loadConfig()', function () {
87+
beforeEach(loadMigrate);
88+
89+
it('should set klei-migrate options from config file if it exists', function (done) {
90+
migrate.cwd(join(__dirname, 'config-migrations')).loadConfig();
91+
migrate.directory().should.equal(join(__dirname, 'config-migrations', 'migs'));
92+
migrate.env().should.equal('stage');
93+
migrate.timeout().should.equal(600000);
94+
migrate.coffee().should.equal(true);
95+
migrate.templatePath().should.equal(join(__dirname, 'config-migrations', '.migration.tpl.coffee'));
96+
done();
97+
});
98+
});
99+
77100
describe('env()', function () {
78101
beforeEach(loadMigrate);
79102

0 commit comments

Comments
 (0)