|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const { mkdir, readFileSync, writeFile } = require('fs'); |
| 3 | +// doc/api/addons.md has a bunch of code. Extract it for verification |
| 4 | +// that the C++ code compiles and the js code runs. |
| 5 | +// Add .gyp files which will be used to compile the C++ code. |
| 6 | +// Modify the require paths in the js code to pull from the build tree. |
| 7 | +// Triggered from the build-addons target in the Makefile and vcbuild.bat. |
| 8 | + |
| 9 | +const { mkdir, writeFile } = require('fs'); |
4 | 10 | const { resolve } = require('path');
|
5 |
| -const { lexer } = require('marked'); |
| 11 | +const vfile = require('to-vfile'); |
| 12 | +const unified = require('unified'); |
| 13 | +const remarkParse = require('remark-parse'); |
6 | 14 |
|
7 | 15 | const rootDir = resolve(__dirname, '..', '..');
|
8 | 16 | const doc = resolve(rootDir, 'doc', 'api', 'addons.md');
|
9 | 17 | const verifyDir = resolve(rootDir, 'test', 'addons');
|
10 | 18 |
|
11 |
| -const tokens = lexer(readFileSync(doc, 'utf8')); |
| 19 | +const file = vfile.readSync(doc, 'utf8'); |
| 20 | +const tree = unified().use(remarkParse).parse(file); |
12 | 21 | const addons = {};
|
13 | 22 | let id = 0;
|
14 | 23 | let currentHeader;
|
15 | 24 |
|
16 | 25 | const validNames = /^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/;
|
17 |
| -tokens.forEach(({ type, text }) => { |
18 |
| - if (type === 'heading') { |
19 |
| - currentHeader = text; |
| 26 | +tree.children.forEach((node) => { |
| 27 | + if (node.type === 'heading') { |
| 28 | + currentHeader = file.contents.slice( |
| 29 | + node.children[0].position.start.offset, |
| 30 | + node.position.end.offset); |
20 | 31 | addons[currentHeader] = { files: {} };
|
21 |
| - } |
22 |
| - if (type === 'code') { |
23 |
| - const match = text.match(validNames); |
| 32 | + } else if (node.type === 'code') { |
| 33 | + const match = node.value.match(validNames); |
24 | 34 | if (match !== null) {
|
25 |
| - addons[currentHeader].files[match[1]] = text; |
| 35 | + addons[currentHeader].files[match[1]] = node.value; |
26 | 36 | }
|
27 | 37 | }
|
28 | 38 | });
|
|
0 commit comments