forked from motemen/dts-google-apps-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.js
More file actions
113 lines (94 loc) · 3.06 KB
/
Copy pathgen.js
File metadata and controls
113 lines (94 loc) · 3.06 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
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
#!/usr/bin/env node --harmony
var fs = require('fs');
var outFiles = {};
var input = '';
process.stdin.on('data', (buf) => input += buf.toString());
process.stdin.on('end', () => {
var data = JSON.parse(input);
function mkDoc (doc) {
var doc = [];
doc.push('/**');
decl.doc.replace(/( *\n){3,}/g, '\n\n').replace(/\s+$/, '').split(/\n/).forEach((line) => doc.push(' * ' + line));
doc.push(' */');
return doc;
}
function indent (s) {
return s.replace(/^./, ' $&');
}
for (var cat in data.categories) {
var result = [];
var catName = data.categories[cat].name.replace(/\W/g, '_');
var decls = data.categories[cat].decls;
var exports = {};
function mkTypedName (o) {
var name = o.name,
typeName = o.type.name,
typeCat = o.type.category;
if (typeCat && typeCat !== cat) {
typeName = data.categories[typeCat].name.replace(/\W/g, '_') + '.' + typeName;
if (references.indexOf(typeCat) === -1) {
references.push(typeCat);
}
}
if (/^(.+)\.\.\.$/.test(typeName)) {
typeName = RegExp.$1 + '[]';
name = '...' + o.name;
}
return name + ': ' + typeName;
}
var references = [ 'types' ];
result.push(
'declare module GoogleAppsScript {',
' export module ' + catName + ' {'
);
for (var name in decls) {
var decl = decls[name];
if (!decl) continue;
var lines = mkDoc(decl.doc);
if (decl.kind === 'enum') {
lines.push('export enum ' + decl.name + ' { ' + decl.properties.map((p) => p.name).join(', ') + ' }');
lines.push('');
} else {
lines.push('export interface ' + decl.name + ' {');
lines.push.apply(lines, decl.properties.map(mkTypedName).map(indent))
lines.push.apply(lines,
decl.methods.map((method) =>
mkTypedName({
name: method.name + '(' +
method.params.map(mkTypedName).join(', ')
.replace(/(\bsql:.*)\bsql:/g, '$1sql_:') + // ad-hoc fix for same-named arguments in jdbc
')',
type: method.returnType
}) + ';'
).map(indent)
)
lines.push('}');
lines.push('');
}
if (data.services[decl.url]) {
exports[name] = true;
}
result = result.concat(lines.map(indent).map(indent));
}
result.push(
' }',
'}',
''
);
for (var name in exports) {
var line = 'declare var ' + name + ': GoogleAppsScript.' + catName + '.' + name + ';'
if (name === 'MimeType') {
result.push('// conflicts with MimeType in lib.d.ts');
result.push('// ' + line);
} else {
result.push(line);
}
}
result = references.map((ref) => '/// <reference path="google-apps-script.' + ref + '.d.ts" />')
.concat('', result);
var file = 'google-apps-script/google-apps-script.' + cat + '.d.ts';
var f = fs.openSync(file, 'w');
fs.writeSync(f, result.join('\n'));
console.error('Wrote to ' + file);
}
});