From 0a9fc9f5a0481531ada78f96213cabff3dd14081 Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Sun, 2 Jul 2023 16:42:22 +0200 Subject: [PATCH] color support --- README.md | 1 + npm-manifest-check.py | 53 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2ee7d13..fbdc6de 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ positional arguments: optional arguments: -h, --help show this help message and exit -b, --brief do not show detailed comparisons of mismatching values + -c, --color colorize the output ``` ### Single package diff --git a/npm-manifest-check.py b/npm-manifest-check.py index 63a46ee..c8dc8f6 100755 --- a/npm-manifest-check.py +++ b/npm-manifest-check.py @@ -25,6 +25,18 @@ class Package: self.reported_manifest = parse_manifest(name) self.actual_manifest = parse_actual_manifest(name, self.reported_manifest.version) +class colors: + PURPLE = '\033[1;35;48m' + CYAN = '\033[1;36;48m' + BOLD = '\033[1;37;48m' + BLUE = '\033[1;34;48m' + GREEN = '\033[1;32;48m' + YELLOW = '\033[1;33;48m' + RED = '\033[1;31;48m' + BLACK = '\033[1;30;48m' + UNDERLINE = '\033[4;37;48m' + END = '\033[1;37;0m' + # https://www.npmjs.com/package/darcyclarke-manifest-pkg/v/2.1.15/index # hex checksum = file name # use hex to get *actual* manifest: @@ -93,41 +105,77 @@ def parse_actual_manifest(pkg, ver): return Manifest(name, version, dependencies, scripts) -def compare_manifests(pkg, brief=False): +def compare_manifests(pkg, brief=False, color=False): mismatch = False if pkg.reported_manifest.version != pkg.actual_manifest.version: mismatch = True + if color: + print(colors.RED, end='') print('Version mismatch for {}!'.format(pkg.name)) + if color: + print(colors.END, end='') if not brief: + if color: + print(colors.YELLOW, end='') print('Reported version: {}'.format(pkg.reported_manifest.version)) print('Actual version: {}'.format(pkg.actual_manifest.version)) + if color: + print(colors.END, end='') if pkg.actual_manifest.dependencies != pkg.reported_manifest.dependencies: mismatch = True + if color: + print(colors.RED, end='') print('Dependency mismatch detected for {}!'.format(pkg.name)) + if color: + print(colors.END, end='') if not brief: + if color: + print(colors.YELLOW, end='') dep_diff = DeepDiff(pkg.reported_manifest.dependencies, pkg.actual_manifest.dependencies, verbose_level=2) pprint(dep_diff, indent=2) + if color: + print(colors.END, end='') if pkg.actual_manifest.scripts != pkg.reported_manifest.scripts: mismatch = True + if color: + print(colors.RED, end='') print('Scripts mismatch detected for {}!'.format(pkg.name)) + if color: + print(colors.END, end='') if not brief: + if color: + print(colors.YELLOW, end='') scripts_diff = DeepDiff(pkg.reported_manifest.scripts, pkg.actual_manifest.scripts, verbose_level=2) pprint(scripts_diff, indent=2) + if color: + print(colors.END, end='') if pkg.actual_manifest.name != pkg.reported_manifest.name: mismatch = True + if color: + print(colors.RED, end='') print('Name mismatch detected for {}!'.format(pkg.name)) + if color: + print(colors.END, end='') if not brief: + if color: + print(colors.YELLOW, end='') print('Reported name: {}'.format(pkg.reported_manifest.name)) print('Actual name: {}'.format(pkg.actual_manifest.name)) + if color: + print(colors.END, end='') if not mismatch: + if color: + print(colors.GREEN, end='') print('No mismatch detected for {}.'.format(pkg.name)) + if color: + print(colors.END, end='') return mismatch @@ -138,11 +186,12 @@ def main(): parser = argparse.ArgumentParser(prog='npm-manifest-check', description='Check NPM packages for manifest mismatches') parser.add_argument('-b', '--brief', action='store_true', help='do not show detailed comparisons of mismatching values') + parser.add_argument('-c', '--color', action='store_true', help='colorize the output') parser.add_argument('package', type=str, help='name of the NPM package') args = parser.parse_args() package = Package(args.package) - mismatching = compare_manifests(package, brief=args.brief) + mismatching = compare_manifests(package, brief=args.brief, color=args.color) if mismatching: sys.exit(1)