]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/space-pen/node_modules/grim/node_modules/coffeestack/index.js
5ad299b002e6de090e32c2f14b714e9c9067e157
[rbdr/dotfiles] / atom / packages / ex-mode / node_modules / space-pen / node_modules / grim / node_modules / coffeestack / index.js
1 (function() {
2 var CoffeeScript, CoffeeScriptVersion, SourceMapConsumer, cachePath, compileSourceMap, convertLine, convertStackTrace, crypto, fs, getCachePath, getCachedSourceMap, getSourceMapPosition, loadCoffeeScript, path, writeSourceMapToCache;
3
4 crypto = require('crypto');
5
6 fs = require('fs-plus');
7
8 path = require('path');
9
10 CoffeeScriptVersion = null;
11
12 CoffeeScript = null;
13
14 SourceMapConsumer = null;
15
16 cachePath = null;
17
18 getCachePath = function(code) {
19 var digest;
20 if (!cachePath) {
21 return;
22 }
23 digest = crypto.createHash('sha1').update(code, 'utf8').digest('hex');
24 if (CoffeeScriptVersion == null) {
25 CoffeeScriptVersion = require('coffee-script/package.json').version;
26 }
27 return path.join(cachePath, CoffeeScriptVersion, "" + digest + ".json");
28 };
29
30 getCachedSourceMap = function(codeCachePath) {
31 if (fs.isFileSync(codeCachePath)) {
32 try {
33 return fs.readFileSync(codeCachePath, 'utf8');
34 } catch (_error) {}
35 }
36 };
37
38 writeSourceMapToCache = function(codeCachePath, sourceMap) {
39 if (codeCachePath) {
40 try {
41 fs.writeFileSync(codeCachePath, sourceMap);
42 } catch (_error) {}
43 }
44 };
45
46 loadCoffeeScript = function() {
47 var coffee, coffeePrepareStackTrace;
48 coffee = require('coffee-script');
49 coffeePrepareStackTrace = Error.prepareStackTrace;
50 if (coffeePrepareStackTrace != null) {
51 Error.prepareStackTrace = function(error, stack) {
52 var coffeeError;
53 try {
54 return coffeePrepareStackTrace(error, stack);
55 } catch (_error) {
56 coffeeError = _error;
57 return stack;
58 }
59 };
60 }
61 return coffee;
62 };
63
64 compileSourceMap = function(code, filePath, codeCachePath) {
65 var v3SourceMap;
66 if (CoffeeScript == null) {
67 CoffeeScript = loadCoffeeScript();
68 }
69 v3SourceMap = CoffeeScript.compile(code, {
70 sourceMap: true,
71 filename: filePath
72 }).v3SourceMap;
73 writeSourceMapToCache(codeCachePath, v3SourceMap);
74 return v3SourceMap;
75 };
76
77 getSourceMapPosition = function(sourceMapContents, line, column) {
78 var sourceMap;
79 if (SourceMapConsumer == null) {
80 SourceMapConsumer = require('source-map').SourceMapConsumer;
81 }
82 sourceMap = new SourceMapConsumer(sourceMapContents);
83 return sourceMap.originalPositionFor({
84 line: line,
85 column: column
86 });
87 };
88
89 convertLine = function(filePath, line, column, sourceMaps) {
90 var code, codeCachePath, position, source, sourceMapContents, sourceMapPath;
91 if (sourceMaps == null) {
92 sourceMaps = {};
93 }
94 try {
95 if (!(sourceMapContents = sourceMaps[filePath])) {
96 if (path.extname(filePath) === '.js') {
97 sourceMapPath = "" + filePath + ".map";
98 sourceMapContents = fs.readFileSync(sourceMapPath, 'utf8');
99 } else {
100 code = fs.readFileSync(filePath, 'utf8');
101 codeCachePath = getCachePath(code);
102 sourceMapContents = getCachedSourceMap(codeCachePath);
103 if (sourceMapContents == null) {
104 sourceMapContents = compileSourceMap(code, filePath, codeCachePath);
105 }
106 }
107 }
108 if (sourceMapContents) {
109 sourceMaps[filePath] = sourceMapContents;
110 position = getSourceMapPosition(sourceMapContents, line, column);
111 if ((position.line != null) && (position.column != null)) {
112 if (position.source && position.source !== '.') {
113 source = path.resolve(filePath, '..', position.source);
114 } else {
115 source = filePath;
116 }
117 return {
118 line: position.line,
119 column: position.column,
120 source: source
121 };
122 }
123 }
124 } catch (_error) {}
125 return null;
126 };
127
128 convertStackTrace = function(stackTrace, sourceMaps) {
129 var atLinePattern, column, convertedLines, filePath, line, mappedLine, match, stackTraceLine, _i, _len, _ref;
130 if (sourceMaps == null) {
131 sourceMaps = {};
132 }
133 if (!stackTrace) {
134 return stackTrace;
135 }
136 convertedLines = [];
137 atLinePattern = /^(\s+at .* )\((.*):(\d+):(\d+)\)/;
138 _ref = stackTrace.split('\n');
139 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
140 stackTraceLine = _ref[_i];
141 if (match = atLinePattern.exec(stackTraceLine)) {
142 filePath = match[2];
143 line = match[3];
144 column = match[4];
145 if (path.extname(filePath) === '.js') {
146 mappedLine = convertLine(filePath, line, column, sourceMaps);
147 }
148 if (mappedLine != null) {
149 convertedLines.push("" + match[1] + "(" + mappedLine.source + ":" + mappedLine.line + ":" + mappedLine.column + ")");
150 } else {
151 convertedLines.push(stackTraceLine);
152 }
153 } else {
154 convertedLines.push(stackTraceLine);
155 }
156 }
157 return convertedLines.join('\n');
158 };
159
160 exports.convertLine = convertLine;
161
162 exports.convertStackTrace = convertStackTrace;
163
164 exports.setCacheDirectory = function(newCachePath) {
165 return cachePath = newCachePath;
166 };
167
168 exports.getCacheDirectory = function() {
169 return cachePath;
170 };
171
172 }).call(this);