forked from motemen/dts-google-apps-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.js
144 lines (118 loc) · 4.06 KB
/
gen.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
#!/usr/bin/env node --harmony
var fs = require('fs');
var header = fs.readFileSync('HEADER', { encoding: 'utf-8' })
.replace(/{date}/, function () {
var date = new Date();
return date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).substr(-2) + '-' + ('0' + date.getDate()).substr(-2)
});
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 lines = [];
lines.push('/**');
doc.replace(/( *\n){3,}/g, '\n\n').replace(/\s+$/, '').split(/\n/).forEach((line) => lines.push(' * ' + line));
lines.push(' */');
return lines;
}
function indent (s) {
return s.replace(/^./, ' $&');
}
Object.keys(data.categories).sort().forEach(function (cat) {
var result = [];
var catName = data.categories[cat].name.replace(/\W/g, '_');
var decls = data.categories[cat].decls;
var exports = {};
function mkTypedName (o, isField) {
var name = o.name,
typeName = o.type.name,
typeCat = o.type.category,
isType = false;
if (isField === true) {
if (data.categories[typeCat] && data.categories[typeCat].decls[typeName]) {
if (data.categories[typeCat].decls[typeName].kind === 'enum') {
isType = true;
}
}
}
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;
}
if (typeName === 'String' || typeName === 'Boolean') {
typeName = typeName.toLowerCase();
}
return name + ': ' + (isType ? 'typeof ' : '') + typeName;
}
var references = [ 'types' ];
result.push(
'declare namespace GoogleAppsScript {',
' export module ' + catName + ' {'
);
Object.keys(decls).sort().forEach(function (name) {
var decl = decls[name];
if (!decl) return;
var lines = mkDoc(decl.doc);
var names = name.split(/\./);
name = names.pop();
names.forEach(function (ns) {
lines.push('namespace ' + ns + ' {');
});
if (decl.kind === 'enum') {
lines.push('export enum ' + name + ' { ' + decl.properties.map((p) => p.name).join(', ') + ' }');
} else {
lines.push('export interface ' + name + ' {');
lines.push.apply(lines, decl.properties.map(p => mkTypedName(p, true) + ';').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('}');
}
names.forEach(function (ns) {
lines.push('}');
});
lines.push('');
if (data.services[decl.url]) {
exports[name] = true;
}
result = result.concat(lines.map(indent).map(indent));
});
result.push(
' }',
'}',
''
);
Object.keys(exports).sort().forEach(function (name) {
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 = [ header ]
.concat(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').replace(/ +$/mg, '') + '\n');
console.error('Wrote to ' + file);
});
});