aboutsummaryrefslogtreecommitdiffstats
path: root/build/plugins/lib/nots/package_manager/pnpm/workspace.py
blob: e596e20a18b9830674222bc6c9e2ddec18039e89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import yaml


class PnpmWorkspace(object):
    @classmethod
    def load(cls, path):
        ws = cls(path)
        ws.read()

        return ws

    def __init__(self, path):
        if not os.path.isabs(path):
            raise TypeError("Absolute path required, given: {}".format(path))

        self.path = path
        # NOTE: pnpm requires relative workspace paths.
        self.packages = set()

    def read(self):
        with open(self.path) as f:
            self.packages = set(yaml.load(f, Loader=yaml.CSafeLoader).get("packages", []))

    def write(self, path=None):
        if not path:
            path = self.path

        with open(path, "w") as f:
            data = {
                "packages": list(self.packages),
            }
            yaml.dump(data, f, Dumper=yaml.CSafeDumper)

    def get_paths(self, base_path=None, ignore_self=False):
        """
        Returns absolute paths of the workspace packages.
        :param base_path: base path to resolve relative dep paths
        :type base_path: str
        :param ignore_self: whether path of the current module will be excluded (if present)
        :type ignore_self: bool
        :rtype: list of str
        """
        if base_path is None:
            base_path = os.path.dirname(self.path)

        return [
            os.path.normpath(os.path.join(base_path, pkg_path))
            for pkg_path in self.packages
            if not ignore_self or pkg_path != "."
        ]

    def set_from_package_json(self, package_json):
        """
        Sets packages to "workspace" deps from given package.json.
        :param package_json: package.json of workspace
        :type package_json: PackageJson
        """
        if os.path.dirname(package_json.path) != os.path.dirname(self.path):
            raise TypeError(
                "package.json should be in workspace directory {}, given: {}".format(
                    os.path.dirname(self.path), package_json.path
                )
            )

        self.packages = set(path for _, path in package_json.get_workspace_dep_spec_paths())
        # Add relative path to self.
        self.packages.add(".")

    def merge(self, ws):
        """
        Adds `ws`'s packages to the workspace.
        :param ws: workspace to merge
        :type ws: PnpmWorkspace
        """
        dir_path = os.path.dirname(self.path)
        ws_dir_path = os.path.dirname(ws.path)

        for p_rel_path in ws.packages:
            p_path = os.path.normpath(os.path.join(ws_dir_path, p_rel_path))
            self.packages.add(os.path.relpath(p_path, dir_path))