Skip to content

Commit 5cb7ce0

Browse files
author
Jon Ursenbach
committed
Adding support for loading custom rulesets off the filesystem.
1 parent 2008684 commit 5cb7ce0

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

docs/rules/2-custom-rulesets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ There is a reserved `require` property (type `string`) at the top level, which c
6464

6565
### Using Custom Rulesets
6666

67-
The `speccy lint` command supports `--rules`, and currently the value needs to be a URL to your custom ruleset file. Local file paths will not work. Pull requests welcome!
67+
The `speccy lint` command supports `--rules`, and the value can either be a URL to your custom ruleset file, or a path to a ruleset on your filesystem.
6868

6969
<hr>
7070

lib/loader.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ const recursivelyLoadRuleFiles = async (file, loadedFiles, options) => {
9393
if (verbose > 1) console.log('GET ' + file);
9494
data = await fetchUrl(file);
9595
}
96+
else if (fs.existsSync(file)) {
97+
if (verbose > 1) console.log('READ ' + file);
98+
data = yaml.safeLoad(fs.readFileSync(file, 'utf8'));
99+
}
96100
else {
97101
const ruleFile = path.join(__dirname, '../rules/' + file + '.json');
98-
if (verbose > 1) console.log('GET ' + ruleFile);
102+
if (verbose > 1) console.log('READ ' + ruleFile);
99103
data = yaml.safeLoad(fs.readFileSync(ruleFile, 'utf8'));
100104
}
101105

test/loader.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ describe('Loader', () => {
1717
loader.loadRuleFiles(['strict']).should.be.fulfilledWith(['strict', 'default']);
1818
});
1919

20-
2120
it('load default & strict rules', () => {
2221
loader.loadRuleFiles(['strict', 'default']).should.be.fulfilledWith(['strict', 'default']);
2322
});
2423

24+
context('when loading from a local file', () => {
25+
it('retrieves rules from the file', () => {
26+
const file = __dirname + '/../rules/strict.json'
27+
loader.loadRuleFiles([file]).should.be.fulfilledWith([file, 'default']);
28+
})
29+
})
30+
2531
context('when loading from url', () => {
2632
const host = 'http://example.org';
2733
const url = host + '/';

0 commit comments

Comments
 (0)