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

Commit a4fd607

Browse files
author
Alexandr Ishchenko
committed
Changed require configuration to path relative to current working directory
For example working directory is `/home/foo/project_with_gemini/gemini/`. We use js or json default path configuration or use `-c` option. Run `./gemini` or `./gemini -c .very_special_configuration.json`. And we get configuration relative to current working directory: /home/foo/project_with_gemini/gemini/.gemini.conf.json /home/foo/project_with_gemini/gemini/.gemini.conf.js /home/foo/project_with_gemini/gemini/.very_special_configuration.json not in directory anywhere in the node_modules. Fixed #418.
1 parent d11c52c commit a4fd607

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

lib/config/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ Config.prototype.isCoverageEnabled = function() {
5353
return this.system.coverage.enabled;
5454
};
5555

56+
Config.isRelativePath = function(file) {
57+
return file.indexOf('./') === 0 || file.indexOf('/') !== 0;
58+
};
59+
60+
Config.getAbsPath = function(file) {
61+
return process.cwd() + '/' + file;
62+
};
63+
5664
function readConfig(filePath) {
5765
filePath = filePath || getDefaultConfig();
5866

@@ -81,7 +89,11 @@ function getDefaultConfig() {
8189

8290
function requireModule(file) {
8391
try {
84-
return require(file);
92+
if (Config.isRelativePath(file)) {
93+
return require(Config.getAbsPath(file));
94+
} else {
95+
return require(file);
96+
}
8597
} catch (e) {
8698
if (e.code === 'MODULE_NOT_FOUND') {
8799
throw new GeminiError('Config file does not exist: ' + file);

test/functional/config.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ describe('config', function() {
5151
return new Config(configPath('notExists.js'));
5252
}, GeminiError);
5353
});
54+
55+
it('should read relative to current working directory', function() {
56+
assert.deepPropertyVal(
57+
new Config(configPath('validConfig.js').replace(process.cwd() + '/', '')),
58+
'system.projectRoot', '/it/works');
59+
60+
assert.deepPropertyVal(
61+
new Config(configPath('validConfig.js').replace(process.cwd() + '/', './')),
62+
'system.projectRoot', '/it/works');
63+
});
5464
});
5565

5666
describe('.json', function() {
@@ -64,6 +74,16 @@ describe('config', function() {
6474
return new Config(configPath('notExists.json'));
6575
}, GeminiError);
6676
});
77+
78+
it('should read relative to current working directory', function() {
79+
assert.deepPropertyVal(
80+
new Config(configPath('validConfig.json').replace(process.cwd() + '/', '')),
81+
'system.projectRoot', '/it/works');
82+
83+
assert.deepPropertyVal(
84+
new Config(configPath('validConfig.json').replace(process.cwd() + '/', './')),
85+
'system.projectRoot', '/it/works');
86+
});
6787
});
6888
});
6989
});

test/unit/config-options/config-options.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,4 +866,26 @@ describe('config', function() {
866866
});
867867
});
868868
});
869+
870+
describe('util functions', function() {
871+
it('should return true if path is relative', function() {
872+
assert.strictEqual(Config.isRelativePath('./foo/bar'), true);
873+
assert.strictEqual(Config.isRelativePath('foo/bar'), true);
874+
});
875+
876+
it('should return false if path is absolute', function() {
877+
assert.strictEqual(Config.isRelativePath('/foo/bar'), false);
878+
});
879+
880+
it('should return absolute path by pass relative', function() {
881+
var relativePath = 'foo/bar/gemini.config';
882+
var relativePathWithLeadedDot = './foo/bar/gemini.config';
883+
assert.strictEqual(
884+
Config.getAbsPath(relativePath),
885+
process.cwd() + '/' + relativePath);
886+
assert.strictEqual(
887+
Config.getAbsPath(relativePathWithLeadedDot),
888+
process.cwd() + '/' + relativePathWithLeadedDot);
889+
});
890+
});
869891
});

0 commit comments

Comments
 (0)