Skip to content

Commit 0e83952

Browse files
committed
fix: update the readme assertions script
1 parent 9ab3529 commit 0e83952

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

bin/readmeAssertions.js

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,86 @@
11
/**
2-
* This script is used to inline assertions into the README.md documents.
2+
* @file This script is used to inline assertions into the README.md documents.
33
*/
4-
import _ from 'lodash';
5-
import glob from 'glob';
4+
65
import path from 'path';
76
import fs from 'fs';
7+
import _ from 'lodash';
8+
import glob from 'glob';
89

910
const formatCodeSnippet = (setup) => {
10-
const paragraphs = [];
11+
const paragraphs = [];
1112

12-
if (setup.options) {
13-
paragraphs.push('// Options: ' + JSON.stringify(setup.options));
14-
}
13+
if (setup.options) {
14+
paragraphs.push('// Options: ' + JSON.stringify(setup.options));
15+
}
1516

16-
paragraphs.push(setup.code);
17+
paragraphs.push(setup.code);
1718

18-
if (setup.errors) {
19-
setup.errors.forEach((message) => {
20-
paragraphs.push('// Message: ' + message.message);
21-
});
22-
}
19+
if (setup.errors) {
20+
setup.errors.forEach((message) => {
21+
paragraphs.push('// Message: ' + message.message);
22+
});
23+
}
2324

24-
if (setup.rules) {
25-
paragraphs.push('// Additional rules: ' + JSON.stringify(setup.rules));
26-
}
25+
if (setup.rules) {
26+
paragraphs.push('// Additional rules: ' + JSON.stringify(setup.rules));
27+
}
2728

28-
return paragraphs.join('\n');
29+
return paragraphs.join('\n');
2930
};
3031

3132
const getAssertions = () => {
32-
const assertionFiles = glob.sync(path.resolve(__dirname, './../tests/rules/assertions/*.js'));
33+
const assertionFiles = glob.sync(path.resolve(__dirname, '../test/rules/assertions/*.js'));
3334

34-
const assertionNames = _.map(assertionFiles, (filePath) => {
35-
return path.basename(filePath, '.js');
36-
});
35+
const assertionNames = _.map(assertionFiles, (filePath) => {
36+
return path.basename(filePath, '.js');
37+
});
3738

38-
const assertionCodes = _.map(assertionFiles, (filePath) => {
39-
const codes = require(filePath);
39+
const assertionCodes = _.map(assertionFiles, (filePath) => {
40+
// eslint-disable-next-line global-require, import/no-dynamic-require
41+
const codes = require(filePath);
4042

41-
return {
42-
valid: _.map(codes.valid, formatCodeSnippet),
43-
invalid: _.map(codes.invalid, formatCodeSnippet)
44-
};
45-
});
43+
return {
44+
invalid: _.map(codes.invalid, formatCodeSnippet),
45+
valid: _.map(codes.valid, formatCodeSnippet)
46+
};
47+
});
4648

47-
return _.zipObject(assertionNames, assertionCodes);
49+
return _.zipObject(assertionNames, assertionCodes);
4850
};
4951

5052
const updateDocuments = (assertions) => {
51-
const readmeDocumentPath = path.join(__dirname, './../README.md');
53+
const readmeDocumentPath = path.join(__dirname, '../README.md');
5254

53-
let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8');
55+
let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8');
5456

55-
documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => {
56-
let exampleBody;
57+
documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => {
58+
let exampleBody;
5759

58-
const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1];
60+
const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1];
5961

60-
const ruleAssertions = assertions[ruleName];
62+
const ruleAssertions = assertions[ruleName];
6163

62-
if (!ruleAssertions) {
63-
throw new Error('No assertions available for rule "' + ruleName + '".');
64+
if (!ruleAssertions) {
65+
throw new Error('No assertions available for rule "' + ruleName + '".');
6466

65-
return assertionsBlock;
66-
}
67+
return assertionsBlock;
68+
}
6769

68-
exampleBody = '';
70+
exampleBody = '';
6971

70-
if (ruleAssertions.invalid.length) {
71-
exampleBody += 'The following patterns are considered problems:\n\n```js\n' + ruleAssertions.invalid.join('\n\n') + '\n```\n\n';
72-
}
72+
if (ruleAssertions.invalid.length) {
73+
exampleBody += 'The following patterns are considered problems:\n\n```js\n' + ruleAssertions.invalid.join('\n\n') + '\n```\n\n';
74+
}
7375

74-
if (ruleAssertions.valid.length) {
75-
exampleBody += 'The following patterns are not considered problems:\n\n```js\n' + ruleAssertions.valid.join('\n\n') + '\n```\n\n';
76-
}
76+
if (ruleAssertions.valid.length) {
77+
exampleBody += 'The following patterns are not considered problems:\n\n```js\n' + ruleAssertions.valid.join('\n\n') + '\n```\n\n';
78+
}
7779

78-
return exampleBody;
79-
});
80+
return exampleBody;
81+
});
8082

81-
fs.writeFileSync(readmeDocumentPath, documentBody);
83+
fs.writeFileSync(readmeDocumentPath, documentBody);
8284
};
8385

8486
updateDocuments(getAssertions());

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"eslint": "^3.19.0",
2424
"eslint-config-canonical": "8.2.1",
2525
"gitdown": "^2.5.0",
26+
"glob": "^7.1.2",
2627
"husky": "^0.13.4",
2728
"jsonlint": "^1.6.2",
2829
"mocha": "^3.4.2",

0 commit comments

Comments
 (0)