forked from pimatic/pimatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffee-cache.js
176 lines (153 loc) · 5.55 KB
/
coffee-cache.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
/*
node-coffee-cache
from: https://github.com/FogCreek/node-coffee-cache
The MIT License
Copyright (c) Fog Creek Software Inc. 2013
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var fs = require('fs.extra');
var path = require('path');
var colors = require('colors');
if(process.env['PIMATIC_DAEMONIZED']) {
colors.mode = 'none'
}
// We don't want to include our own version of CoffeeScript since we don't know
// what version the parent relies on
var coffee;
try {
coffee = require('coffee-script');
// CoffeeScript 1.7 support
if (typeof coffee.register === 'function') {
coffee.register();
}
} catch (e) {
// No coffee-script installed in the project; warn them and stop
process.stderr.write("coffee-cache: No coffee-script package found.\n");
return;
}
// Directory to store compiled source
var cacheDir = process.env['COFFEE_CACHE_DIR'] || '.js';
function getCachPaths(filename) {
// First, convert the filename to something more digestible and use our cache
// folder
var rootDir = "";
var insideCache = "";
var match = filename.match(/^(.*\/node_modules\/[^\/]+)\/(.*)$/);
if(match) {
rootDir = match[1];
insideCache = match[2];
} else {
rootDir = __dirname;
insideCache = path.relative(rootDir, filename);
}
var cachePath = path.resolve(rootDir, cacheDir, insideCache).replace(/\.coffee$/, '.js');
var mapPath = path.resolve(cachePath.replace(/\.js$/, '.map'));
return {
cache: cachePath,
map: mapPath,
root: rootDir
};
}
require('source-map-support').install({
handleUncaughtExceptions: false,
retrieveSourceMap: function(source) {
if(source.indexOf('.coffee', source.length - '.coffee'.length) === -1) {
return null;
}
var paths = getCachPaths(source);
try{
return {
url: source,
map: fs.readFileSync(paths.map, 'utf8')
};
}catch(err) {
// if we can't read the source map, then we can't read the source map...
return null;
}
return null;
}
});
// See: https://github.com/evanw/node-source-map-support/issues/34
var prepareStackTrace = Error.prepareStackTrace;
Error.__defineGetter__('prepareStackTrace', function(){
return prepareStackTrace;
});
Error.__defineSetter__('prepareStackTrace', function(arg){
if(arg && arg.toString().indexOf("getSourceMapping") != -1) {
//ignore changes to stack trace from coffeescript
return;
}
prepareStackTrace = arg;
});
// Set up an extension map for .coffee files -- we are completely overriding
// CoffeeScript's since it only returns the compiled module.
var coffeeExtension = require.extensions['.coffee'];
var compile = function(module, filename) {
var paths = getCachPaths(filename);
var cachePath = paths.cache;
var mapPath = paths.map;
var rootDir = paths.root;
var content;
// Try and stat the files for their last modified time
try {
var sourceTime = fs.statSync(filename).mtime;
var cacheTime = fs.statSync(cachePath).mtime;
if (cacheTime >= sourceTime) {
// We can return the cached version
content = fs.readFileSync(cachePath, 'utf8');
}
} catch (err) {
// If the cached file was not created yet, this will fail, and that is okay
}
// If we don't have the content, we need to compile ourselves
if (!content) {
try {
// Read from disk and then compile
process.stdout.write(colors.italic(
"coffee-cache: compiling coffee-script file \""+path.relative(rootDir, filename)+"\"..."
));
var compiled = coffee.compile(fs.readFileSync(filename, 'utf8'), {
filename: filename,
sourceMap: true,
bare: true,
generatedFile: path.basename(cachePath),
sourceFiles: [path.basename(filename)]
});
process.stdout.write(colors.italic('Done\n'));
content = compiled.js;
// Since we don't know which version of CoffeeScript we have, make sure
// we handle the older versions that return just the compiled version.
if (content == null)
content = compiled;
// Try writing to cache
fs.mkdirsSync(path.dirname(cachePath));
fs.writeFileSync(cachePath, content, 'utf8');
if (mapPath)
fs.writeFileSync(mapPath, compiled.v3SourceMap, 'utf8');
} catch (err) {
// Let's fail silently and use coffee's require if we need to
if (!content)
return coffeeExtension.apply(this, arguments);
}
}
return module._compile(content, filename);
};
// overwrite require.extensions['.coffee'] the hard way:
// This prevents coffee-script to redefine it with coffee-script/register
require.extensions.__defineGetter__('.coffee', function(){
return compile;
});