]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/space-pen/node_modules/grim/node_modules/coffeestack/index.js
5ad299b002e6de090e32c2f14b714e9c9067e157
2 var CoffeeScript
, CoffeeScriptVersion
, SourceMapConsumer
, cachePath
, compileSourceMap
, convertLine
, convertStackTrace
, crypto
, fs
, getCachePath
, getCachedSourceMap
, getSourceMapPosition
, loadCoffeeScript
, path
, writeSourceMapToCache
;
4 crypto
= require('crypto');
6 fs
= require('fs-plus');
8 path
= require('path');
10 CoffeeScriptVersion
= null;
14 SourceMapConsumer
= null;
18 getCachePath = function(code
) {
23 digest
= crypto
.createHash('sha1').update(code
, 'utf8').digest('hex');
24 if (CoffeeScriptVersion
== null) {
25 CoffeeScriptVersion
= require('coffee-script/package.json').version
;
27 return path
.join(cachePath
, CoffeeScriptVersion
, "" + digest
+ ".json");
30 getCachedSourceMap = function(codeCachePath
) {
31 if (fs
.isFileSync(codeCachePath
)) {
33 return fs
.readFileSync(codeCachePath
, 'utf8');
38 writeSourceMapToCache = function(codeCachePath
, sourceMap
) {
41 fs
.writeFileSync(codeCachePath
, sourceMap
);
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
) {
54 return coffeePrepareStackTrace(error
, stack
);
64 compileSourceMap = function(code
, filePath
, codeCachePath
) {
66 if (CoffeeScript
== null) {
67 CoffeeScript
= loadCoffeeScript();
69 v3SourceMap
= CoffeeScript
.compile(code
, {
73 writeSourceMapToCache(codeCachePath
, v3SourceMap
);
77 getSourceMapPosition = function(sourceMapContents
, line
, column
) {
79 if (SourceMapConsumer
== null) {
80 SourceMapConsumer
= require('source-map').SourceMapConsumer
;
82 sourceMap
= new SourceMapConsumer(sourceMapContents
);
83 return sourceMap
.originalPositionFor({
89 convertLine = function(filePath
, line
, column
, sourceMaps
) {
90 var code
, codeCachePath
, position
, source
, sourceMapContents
, sourceMapPath
;
91 if (sourceMaps
== null) {
95 if (!(sourceMapContents
= sourceMaps
[filePath
])) {
96 if (path
.extname(filePath
) === '.js') {
97 sourceMapPath
= "" + filePath
+ ".map";
98 sourceMapContents
= fs
.readFileSync(sourceMapPath
, 'utf8');
100 code
= fs
.readFileSync(filePath
, 'utf8');
101 codeCachePath
= getCachePath(code
);
102 sourceMapContents
= getCachedSourceMap(codeCachePath
);
103 if (sourceMapContents
== null) {
104 sourceMapContents
= compileSourceMap(code
, filePath
, codeCachePath
);
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
);
119 column: position
.column
,
128 convertStackTrace = function(stackTrace
, sourceMaps
) {
129 var atLinePattern
, column
, convertedLines
, filePath
, line
, mappedLine
, match
, stackTraceLine
, _i
, _len
, _ref
;
130 if (sourceMaps
== null) {
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
)) {
145 if (path
.extname(filePath
) === '.js') {
146 mappedLine
= convertLine(filePath
, line
, column
, sourceMaps
);
148 if (mappedLine
!= null) {
149 convertedLines
.push("" + match
[1] + "(" + mappedLine
.source
+ ":" + mappedLine
.line
+ ":" + mappedLine
.column
+ ")");
151 convertedLines
.push(stackTraceLine
);
154 convertedLines
.push(stackTraceLine
);
157 return convertedLines
.join('\n');
160 exports
.convertLine
= convertLine
;
162 exports
.convertStackTrace
= convertStackTrace
;
164 exports
.setCacheDirectory = function(newCachePath
) {
165 return cachePath
= newCachePath
;
168 exports
.getCacheDirectory = function() {