Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50650 views
1
var fs = require('fs');
2
var path = require('path');
3
var mkpath = require('mkpath');
4
// We don't want to include our own version of CoffeeScript since we don't know
5
// what version the parent relies on
6
var coffee;
7
try {
8
coffee = require('coffee-script');
9
// CoffeeScript 1.7 support
10
if (typeof coffee.register === 'function') {
11
coffee.register();
12
}
13
} catch (e) {
14
// No coffee-script installed in the project; warn them and stop
15
process.stderr.write("coffee-cache: No coffee-script package found.\n");
16
return;
17
}
18
19
// Directory to store compiled source
20
var cacheDir = process.env['COFFEE_CACHE_DIR'] || '.coffee';
21
22
// Root directory of project - use __dirname by default
23
var rootDir = process.env['COFFEE_ROOT_DIR'] || '.';
24
25
// Storing coffee's require extension for backup use
26
var coffeeExtension = require.extensions['.coffee'];
27
28
// Only log once if we can't write the cache directory
29
var logCouldNotWriteCache = function() {
30
process.stderr.write(
31
"coffee-cache: Could not write cache at " + cacheDir + ".\n"
32
);
33
logCouldNotWriteCache = function(){};
34
}
35
36
// Compile a file as you would with require and return the contents
37
function cacheFile(filename) {
38
// First, convert the filename to something more digestible and use our cache
39
// folder
40
var cachePath = path.join(cacheDir, path.relative(rootDir, filename)).replace(/\.coffee$/, '.js');
41
var content;
42
43
// Try and stat the files for their last modified time
44
try {
45
var cacheTime = fs.statSync(cachePath).mtime;
46
var sourceTime = fs.statSync(filename).mtime;
47
if (cacheTime > sourceTime) {
48
// We can return the cached version
49
content = fs.readFileSync(cachePath, 'utf8');
50
}
51
} catch (err) {
52
// If the cached file was not created yet, this will fail, and that is okay
53
}
54
55
// If we don't have the content, we need to compile ourselves
56
if (!content) {
57
// Read from disk and then compile
58
var mapPath = path.resolve(cachePath.replace(/\.js$/, '.map'));
59
var compiled = coffee.compile(fs.readFileSync(filename, 'utf8'), {
60
filename: filename,
61
sourceMap: true
62
});
63
content = compiled.js;
64
65
// Since we don't know which version of CoffeeScript we have, make sure
66
// we handle the older versions that return just the compiled version.
67
if (content == null)
68
content = compiled;
69
70
try {
71
// Try writing to cache
72
mkpath.sync(path.dirname(cachePath));
73
fs.writeFileSync(cachePath, content, 'utf8');
74
if (mapPath)
75
fs.writeFileSync(mapPath, compiled.v3SourceMap, 'utf8');
76
} catch (err) {
77
logCouldNotWriteCache()
78
}
79
}
80
81
return content;
82
}
83
84
// Set up an extension map for .coffee files -- we are completely overriding
85
// CoffeeScript's since it only returns the compiled module.
86
require.extensions['.coffee'] = function(module, filename) {
87
var content = cacheFile(filename);
88
if (content)
89
// Successfully retrieved the file from disk or the cache
90
return module._compile(content, filename);
91
else
92
// Something went wrong, so we can use coffee's require
93
return coffeeExtension.apply(this, arguments);
94
};
95
96
// Export settings
97
module.exports = {
98
setCacheDir: function(dir, root){
99
if(root) {
100
rootDir = root;
101
}
102
103
cacheDir = dir;
104
return this;
105
},
106
cacheFile: cacheFile,
107
};
108
109