build.js
2.02 KB
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
'use strict';
var fs = require('fs');
var rimraf = require('rimraf');
var acorn = require('acorn');
var walk = require('acorn/dist/walk');
var ids = [];
var names = {};
function getIdFor(name) {
if (name in names) return names[name];
var id;
do {
id = '_' + Math.floor(Math.random() * 100);
} while (ids.indexOf(id) !== -1)
ids.push(id);
names[name] = id;
return id;
}
function fixup(src) {
var ast = acorn.parse(src);
src = src.split('');
walk.simple(ast, {
MemberExpression: function (node) {
if (node.computed) return;
if (node.property.type !== 'Identifier') return;
if (node.property.name[0] !== '_') return;
replace(node.property, getIdFor(node.property.name));
}
});
function source(node) {
return src.slice(node.start, node.end).join('');
}
function replace(node, str) {
for (var i = node.start; i < node.end; i++) {
src[i] = '';
}
src[node.start] = str;
}
return src.join('');
}
rimraf.sync(__dirname + '/lib/');
fs.mkdirSync(__dirname + '/lib/');
fs.readdirSync(__dirname + '/src').forEach(function (filename) {
var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
var out = fixup(src);
fs.writeFileSync(__dirname + '/lib/' + filename, out);
});
rimraf.sync(__dirname + '/domains/');
fs.mkdirSync(__dirname + '/domains/');
fs.readdirSync(__dirname + '/src').forEach(function (filename) {
var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
var out = fixup(src);
out = out.replace(/require\(\'asap\/raw\'\)/g, "require('asap')");
fs.writeFileSync(__dirname + '/domains/' + filename, out);
});
rimraf.sync(__dirname + '/setimmediate/');
fs.mkdirSync(__dirname + '/setimmediate/');
fs.readdirSync(__dirname + '/src').forEach(function (filename) {
var src = fs.readFileSync(__dirname + '/src/' + filename, 'utf8');
var out = fixup(src);
out = out.replace(/var asap \= require\(\'([a-z\/]+)\'\);/g, '');
out = out.replace(/asap/g, "setImmediate");
fs.writeFileSync(__dirname + '/setimmediate/' + filename, out);
});