|
2 | 2 |
|
3 | 3 | const fs = require('fs'); |
4 | 4 | const cheerio = require('cheerio'); |
5 | | -const request = require('request'); |
6 | 5 | const str = require('string'); |
7 | 6 |
|
8 | 7 | const htmlURL = 'https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes'; |
@@ -136,24 +135,31 @@ function extractElements(attributes) { |
136 | 135 |
|
137 | 136 | // A local copy of the MDN attributes web page has been saved for reference: |
138 | 137 | // fs.readFile('./data/attributes.html', 'utf-8', (error, html) => { |
139 | | -request(htmlURL, (error, response, html) => { |
140 | | - if (error) { |
| 138 | +fetch(htmlURL) |
| 139 | + .then(response => { |
| 140 | + if (!response.ok) { |
| 141 | + throw new Error(`HTTP error! status: ${response.status}`); |
| 142 | + } |
| 143 | + return response.text(); |
| 144 | + }) |
| 145 | + .then(html => { |
| 146 | + const html2 = html.split(/\n\r|\r\n|\r|\n/).map(l => l.trimRight()).join('\n'); |
| 147 | + // write back to the saved copy of MDN attributes so we can see the diff |
| 148 | + fs.writeFileSync(htmlPath, html2); |
| 149 | + |
| 150 | + const $ = cheerio.load(html); |
| 151 | + const attributes = extractAttributes($); |
| 152 | + const elements = extractElements(attributes); |
| 153 | + const out = { |
| 154 | + attributes, |
| 155 | + elements |
| 156 | + }; |
| 157 | + |
| 158 | + // Print out JSON with 4-space indentation formatting. |
| 159 | + // http://stackoverflow.com/a/11276104 |
| 160 | + const tabWidth = 4; |
| 161 | + fs.writeFileSync(dataPath, JSON.stringify(out, null, tabWidth)); |
| 162 | + }) |
| 163 | + .catch(error => { |
141 | 164 | throw error; |
142 | | - } |
143 | | - const html2 = html.split(/\n\r|\r\n|\r|\n/).map(l => l.trimRight()).join('\n'); |
144 | | - // write back to the saved copy of MDN attributes so we can see the diff |
145 | | - fs.writeFileSync(htmlPath, html2); |
146 | | - |
147 | | - const $ = cheerio.load(html); |
148 | | - const attributes = extractAttributes($); |
149 | | - const elements = extractElements(attributes); |
150 | | - const out = { |
151 | | - attributes, |
152 | | - elements |
153 | | - }; |
154 | | - |
155 | | - // Print out JSON with 4-space indentation formatting. |
156 | | - // http://stackoverflow.com/a/11276104 |
157 | | - const tabWidth = 4; |
158 | | - fs.writeFileSync(dataPath, JSON.stringify(out, null, tabWidth)); |
159 | | -}); |
| 165 | + }); |
0 commit comments