This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
webstorm_traceur.js
59 lines (47 loc) · 1.89 KB
/
webstorm_traceur.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
var inputFilename = process.argv[2];
var outputFilename = process.argv[3] || inputFilename.replace(/\.ats$/, '.js');
var path = require('path');
var sourceRoot = path.relative(path.dirname(outputFilename), '');
var TraceurNodeCompiler = require('traceur/src/node/NodeCompiler').NodeCompiler;
var options = require('./config.json').traceur;
var compiler = new TraceurNodeCompiler(options);
// Increment the lock to delay Karma unit tests until all watchers are finished.
var LOCK_FILE = '.atscriptwatcher';
var fs = require('fs');
if (fs.existsSync(LOCK_FILE)) {
var lockCount = parseInt(fs.readFileSync(LOCK_FILE).toString(), 10);
lockCount++;
fs.writeFileSync(LOCK_FILE, lockCount.toString());
} else {
fs.writeFileSync(LOCK_FILE, '1');
}
// TODO(vojta): Fix this in Traceur instead.
// Traceur generates source map file in the CWD.
// This hacks Traceur to generate source map file in the output file directory, respecting the `sourceRoot` option.
var writeFile = require('traceur/src/node/file-util').writeFile;
compiler.writeTreeToFile = function(tree, filename) {
filename = this.normalize(filename);
// This is changed.
// Pass sourceRoot to `this.write`. Traceur does not pass it through.
var compiledCode = this.write(tree, filename, sourceRoot);
if (this.options_.sourceMaps === 'file') {
var sourcemap = this.getSourceMap();
if (sourcemap) {
// This is changed.
// Generate the source map in the same folder as the output JS file.
writeFile(filename.replace(/\.js$/, '.map'), sourcemap);
}
}
writeFile(filename, compiledCode);
};
compiler.compileSingleFile(inputFilename, outputFilename, function(err) {
console.error(err);
});
// Release/decrement the lock.
var lockCount = parseInt(fs.readFileSync(LOCK_FILE).toString(), 10);
lockCount--;
if (lockCount === 0) {
fs.unlinkSync(LOCK_FILE);
} else {
fs.writeFileSync(LOCK_FILE, lockCount.toString());
}