|
1 | 1 | /** |
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. |
3 | 3 | */ |
4 | | -import _ from 'lodash'; |
5 | | -import glob from 'glob'; |
| 4 | + |
6 | 5 | import path from 'path'; |
7 | 6 | import fs from 'fs'; |
| 7 | +import _ from 'lodash'; |
| 8 | +import glob from 'glob'; |
8 | 9 |
|
9 | 10 | const formatCodeSnippet = (setup) => { |
10 | | - const paragraphs = []; |
| 11 | + const paragraphs = []; |
11 | 12 |
|
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 | + } |
15 | 16 |
|
16 | | - paragraphs.push(setup.code); |
| 17 | + paragraphs.push(setup.code); |
17 | 18 |
|
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 | + } |
23 | 24 |
|
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 | + } |
27 | 28 |
|
28 | | - return paragraphs.join('\n'); |
| 29 | + return paragraphs.join('\n'); |
29 | 30 | }; |
30 | 31 |
|
31 | 32 | 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')); |
33 | 34 |
|
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 | + }); |
37 | 38 |
|
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); |
40 | 42 |
|
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 | + }); |
46 | 48 |
|
47 | | - return _.zipObject(assertionNames, assertionCodes); |
| 49 | + return _.zipObject(assertionNames, assertionCodes); |
48 | 50 | }; |
49 | 51 |
|
50 | 52 | const updateDocuments = (assertions) => { |
51 | | - const readmeDocumentPath = path.join(__dirname, './../README.md'); |
| 53 | + const readmeDocumentPath = path.join(__dirname, '../README.md'); |
52 | 54 |
|
53 | | - let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8'); |
| 55 | + let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8'); |
54 | 56 |
|
55 | | - documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => { |
56 | | - let exampleBody; |
| 57 | + documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => { |
| 58 | + let exampleBody; |
57 | 59 |
|
58 | | - const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1]; |
| 60 | + const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1]; |
59 | 61 |
|
60 | | - const ruleAssertions = assertions[ruleName]; |
| 62 | + const ruleAssertions = assertions[ruleName]; |
61 | 63 |
|
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 + '".'); |
64 | 66 |
|
65 | | - return assertionsBlock; |
66 | | - } |
| 67 | + return assertionsBlock; |
| 68 | + } |
67 | 69 |
|
68 | | - exampleBody = ''; |
| 70 | + exampleBody = ''; |
69 | 71 |
|
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 | + } |
73 | 75 |
|
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 | + } |
77 | 79 |
|
78 | | - return exampleBody; |
79 | | - }); |
| 80 | + return exampleBody; |
| 81 | + }); |
80 | 82 |
|
81 | | - fs.writeFileSync(readmeDocumentPath, documentBody); |
| 83 | + fs.writeFileSync(readmeDocumentPath, documentBody); |
82 | 84 | }; |
83 | 85 |
|
84 | 86 | updateDocuments(getAssertions()); |
0 commit comments