forked from grmlin/meteor-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.old.js
180 lines (154 loc) · 5.1 KB
/
package.old.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
/*global Package*/
Package.describe({
summary: "TypeScript, a typed superset of Javascript that compiles to plain Javascript"
});
Npm.depends({
"exec-sync": "0.1.5",
"mkdirp": "0.3.5"
});
var path = Npm.require('path');
var fs = Npm.require('fs');
var refs = [];
var dirs = [];
function getReferences(dir) {
var res = "";
refs.forEach(function (entry) {
res += '///<reference path="' + path.relative(dir, entry) + '" />\n';
});
return res;
}
Package.register_extension("ts", function (bundle, source_path, serve_path, where) {
// console.log("Src: " + source_path);
var isRef = source_path.substring(source_path.length - 5) === '.d.ts';
var mkdirp = Npm.require('mkdirp');
// cache check
//console.log(serve_path);
var cachePath = '.meteor/cache' + serve_path;
var cacheDir = path.dirname(cachePath);
var baseName = path.basename(source_path, '.ts');
var changeTime = fs.statSync(source_path).mtime;
var jsPath;
if (serve_path.substring(0, 10) === '/packages/') {
jsPath = cacheDir + '/' + path.dirname(serve_path.substring(10)) + '/' + baseName + '.js';
} else {
var rootPath = path.normalize(source_path.substring(0, source_path.length - serve_path.length) + '/..');
jsPath = cacheDir + '/' + path.dirname(path.relative(rootPath, source_path)) + '/' + baseName + '.js';
}
var mapPath = jsPath + '.map';
// references
var dir = path.dirname(source_path);
if (dirs.indexOf(dir) == -1) {
dirs.push(dir);
}
if (refs.indexOf(source_path) == -1) {
refs.push(source_path);
dirs.forEach(function (dir) {
var refData = getReferences(dir);
// console.log('path: ' + dir + "/.ref.d.ts");
// console.log('content:\n' + refData);
fs.writeFileSync(dir + "/.ref.d.ts", refData);
// try {
// fs.unlinkSync(dir + "/.ref.d.ts");
// } catch(err) {
//
// }
});
}
// dont compile d.ts files
if (isRef) {
return;
}
if (!fs.existsSync(cachePath)) {
mkdirp.sync(cacheDir);
}
if (!fs.existsSync(cachePath) || changeTime.getTime() > fs.statSync(cachePath).mtime.getTime()) {
var execSync = Npm.require('exec-sync');
var ERROR = "\nTypeScript compilation failed!\n";
ERROR = ERROR + (new Array(ERROR.length - 1).join("-")) + "\n";
// XXX Use other npm packages. Seen in the handlebars package ;)
var compileCommand = 'tsc --target ES5 --sourcemap --out ' + cacheDir + ' ' + source_path; // add client,server module type switch?
// var compileCommand = 'tsc --nolib --sourcemap --out ' + cacheDir + " " + source_path; // add client,server module type switch?
var result = null;
// Compile the TypeScript file with the TypeScript command line compiler.
// Until the TypeScript module provides a public API there is no reliable way around it without changing the
// TypeScript sources.
try {
result = execSync(compileCommand);
//console.log(compileCommand);
//console.log(result);
} catch (e) {
var lines = e.message.split('\n');
var errors = [];
for (var i = 0; i < lines.length; i++) {
if (
// lines[i].trim() &&
// !/The property '__super__' does not exist on value of type/.test(lines[i]) &&
// lines[i].substr(-36) !== 'Base type must be interface or class' &&
// lines[i].substr(-30) !== 'not exist in the current scope' &&
// lines[i].substr(-24) !== 'lacks an implementation.'
lines[i].indexOf('error TS2095') == -1
) {
errors.push(lines[i]);
}
}
if (errors.length > 0) {
bundle.error(ERROR + errors.join('\n'));
return;
}
else
result = true;
}
if (fs.existsSync(cacheDir + '/' + baseName + '.js')) {
jsPath = cacheDir + '/' + baseName + '.js';
mapPath = jsPath + '.map';
}
if (fs.existsSync(jsPath)) {
if (result !== null) {
var sourceBuffer = new Buffer(fs.readFileSync(source_path));
var compiledBuffer = new Buffer(
fs.readFileSync(jsPath).toString().replace(
/\/\/@ sourceMappingURL=[0-9a-zA-Z_.-]+/,
'//@ sourceMappingURL=' + serve_path + '.map?' + changeTime.getTime()
)
);
var mapBuffer = new Buffer(
fs.readFileSync(mapPath).toString().replace(
/"sources":\["[0-9a-zA-Z-\/\.-]+"]/,
'"sources":["' + path.dirname(serve_path) + '/' + path.basename(serve_path) + '?' + changeTime.getTime() + '"]'
)
);
fs.writeFileSync(cachePath, sourceBuffer);
fs.writeFileSync(cachePath + '.js', compiledBuffer);
fs.writeFileSync(cachePath + '.map', mapBuffer);
}
// Delete the created file afterwards and add the content to the bundle
fs.unlinkSync(jsPath);
fs.unlinkSync(mapPath);
}
else {
bundle.error(ERROR + 'file was not created: \n' + jsPath + '\n' + serve_path + '\n' + cacheDir + '\n' + source_path);
return;
}
}
// source
bundle.add_resource({
type: "static",
path: serve_path,
data: new Buffer(fs.readFileSync(cachePath)),
where: where
});
// map
bundle.add_resource({
type: "static",
path: serve_path + '.map',
data: new Buffer(fs.readFileSync(cachePath + '.map')),
where: where
});
// compiled
bundle.add_resource({
type: "js",
path: serve_path + '.js',
data: new Buffer(fs.readFileSync(cachePath + '.js')),
where: where
});
});