summaryrefslogtreecommitdiff
path: root/tools/doc/addon-verify.js
blob: ae6a08b2cc64758893e7af520c0a0156965df3ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';

// doc/api/addons.md has a bunch of code.  Extract it for verification
// that the C++ code compiles and the js code runs.
// Add .gyp files which will be used to compile the C++ code.
// Modify the require paths in the js code to pull from the build tree.
// Triggered from the build-addons target in the Makefile and vcbuild.bat.

const { mkdir, writeFile } = require('fs');
const { resolve } = require('path');
const vfile = require('to-vfile');
const unified = require('unified');
const remarkParse = require('remark-parse');

const rootDir = resolve(__dirname, '..', '..');
const doc = resolve(rootDir, 'doc', 'api', 'addons.md');
const verifyDir = resolve(rootDir, 'test', 'addons');

const file = vfile.readSync(doc, 'utf8');
const tree = unified().use(remarkParse).parse(file);
const addons = {};
let id = 0;
let currentHeader;

const validNames = /^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/;
tree.children.forEach((node) => {
  if (node.type === 'heading') {
    currentHeader = file.contents.slice(
      node.children[0].position.start.offset,
      node.position.end.offset);
    addons[currentHeader] = { files: {} };
  } else if (node.type === 'code') {
    const match = node.value.match(validNames);
    if (match !== null) {
      addons[currentHeader].files[match[1]] = node.value;
    }
  }
});

Object.keys(addons).forEach((header) => {
  verifyFiles(addons[header].files, header);
});

function verifyFiles(files, blockName) {
  const fileNames = Object.keys(files);

  // Must have a .cc and a .js to be a valid test.
  if (!fileNames.some((name) => name.endsWith('.cc')) ||
      !fileNames.some((name) => name.endsWith('.js'))) {
    return;
  }

  blockName = blockName.toLowerCase().replace(/\s/g, '_').replace(/\W/g, '');
  const dir = resolve(
    verifyDir,
    `${String(++id).padStart(2, '0')}_${blockName}`
  );

  files = fileNames.map((name) => {
    if (name === 'test.js') {
      files[name] = `'use strict';
const common = require('../../common');
${files[name].replace(
    "'./build/Release/addon'",
    // eslint-disable-next-line no-template-curly-in-string
    '`./build/${common.buildType}/addon`')}
`;
    }
    return {
      path: resolve(dir, name),
      name: name,
      content: files[name]
    };
  });

  files.push({
    path: resolve(dir, 'binding.gyp'),
    content: JSON.stringify({
      targets: [
        {
          target_name: 'addon',
          defines: [ 'V8_DEPRECATION_WARNINGS=1' ],
          sources: files.map(({ name }) => name)
        }
      ]
    })
  });

  mkdir(dir, () => {
    // Ignore errors.

    files.forEach(({ path, content }) => {
      writeFile(path, content, (err) => {
        if (err)
          throw err;

        console.log(`Wrote ${path}`);
      });
    });
  });
}