1 | (function() {
|
2 | var Cache, crypto, csVer, fs, ltVer, path;
|
3 |
|
4 | fs = require('fs');
|
5 |
|
6 | path = require('path');
|
7 |
|
8 | crypto = require('crypto');
|
9 |
|
10 | ltVer = require('./../package.json').version;
|
11 |
|
12 | csVer = ((typeof window !== "undefined" && window !== null ? window.CoffeeScript : void 0) || require('coffee-script')).VERSION;
|
13 |
|
14 | module.exports = Cache = (function() {
|
15 | function Cache(basepath) {
|
16 | this.basepath = basepath;
|
17 | if (!fs.existsSync(this.basepath)) {
|
18 | fs.mkdirSync(this.basepath, 0x1ed);
|
19 | }
|
20 | }
|
21 |
|
22 | Cache.prototype.path = function(source) {
|
23 | return path.join(this.basepath, csVer + "-" + ltVer + "-" + this.prefix + "-" + (this.hash(source)));
|
24 | };
|
25 |
|
26 | Cache.prototype.get = function(source) {
|
27 | return JSON.parse(fs.readFileSync(this.path(source), 'utf8'));
|
28 | };
|
29 |
|
30 | Cache.prototype.set = function(source, result) {
|
31 | return fs.writeFileSync(this.path(source), JSON.stringify(result));
|
32 | };
|
33 |
|
34 | Cache.prototype.has = function(source) {
|
35 | return fs.existsSync(this.path(source));
|
36 | };
|
37 |
|
38 | Cache.prototype.hash = function(data) {
|
39 | return crypto.createHash('md5').update('' + data).digest('hex').substring(0, 8);
|
40 | };
|
41 |
|
42 | Cache.prototype.setConfig = function(config) {
|
43 | return this.prefix = this.hash(JSON.stringify(config));
|
44 | };
|
45 |
|
46 | return Cache;
|
47 |
|
48 | })();
|
49 |
|
50 | }).call(this);
|