forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtypescript.js
60 lines (50 loc) · 1.42 KB
/
typescript.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
'use strict';
const _ = require('underscore-plus');
const crypto = require('crypto');
const path = require('path');
const defaultOptions = {
target: 1,
module: 'commonjs',
sourceMap: true
};
let TypeScriptSimple = null;
let typescriptVersionDir = null;
exports.shouldCompile = function() {
return true;
};
exports.getCachePath = function(sourceCode) {
if (typescriptVersionDir == null) {
const version = require('typescript-simple/package.json').version;
typescriptVersionDir = path.join(
'ts',
createVersionAndOptionsDigest(version, defaultOptions)
);
}
return path.join(
typescriptVersionDir,
crypto
.createHash('sha1')
.update(sourceCode, 'utf8')
.digest('hex') + '.js'
);
};
exports.compile = function(sourceCode, filePath) {
if (!TypeScriptSimple) {
TypeScriptSimple = require('typescript-simple').TypeScriptSimple;
}
if (process.platform === 'win32') {
filePath = 'file:///' + path.resolve(filePath).replace(/\\/g, '/');
}
const options = _.defaults({ filename: filePath }, defaultOptions);
return new TypeScriptSimple(options, false).compile(sourceCode, filePath);
};
function createVersionAndOptionsDigest(version, options) {
return crypto
.createHash('sha1')
.update('typescript', 'utf8')
.update('\0', 'utf8')
.update(version, 'utf8')
.update('\0', 'utf8')
.update(JSON.stringify(options), 'utf8')
.digest('hex');
}