Skip to content

Commit

Permalink
slt: add shrinkwrap cmd for filtering fields
Browse files Browse the repository at this point in the history
When using shrinkwrap, combined with a private staging registry, you may
not want your internal registry's URLs baked in to the
npm-shrinkwrap.json file that gets created. This utility strips out
those fields.
  • Loading branch information
rmg committed May 26, 2016
1 parent 4408868 commit 148e93b
Show file tree
Hide file tree
Showing 5 changed files with 3,722 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ module.exports = {
version: require('./lib/version'),
semver: wrapped('semver/bin/semver'),
copyright: require('./lib/copyright'),
shrinkwrap: require('./lib/shrinkwrap'),
};
46 changes: 46 additions & 0 deletions lib/shrinkwrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: strong-tools
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

var _ = require('lodash');
var assert = require('assert');
var debug = require('debug')('strong-tools:fix-license');
var json = require('json-file-plus');
var path = require('path');

updateShrinkwrap.out = console.log;
exports.cli = updateShrinkwrap;

function updateShrinkwrap(shrinkwrap) {
shrinkwrap = path.resolve(shrinkwrap || 'npm-shrinkwrap.json');
debug(shrinkwrap);

updateShrinkwrap.out('removed resolution URLs from %s', shrinkwrap);

return json(shrinkwrap).catch(function(err) {
assert.ifError(err, 'Failed to open ' + shrinkwrap + ': ' + shrinkwrap);
}).then(rewrite).catch(function(err) {
assert.ifError(err, 'Failed to filter out "resolved" from ' + shrinkwrap);
});

function rewrite(file) {
removeResolved(file.data);
updateShrinkwrap.out('removed resolution URLs from %s', shrinkwrap);
return file.save();

// XXX(rmg): this is recursive, but the tree should not be too deep
function removeResolved(obj, k) {
if (_.isObjectLike(obj)) {
// only removed resolved key when the value is a string, otherwise
// we're basically blacklisting any use of the module named "resolved"
if (_.isString(obj.resolved)) {
delete obj.resolved;
}
_.each(obj, removeResolved);
}
}
}
}
Loading

0 comments on commit 148e93b

Please sign in to comment.