Skip to content

Commit

Permalink
Add command line option parser
Browse files Browse the repository at this point in the history
This commit also addresses Issue #5 and adds the ability to not
modify packages with a resolved field.
  • Loading branch information
jeslie0 committed Oct 8, 2024
1 parent f792f60 commit 40a460e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 45 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 37 additions & 23 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
description = "A very basic python flake template, providing a devshell.";
description = "Add missing integrity and resolved fields to a package-lock.json file.";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
Expand All @@ -21,13 +21,13 @@
);

pyPkgs = system:
nixpkgsFor.${system}.python310Packages;
nixpkgsFor.${system}.python312Packages;

packageName = with builtins;
head (match "^.*name[[:space:]]*=[[:space:]][\"]([^[:space:]]*)[\"][,].*$" (readFile ./setup.py));
head (match "^.*name[[:space:]]*=[[:space:]]*[\"]([^[:space:]]*)[\"][,].*$" (readFile ./setup.py));

version = with builtins;
head (match "^.*version[[:space:]]*=[[:space:]][\"]([^[:space:]]*)[\"][,].*$" (readFile ./setup.py));
head (match "^.*version[[:space:]]*=[[:space:]]*[\"]([^[:space:]]*)[\"][,].*$" (readFile ./setup.py));

in
{
Expand All @@ -36,20 +36,35 @@
let
pkgs =
nixpkgsFor.${system};

in
{
default = (pyPkgs system).buildPythonPackage {
pname = packageName;

default =
self.packages.${system}.npm-lockfile-fix;

npm-lockfile-fix = (pyPkgs system).buildPythonPackage {
inherit version;
src = ./.;
doCheck = false;
pname =
packageName;

src =
./.;

doCheck =
false;

propagatedBuildInputs =
[ (pyPkgs system).requests ];

propagatedBuildInputs = [ (pyPkgs system).requests ];
meta = {
homepage = "https://github.com/jeslie0/npm-lockfile-fix";
description = "";
license = pkgs.lib.licenses.mit;
homepage =
"https://github.com/jeslie0/npm-lockfile-fix";

description =
"Add missing integrity and resolved fields to a package-lock.json file.";

license =
pkgs.lib.licenses.mit;
};
};
}
Expand All @@ -64,16 +79,15 @@
{
default =
pkgs.mkShell {
packages = with pkgs;
[ python310Packages.python-lsp-server
python310Packages.rope
python310Packages.pyflakes
python310Packages.mccabe
python310Packages.pycodestyle
python310Packages.mypy
python310Packages.flake8
python310Packages.pylint

packages = with pkgs.python312Packages;
[ python-lsp-server
rope
pyflakes
mccabe
pycodestyle
mypy
flake8
pylint
];

inputsFrom =
Expand Down
16 changes: 8 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from setuptools import setup, find_packages

setup(
name = "npm-lockfile-fix",
version = "0.1.0",
author = "James Leslie",
author_email = "[email protected]",
description = " A tool to add missing resolved and integrity fields to a npm package lockfile.",
url = "https://github.com/jeslie0/npm-lockfile-fix",
packages = find_packages(),
name="npm-lockfile-fix",
version="0.1.0",
author="James Leslie",
author_email="[email protected]",
description="A tool to add missing resolved and integrity fields to a npm package lockfile.",
url="https://github.com/jeslie0/npm-lockfile-fix",
packages=find_packages(),
python_requires='>=3.10',
entry_points={
'console_scripts': [
'npm-lockfile-fix=src.__main__:main',
],
},
install_requires=[
'requests',
'requests'
]
)
69 changes: 58 additions & 11 deletions src/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import sys
import requests
import argparse

# Set the URL of the registry
REGISTRY_URL = 'https://registry.npmjs.org/'
Expand All @@ -15,7 +15,7 @@ def get_lockfile_json(lockfile_path: str):
return lockfile_json


def loop_through_packages(packages: json) -> None:
def loop_through_packages(packages: json, onlyWithoutResolved: bool) -> None:
"""Loop over each package in the packages section of the lockfile"""

# Establish a session to allow a connection to the same host to persist.
Expand All @@ -26,14 +26,22 @@ def loop_through_packages(packages: json) -> None:
if package_key == "" or "node_modules/" not in package_key:
continue

package: str = packages[package_key]
package: json = packages[package_key]
package_name: str = package.get("name") or package_key.split("node_modules/")[-1]

# Check if the package is missing resolved and integrity fields
noResolved: bool = 'resolved' not in package
noIntegrity: bool = 'integrity' not in package
noLink: bool = 'link' not in package
if noResolved or (noIntegrity and noLink):

# Define whether or not the json should be updated. Normally,
# we update the json if there is no resolved, or noIntegrity
# and noLink.
shouldBeUpdated: bool = noResolved or (noIntegrity and noLink)
if onlyWithoutResolved:
shouldBeUpdated = (not noResolved) and noIntegrity and noLink

if shouldBeUpdated:
# Get the package version from the lockfile
version: str = package['version']

Expand Down Expand Up @@ -63,18 +71,57 @@ def save_json(data: json, path: str) -> None:
json.dump(data, f, indent=2)


def makeParser():
parser = argparse.ArgumentParser(
prog='npm-lockfile-fix',
description='Add missing integrity and resolved fields to a package-lock.json file. By default this will modify the specified file.',
)

parser.add_argument(
"filename",
help="the package-lock.json file to patch"
)
parser.add_argument(
"-r",
action='store_true',
help='only patch dependencies without a resolve field'
)
parser.add_argument(
"-o", "--output",
action="store_const",
help="leave input file unmodified and output to specified file"
)
parser.add_argument(
"--cout",
action="store_true",
help="leave input file unmodified and output to stdout"
)

return parser


def main():
# Get the path to the package-lock.json file from the command-line arguments
if len(sys.argv) != 2:
print('Usage: npm-fixer /path/to/package-lock.json')
sys.exit(1)
lockfile_path = sys.argv[1]
args = makeParser().parse_args()

lockfile_path = args.filename

lockfile_json = get_lockfile_json(lockfile_path)

loop_through_packages(lockfile_json['packages'])
loop_through_packages(lockfile_json['packages'], args.r)

if args.cout:
print(json.dumps(lockfile_json))
return 0

outpath = lockfile_path if args.output is None else args.output

save_json(lockfile_json, outpath)

return 0




save_json(lockfile_json, lockfile_path)


if __name__ == "__main__":
Expand Down

0 comments on commit 40a460e

Please sign in to comment.