|
|
30582b |
#!/usr/bin/python
|
|
|
30582b |
|
|
|
30582b |
"""Modify a dependency listed in a package.json file"""
|
|
|
30582b |
|
|
|
30582b |
# Copyright 2013 T.C. Hollingsworth <tchollingsworth@gmail.com>
|
|
|
30582b |
#
|
|
|
30582b |
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
30582b |
# of this software and associated documentation files (the "Software"), to
|
|
|
30582b |
# deal in the Software without restriction, including without limitation the
|
|
|
30582b |
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
30582b |
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
30582b |
# furnished to do so, subject to the following conditions:
|
|
|
30582b |
#
|
|
|
30582b |
# The above copyright notice and this permission notice shall be included in
|
|
|
30582b |
# all copies or substantial portions of the Software.
|
|
|
30582b |
#
|
|
|
30582b |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
30582b |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
30582b |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
30582b |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
30582b |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
30582b |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
30582b |
# IN THE SOFTWARE.
|
|
|
30582b |
|
|
|
30582b |
import json
|
|
|
30582b |
import os
|
|
|
30582b |
import shutil
|
|
|
30582b |
import sys
|
|
|
30582b |
|
|
|
30582b |
if not os.path.exists('package.json~'):
|
|
|
30582b |
shutil.copy2('package.json', 'package.json~')
|
|
|
30582b |
|
|
|
30582b |
md = json.load(open('package.json'))
|
|
|
30582b |
|
|
|
30582b |
if 'dependencies' not in md:
|
|
|
30582b |
md['dependencies'] = {}
|
|
|
30582b |
|
|
|
30582b |
if sys.argv[1] == '-r':
|
|
|
30582b |
dep = sys.argv[2]
|
|
|
30582b |
del md['dependencies'][dep]
|
|
|
30582b |
else:
|
|
|
30582b |
dep = sys.argv[1]
|
|
|
30582b |
|
|
|
30582b |
if len(sys.argv) > 2:
|
|
|
30582b |
ver = sys.argv[2]
|
|
|
30582b |
else:
|
|
|
30582b |
ver = '*'
|
|
|
30582b |
|
|
|
30582b |
md['dependencies'][dep] = ver
|
|
|
30582b |
|
|
|
30582b |
fh = open('package.json', 'w')
|
|
|
30582b |
data = json.JSONEncoder(indent=4).encode(md)
|
|
|
30582b |
fh.write(data)
|
|
|
30582b |
fh.close()
|