1#!/usr/bin/env nix-shell
2#!nix-shell -i python3 -p
3
4import collections.abc
5import fileinput
6import json
7import os.path
8import re
9import sys
10
11
12def remove(attr):
13 with open(os.path.join(os.path.dirname(__file__), 'node-packages.json'), 'r+') as node_packages_json:
14 packages = json.load(node_packages_json)
15 idx = 0
16 while idx < len(packages):
17 if packages[idx] == attr or (isinstance(packages[idx], collections.abc.Mapping) and next(iter(packages[idx].keys())) == attr):
18 del packages[idx]
19 else:
20 idx += 1
21
22 node_packages_json.seek(0)
23 for idx, package in enumerate(packages):
24 if idx == 0:
25 node_packages_json.write('[\n ')
26 else:
27 node_packages_json.write(', ')
28 json.dump(package, node_packages_json)
29 node_packages_json.write('\n')
30 node_packages_json.write(']\n')
31 node_packages_json.truncate()
32
33 with fileinput.input(os.path.join(os.path.dirname(__file__), 'node-packages.nix'), inplace=1) as node_packages:
34 safe_attr = re.escape(attr)
35 in_attr = False
36 for line in node_packages:
37 if in_attr:
38 if re.fullmatch(r' \};\n', line):
39 in_attr = False
40 else:
41 if re.fullmatch(rf' (?:{safe_attr}|"{safe_attr}") = nodeEnv\.buildNodePackage \{{\n', line):
42 in_attr = True
43 else:
44 sys.stdout.write(line)
45
46
47if __name__ == '__main__':
48 import argparse
49
50 parser = argparse.ArgumentParser(description='Remove a given package from the node-packages.nix file')
51 parser.add_argument('attr', help='The package attribute to remove')
52 args = parser.parse_args()
53
54 remove(args.attr)