This repository has been archived by the owner on Aug 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
cache.js
112 lines (93 loc) · 2.78 KB
/
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
var tar = require('./tar'),
env = require('./env'),
rimraf = require('rimraf'),
mkdirp = require('mkdirp'),
async = require('async'),
path = require('path'),
fs = require('fs');
var pathExists = fs.exists || path.exists;
exports.CACHE_DIR = path.resolve(env.home, '.jam/cache');
exports.add = function (name, version, filepath, callback) {
var filename = name + '-' + version + '.tar.gz';
var dir = exports.dir(name, version);
var tarfile = path.resolve(dir, filename);
var cachedir = path.resolve(dir, 'package');
mkdirp(dir, function (err) {
if (err) {
return callback(err);
}
async.series([
async.apply(tar.create, filepath, tarfile),
async.apply(tar.extract, tarfile, cachedir)
],
function (err) {
callback(err, tarfile, cachedir);
});
});
};
exports.get = function (name, version, callback) {
var filename = name + '-' + version + '.tar.gz';
var dir = exports.dir(name, version);
var tarfile = path.resolve(dir, filename);
var cachedir = path.resolve(dir, 'package');
pathExists(cachedir, function (exists) {
if (exists) {
return callback(null, tarfile, cachedir);
}
else {
// Package not found in cache, return null
return callback(null, null, null);
}
});
};
exports.moveTar = function (name, version, filepath, callback) {
var filename = name + '-' + version + '.tar.gz';
var dir = exports.dir(name, version);
var tarfile = path.resolve(dir,filename);
var cachedir = path.resolve(dir,'package');
async.series([
async.apply(mkdirp, dir),
async.apply(rimraf, cachedir),
async.apply(fs.rename, filepath, tarfile),
async.apply(tar.extract, tarfile, cachedir)
],
function (err) {
if (err) {
return callback(err);
}
callback(null, tarfile, cachedir);
});
};
exports.dir = function (name, version) {
var args = Array.prototype.slice.call(arguments);
return path.resolve.apply(path, [exports.CACHE_DIR].concat(args));
};
exports.clear = function (name, version, callback) {
if (!callback) {
callback = version;
version = null;
}
if (!callback) {
callback = name;
name = null;
}
var dir;
if (!name) {
dir = exports.dir();
}
else if (!version) {
dir = exports.dir(name);
}
else {
dir = exports.dir(name, version);
}
rimraf(dir, callback);
};
exports.update = function (name, version, filepath, callback) {
exports.clear(name, version, function (err) {
if (err) {
return callback(err);
}
exports.add(name, version, filepath, callback);
});
};