-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
update-ts.js
67 lines (54 loc) · 2.26 KB
/
update-ts.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
'use strict'
/*jshint asi: true */
var path = require('path');
var fs = require('fs');
var fixRequires = require('./fix-requires');
require('shelljs/global');
var braceroot = path.join(__dirname, '..');
var buildroot = path.join(__dirname, 'DefinitelyTyped');
+function cloneFreshAndRemoveUnneeded() {
rm('-rf', buildroot)
exec('git clone git:https://github.com/DefinitelyTyped/DefinitelyTyped.git ' + buildroot);
ls(path.join(buildroot, 'types')).filter(function (name) { return name !== 'ace'; })
.forEach(function (name) { rm('-rf', path.join(buildroot, name)) })
// move ace files to root after we cleaned it since that is all we need
mv(path.join(buildroot, 'types', 'ace/*'), buildroot)
rm('-rf', path.join(buildroot, 'types', 'ace'));
}()
+function requires() {
var file = path.join(buildroot, 'index.d.ts')
var src = fs.readFileSync(file, 'utf-8');
var fixed = fixRequires(src);
fs.writeFileSync(file, fixed, 'utf-8');
}()
+function modularize() {
var tssrc = fs.readFileSync(path.join(buildroot, 'index.d.ts'), 'utf-8');
// Make these definitions a module by exporting the namespace
var src = tssrc.replace('declare var ace: AceAjax.Ace;', 'export = AceAjax;');
// We can't export an interface from within the namespace, so we remove the
// Ace interface and export the contained functions directly.
// This is easier done line-by-line.
var srclines = src.split('\n');
var in_body = false;
for (var i = 0; i < srclines.length; i++) {
if (!in_body) {
if (srclines[i].includes("export interface Ace")) {
// Once we hit the Ace interface, we'll start rewriting declarations
srclines[i] = "" // Remove the interface export
in_body = true;
}
} else {
if (/\S+\(.+\):.*;/.test(srclines[i])) {
// If the line is a function declaration, export it
srclines[i] = "export function " + srclines[i];
} else if (/\s+\}$/.test(srclines[i])) {
// If we hit a closing brace, we assume we're done the interface.
// This could become wrong in the future, but for now it's right.
srclines[i] = ""
break;
}
}
}
src = srclines.join("\n");
fs.writeFileSync(path.join(braceroot, 'index.d.ts'), src, 'utf-8');
}()