Skip to content

Commit dfe4b66

Browse files
committed
Use consistent cacheName for unit work id
1 parent ad657f6 commit dfe4b66

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

lib/core/package.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ var Package = function (name, endpoint, manager) {
115115
this.on('data', this.manager.emit.bind(this.manager, 'data'));
116116
this.on('error', function (err, origin) {
117117
// Unlock the unit of work automatically on error (only if the error is from this package)
118-
if (!origin && this.unitWork.isLocked(this.name)) this.unitWork.unlock(this.name, this);
118+
if (!origin && this.unitWork.isLocked(this.cacheName)) this.unitWork.unlock(this.cacheName, this);
119119
// Propagate the error event to the parent package/manager
120120
this.manager.emit('error', err, origin || this);
121121
}.bind(this));
@@ -134,9 +134,9 @@ Package.prototype.constructor = Package;
134134
Package.prototype.resolve = function () {
135135
// Ensure that nobody is resolving the same dep at the same time
136136
// If there is, we wait for the unlock event
137-
if (this.unitWork.isLocked(this.name)) return this.unitWork.on('unlock', this.waitUnlock);
137+
if (this.unitWork.isLocked(this.cacheName)) return this.unitWork.on('unlock', this.waitUnlock);
138138

139-
var data = this.unitWork.retrieve(this.name);
139+
var data = this.unitWork.retrieve(this.cacheName);
140140
if (data) {
141141
// Check if this exact package is the last resolved one
142142
// If so, we copy the resolved result and we don't need to do anything else
@@ -148,7 +148,7 @@ Package.prototype.resolve = function () {
148148
}
149149

150150
// If not, we lock and resolve it
151-
this.unitWork.lock(this.name, this);
151+
this.unitWork.lock(this.cacheName, this);
152152

153153
if (this.assetUrl) {
154154
this.download();
@@ -175,7 +175,7 @@ Package.prototype.lookup = function () {
175175

176176
Package.prototype.install = function () {
177177
// Only print the installing action if this package has been resolved
178-
if (this.unitWork.retrieve(this.name)) {
178+
if (this.unitWork.retrieve(this.cacheName)) {
179179
template('action', { name: 'installing', shizzle: this.name + (this.version ? '#' + this.version : '') })
180180
.on('data', this.emit.bind(this, 'data'));
181181
}
@@ -380,10 +380,10 @@ Package.prototype.loadJSON = function () {
380380
var cleanedTag;
381381
if (this.tag && (cleanedTag = semver.clean(this.tag)) && cleanedTag !== this.version) {
382382
// Only print the warning once
383-
if (!this.unitWork.retrieve('mismatch#' + this.name + '_' + cleanedTag)) {
384-
template('warning-mismatch', { name: this.name, json: this.localConfig.json, tag: cleanedTag, version: this.version || 'N/A' })
383+
if (!this.unitWork.retrieve('mismatch#' + this.cacheName + '_' + cleanedTag)) {
384+
template('warning-mismatch', { name: this.cacheName, json: this.localConfig.json, tag: cleanedTag, version: this.version || 'N/A' })
385385
.on('data', this.emit.bind(this, 'data'));
386-
this.unitWork.store('mismatch#' + this.name + '_' + cleanedTag, true);
386+
this.unitWork.store('mismatch#' + this.cacheName + '_' + cleanedTag, true);
387387
}
388388
// Assume the tag
389389
this.version = cleanedTag;
@@ -549,8 +549,8 @@ Package.prototype.getDeepDependencies = function (result) {
549549
};
550550

551551
Package.prototype.saveUnit = function () {
552-
this.unitWork.store(this.name, this.serialize(), this);
553-
this.unitWork.unlock(this.name, this);
552+
this.unitWork.store(this.cacheName, this.serialize(), this);
553+
this.unitWork.unlock(this.cacheName, this);
554554
this.addDependencies();
555555
};
556556

@@ -585,10 +585,10 @@ Package.prototype.cache = function () {
585585
// If the force options is true, we need to erase from the cache
586586
// Be aware that a similar package might already flushed it
587587
// To prevent that we check the unit of work storage
588-
if (this.opts.force && !this.unitWork.retrieve('flushed#' + this.name + '_' + this.resourceId)) {
588+
if (this.opts.force && !this.unitWork.retrieve('flushed#' + this.cacheName + '_' + this.resourceId)) {
589589
rimraf(this.path, function (err) {
590590
if (err) return this.emit('error', err);
591-
this.unitWork.store('flushed#' + this.name + '_' + this.resourceId, true);
591+
this.unitWork.store('flushed#' + this.cacheName + '_' + this.resourceId, true);
592592
this.cache();
593593
}.bind(this));
594594
return this;
@@ -811,11 +811,7 @@ Package.prototype.unserialize = function (obj) {
811811

812812
Package.prototype.generateResourceId = function () {
813813
this.resourceId = crypto.createHash('md5').update(this.gitUrl).digest('hex');
814-
815-
// Always generate cache name from the git url
816-
// this.name maybe a guessed name which can change after loadJSON
817-
var name = path.basename(this.gitUrl).replace(/(\.git)?(#.*)?$/, '');
818-
this.gitPath = path.join(config.cache, name, this.resourceId);
814+
this.gitPath = path.join(config.cache, this.cacheName, this.resourceId);
819815
};
820816

821817
Package.prototype.resolveShorthand = function (shorthand, path) {
@@ -830,6 +826,14 @@ Package.prototype.resolveShorthand = function (shorthand, path) {
830826
});
831827
};
832828

829+
Package.prototype.__defineGetter__('cacheName', function () {
830+
if (this.gitUrl) {
831+
return path.basename(this.gitUrl).replace(/(\.git)?(#.*)?$/, '');
832+
} else {
833+
return this.name;
834+
}
835+
});
836+
833837
Package.prototype.__defineGetter__('localPath', function () {
834838
return path.join(process.cwd(), config.directory, this.name);
835839
});

0 commit comments

Comments
 (0)