-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
367 lines (353 loc) · 9.98 KB
/
build.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
'use strict';
var fs = require('fs');
var rimraf = require('rimraf').sync;
var mkdirp = require('mkdirp').sync;
var EVENT_EMITTER_METHODS = [
'addListener',
'emit',
'getMaxListeners',
'listenerCount',
'listeners',
'on',
'once',
'removeAllListeners',
'removeListener',
'setMaxListeners',
];
var READABLE_STREAM_METHODS = [
'isPaused',
'pause',
'pipe',
'read',
'resume',
'setEncoding',
'unpipe',
'unshift',
];
var PROMISE_METHODS = ['catch', 'done', 'finally', 'nodeify', 'then'];
rimraf(__dirname + '/lib');
mkdirp(__dirname + '/lib');
fs.writeFileSync(
__dirname + '/lib/README.md',
'**WARNING:** This folder is autogenerated by `build.js` from the files in `/schema`',
);
const classes = [];
fs.readdirSync(__dirname + '/schema').forEach(function(type) {
var schema = require('./schema/' + type);
var proxy = [];
var typedef = [];
var both = {
push(str) {
proxy.push(str);
typedef.push(str);
},
};
both.push('/**');
both.push(
' * WARNING: This file is autogenerated by `build.js` from in `/schema/`' +
type,
);
both.push(' */');
both.push('');
proxy.push('var Promise = require("promise");');
typedef.push('import Promise = require("promise");');
if (schema.methods) {
Object.keys(schema.methods)
.filter(function(method) {
return schema.methods[method].proxy;
})
.map(function(method) {
return schema.methods[method].returns;
})
.filter(function(name, index, arr) {
return arr.indexOf(name) === index;
})
.sort()
.forEach(function(name) {
proxy.push(
'var ' + name + ' = require("./' + name.toLowerCase() + '");',
);
typedef.push(`import ${name} from './${name.toLowerCase()}';`);
});
}
both.push('');
var className = type[0].toUpperCase() + type.substr(1).replace(/\.js$/, '');
classes.push({className, type});
proxy.push('module.exports = ' + className + ';');
proxy.push('function ' + className + '(_parent) {');
proxy.push(' this._parent = _parent;');
typedef.push(`export default class ${className} {`);
var defaultValueForLazyPromise = Object.keys(schema.methods).filter(function(
method,
) {
return schema.methods[method].defaultValueForLazyPromise;
});
if (defaultValueForLazyPromise.length > 1) {
throw new Error(
'You cannot have multiple default methods for lazy promise',
);
}
if (defaultValueForLazyPromise.length === 1) {
defaultValueForLazyPromise = defaultValueForLazyPromise[0];
} else {
defaultValueForLazyPromise = null;
}
if (defaultValueForLazyPromise) {
proxy.push(' this._promise = null;');
}
if (schema.properties) {
schema.properties.forEach(function(property) {
if (process.argv.indexOf('--test') !== -1) {
proxy.push(
' if (typeof _parent.' +
property.name +
' === "undefined") throw new Error("Missing required property ' +
property.name +
'");',
);
}
proxy.push(
' this.' + property.name + ' = _parent.' + property.name + ';',
);
});
}
if (schema.methods && process.argv.indexOf('--test') !== -1) {
Object.keys(schema.methods).forEach(function(method) {
proxy.push(
' if (typeof _parent.' +
method +
' !== "function") throw new Error("Missing required method ' +
method +
'");',
);
});
}
if (schema.methods) {
Object.keys(schema.methods).forEach(function(name) {
if (name[0] === '_') {
return;
}
const method = schema.methods[name];
const args = method.args
.map(arg => {
return `${arg.name}${'default' in arg ? '?' : ''}: ${arg.type ||
'any'}`;
})
.join(', ');
if (method.desc) {
typedef.push(` /**`);
typedef.push(` * ${method.desc}`);
typedef.push(` */`);
}
typedef.push(` ${name}(${args}): ${method.returns}`);
});
}
proxy.push('}');
proxy.push('');
if (schema.methods) {
Object.keys(schema.methods).forEach(function(method) {
var args = schema.methods[method].args.slice();
var rest;
if (args.length && args[args.length - 1].rest) {
rest = args.pop();
}
proxy.push(
className +
'.prototype.' +
method +
' = function ' +
method +
'(' +
args
.map(function(arg) {
return arg.name;
})
.join(', ') +
') {',
);
if (schema.methods[method].proxy) {
if (rest) {
proxy.push(
' return new ' +
schema.methods[method].returns +
'(this._parent.' +
method +
'.apply(this._parent, arguments));',
);
} else {
proxy.push(
' return new ' +
schema.methods[method].returns +
'(this._parent.' +
method +
'(' +
args.map(argValue).join(', ') +
'));',
);
}
} else if (/^Promise/.test(schema.methods[method].returns)) {
proxy.push(' var _parent = this._parent');
if (rest) {
proxy.push(' var _args = [];');
proxy.push(' for (var i = 0; i < arguments.length; i++) {');
proxy.push(' _args.push(arguments[i]);');
proxy.push(' }');
}
if (defaultValueForLazyPromise === method) {
proxy.push(' if (this._promise) return this._promise;');
proxy.push(
' return this._promise = new Promise(function (resolve, reject) {',
);
} else {
proxy.push(' return new Promise(function (resolve, reject) {');
}
var cb = 'function (err, res) { err ? reject(err) : resolve(res) }';
if (rest) {
proxy.push(' _args.push(' + cb + ');');
proxy.push(' _parent.' + method + '.apply(_parent, _args);');
} else {
proxy.push(
' _parent.' +
method +
'(' +
args
.map(argValue)
.concat(cb)
.join(', ') +
');',
);
}
proxy.push(' });');
} else {
if (rest) {
throw new Error('Unexpected rest args for sync function');
}
if (schema.methods[method].returns === 'this') {
proxy.push(
' this._parent.' +
method +
'(' +
args.map(argValue).join(', ') +
');',
);
proxy.push(' return this;');
} else {
proxy.push(
' return this._parent.' +
method +
'(' +
args.map(argValue).join(', ') +
');',
);
}
}
proxy.push('};');
if (schema.methods[method].aliases) {
schema.methods[method].aliases.forEach(function(alias) {
proxy.push(
className +
'.prototype.' +
alias +
' = ' +
className +
'.prototype.' +
method +
';',
);
});
}
proxy.push('');
});
}
if (schema.events || schema.readableStream) {
proxy.push('// Event Emitter');
proxy.push('');
EVENT_EMITTER_METHODS.forEach(function(method) {
proxy.push(
className + '.prototype.' + method + ' = function ' + method + '() {',
);
proxy.push(
' return this._parent.' + method + '.apply(this._parent, arguments);',
);
proxy.push('};');
proxy.push('');
});
}
if (schema.readableStream) {
proxy.push('// Readable Stream');
proxy.push('');
READABLE_STREAM_METHODS.forEach(function(method) {
proxy.push(
className + '.prototype.' + method + ' = function ' + method + '() {',
);
proxy.push(
' return this._parent.' + method + '.apply(this._parent, arguments);',
);
proxy.push('};');
proxy.push('');
});
}
if (defaultValueForLazyPromise) {
proxy.push('// Promise Methods');
proxy.push('');
PROMISE_METHODS.forEach(function(method) {
proxy.push(className + '.prototype.' + method + ' = function () {');
proxy.push(' var _promise = this.' + defaultValueForLazyPromise + '();');
proxy.push(
' return _promise.' + method + '.apply(_promise, arguments);',
);
proxy.push('};');
proxy.push('');
});
}
typedef.push('}');
fs.writeFileSync(__dirname + '/lib/' + type, proxy.join('\n'));
fs.writeFileSync(
__dirname + '/lib/' + type.replace(/\.js$/, '.d.ts'),
typedef.join('\n'),
);
});
function argValue(arg) {
return 'default' in arg
? arg.name +
' === undefined ? ' +
JSON.stringify(arg.default) +
' : ' +
arg.name
: arg.name;
}
const indexTypeDef = [];
const exportedVariables = [
'Binary',
'Code',
'Double',
'Long',
'MinKey',
'MaxKey',
'ObjectID',
'Timestamp',
];
indexTypeDef.push('/**');
indexTypeDef.push(' * WARNING: This file is autogenerated by `build.js`');
indexTypeDef.push(' */');
indexTypeDef.push('');
indexTypeDef.push(`import {${exportedVariables.join(', ')}} from 'mongodb';`);
classes.forEach(c => {
indexTypeDef.push(
`import ${c.className} from './lib/${c.type.replace(/\.js$/, '')}';`,
);
});
indexTypeDef.push(``);
indexTypeDef.push(`export {${classes.map(c => c.className).join(', ')}};`);
indexTypeDef.push(``);
indexTypeDef.push(`/**`);
indexTypeDef.push(` * Connect to a database`);
indexTypeDef.push(` */`);
indexTypeDef.push(
`declare function connect(connString: string, collectionNames?: Array<string>, options?: Object): Database;`,
);
indexTypeDef.push(`export default connect;`);
indexTypeDef.push(``);
indexTypeDef.push(`export {${exportedVariables.join(', ')}};`);
indexTypeDef.push(``);
fs.writeFileSync(__dirname + '/index.d.ts', indexTypeDef.join('\n'));