Files
bower/lib/util/save.js
Karthik Kastury e0c30b5573 Revert "Moved config.js to lib/ instead of lib/core"
This reverts commit 6c6e193362.

Revert "Allow Custom Bower Endpoints"

This reverts commit 9e7f136948.
2012-09-14 22:34:50 +05:30

56 lines
1.6 KiB
JavaScript

// ==========================================
// BOWER: SAVE
// ==========================================
// Copyright 2012 Twitter, Inc
// Licensed under The MIT License
// http://opensource.org/licenses/MIT
// ==========================================
var path = require('path');
var fs = require('fs');
var _ = require('underscore');
var config = require('../core/config');
function save (eventType, modifier, emitter, manager, paths) {
manager.on(eventType, manager.on('loadJSON', function () {
if (!this.json) return emitter.emit('error', new Error('Please define a ' + config.json));
var pkgs = paths.map(function (path) {
path = path.split('#')[0];
return _.find(Object.keys(this.dependencies), function (key) {
var dep = this.dependencies[key][0];
return dep.name == path
|| (dep.url && dep.url == path)
|| (dep.path && dep.path == path);
}.bind(this));
}.bind(this));
pkgs = _.compact(pkgs).map(function (name) {
return this.dependencies[name][0];
}.bind(this));
pkgs.forEach(modifier.bind(this));
fs.writeFileSync(path.join(this.cwd, config.json), JSON.stringify(this.json, null, 2));
}).loadJSON.bind(manager));
};
function addDependency(pkg) {
var path = (pkg.gitUrl || pkg.assetUrl || pkg.path || '');
var tag = pkg.tag ? '#' + pkg.tag : '';
this.json.dependencies[pkg.name] = path + tag;
}
function removeDependency(pkg) {
delete this.json.dependencies[pkg.name];
}
module.exports = save.bind(this, 'resolve', addDependency);
module.exports.discard = save.bind(this, 'resolveLocal', removeDependency);