summaryrefslogtreecommitdiff
path: root/tools/doc/addon-verify.js
blob: 2e72abb77f925f1a899519cf6466b7a14bbf0cf8 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';

const fs = require('fs');
const path = require('path');
const marked = require('marked');

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

const contents = fs.readFileSync(doc).toString();

const tokens = marked.lexer(contents);
let id = 0;

let currentHeader;
const addons = {};
tokens.forEach((token) => {
  if (token.type === 'heading' && token.text) {
    currentHeader = token.text;
    addons[currentHeader] = {
      files: {}
    };
  }
  if (token.type === 'code') {
    var match = token.text.match(/^\/\/\s+(.*\.(?:cc|h|js))[\r\n]/);
    if (match !== null) {
      addons[currentHeader].files[match[1]] = token.text;
    }
  }
});
for (var header in addons) {
  verifyFiles(addons[header].files,
              header,
              console.log.bind(null, 'wrote'),
              function(err) { if (err) throw err; });
}

function once(fn) {
  var once = false;
  return function() {
    if (once)
      return;
    once = true;
    fn.apply(this, arguments);
  };
}

function verifyFiles(files, blockName, onprogress, ondone) {
  // must have a .cc and a .js to be a valid test
  if (!Object.keys(files).some((name) => /\.cc$/.test(name)) ||
      !Object.keys(files).some((name) => /\.js$/.test(name))) {
    return;
  }

  blockName = blockName
    .toLowerCase()
    .replace(/\s/g, '_')
    .replace(/[^a-z\d_]/g, '');
  const dir = path.resolve(
    verifyDir,
    `${(++id < 10 ? '0' : '') + id}_${blockName}`
  );

  files = Object.keys(files).map(function(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: path.resolve(dir, name),
      name: name,
      content: files[name]
    };
  });

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

  fs.mkdir(dir, function() {
    // Ignore errors

    const done = once(ondone);
    var waiting = files.length;
    files.forEach(function(file) {
      fs.writeFile(file.path, file.content, function(err) {
        if (err)
          return done(err);

        if (onprogress)
          onprogress(file.path);

        if (--waiting === 0)
          done();
      });
    });
  });
}