@vltpkg/satisfies 1.0.0-rc.23 → 1.0.0-rc.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ import type { DepID, DepIDTuple } from '@vltpkg/dep-id';
2
+ import { Spec } from '@vltpkg/spec';
3
+ import { Monorepo } from '@vltpkg/workspaces';
4
+ /**
5
+ * Return true if the node referenced by this DepID would satisfy the
6
+ * supplied Spec object.
7
+ */
8
+ export declare const satisfies: (id: DepID | undefined, spec: Spec, fromLocation?: string, projectRoot?: string, monorepo?: Monorepo) => boolean;
9
+ export declare const satisfiesTuple: (tuple: DepIDTuple, spec: Spec, fromLocation?: string, projectRoot?: string, monorepo?: Monorepo) => boolean;
package/dist/index.js ADDED
@@ -0,0 +1,121 @@
1
+ import { splitDepID } from '@vltpkg/dep-id';
2
+ import { error } from '@vltpkg/error-cause';
3
+ import { parse, Version } from '@vltpkg/semver';
4
+ import { defaultRegistryName, Spec } from '@vltpkg/spec';
5
+ import { Monorepo } from '@vltpkg/workspaces';
6
+ import { relative, resolve } from 'node:path';
7
+ /**
8
+ * Return true if the node referenced by this DepID would satisfy the
9
+ * supplied Spec object.
10
+ */
11
+ export const satisfies = (id, spec, fromLocation = process.cwd(), projectRoot = process.cwd(), monorepo) => !!id &&
12
+ satisfiesTuple(splitDepID(id), spec, fromLocation, projectRoot, monorepo);
13
+ export const satisfiesTuple = (tuple, spec, fromLocation = process.cwd(), projectRoot = process.cwd(), monorepo) => {
14
+ const { options } = spec;
15
+ spec = spec.final;
16
+ const [type, first, second] = tuple;
17
+ if (spec.type !== type)
18
+ return false;
19
+ switch (spec.type) {
20
+ case 'registry': {
21
+ /* c8 ignore start - should be impossible */
22
+ if (!first) {
23
+ // must be from the default registry
24
+ if (spec.registry !== options.registry) {
25
+ return false;
26
+ }
27
+ /* c8 ignore stop */
28
+ }
29
+ else {
30
+ let namedRegistry = options.registries[first];
31
+ /* c8 ignore next 3 */
32
+ if (!namedRegistry && first === defaultRegistryName) {
33
+ namedRegistry = options.registry;
34
+ }
35
+ if (namedRegistry && namedRegistry !== spec.registry) {
36
+ // we know the name, and it's not the registry being used
37
+ return false;
38
+ }
39
+ else if (!namedRegistry && first !== spec.registry) {
40
+ // an explicit registry URL, but does not match
41
+ return false;
42
+ }
43
+ }
44
+ /* c8 ignore next */
45
+ if (!second)
46
+ throw error('Invalid DepID', { found: tuple });
47
+ const [name, version] = parseNameVer(second);
48
+ return (
49
+ // mismatched name always invalid
50
+ name !== spec.name || !version ? false
51
+ // if just a dist-tag, assume valid
52
+ : !spec.range ? true
53
+ : spec.range.test(Version.parse(version)));
54
+ }
55
+ case 'file': {
56
+ /* c8 ignore next - should be impossible */
57
+ if (spec.file === undefined)
58
+ return false;
59
+ const resolvedSpec = resolve(projectRoot, fromLocation, spec.file);
60
+ const resolvedId = resolve(projectRoot, first);
61
+ // valid if the relative path is '', refers to the same path
62
+ return !relative(resolvedSpec, resolvedId);
63
+ }
64
+ case 'workspace': {
65
+ monorepo ??= Monorepo.load(projectRoot);
66
+ /* c8 ignore next */
67
+ if (!spec.workspace)
68
+ return false;
69
+ const fromID = monorepo.get(first);
70
+ const fromSpec = monorepo.get(spec.workspace);
71
+ if (fromID !== fromSpec || !fromSpec || !fromID)
72
+ return false;
73
+ if (!spec.range)
74
+ return true;
75
+ const v = parse(fromID.manifest.version ?? '');
76
+ return !!v && spec.range.test(v);
77
+ }
78
+ case 'remote': {
79
+ return spec.remoteURL === first;
80
+ }
81
+ case 'git': {
82
+ const { gitRemote, gitSelectorParsed = {}, gitSelector, gitCommittish, namedGitHost, namedGitHostPath, } = spec;
83
+ if (gitRemote !== first) {
84
+ if (namedGitHost && namedGitHostPath) {
85
+ const ngh = `${namedGitHost}:`;
86
+ if (first.startsWith(ngh)) {
87
+ if (first !== ngh + namedGitHostPath)
88
+ return false;
89
+ }
90
+ else
91
+ return false;
92
+ }
93
+ else
94
+ return false;
95
+ }
96
+ if (gitSelector && !second)
97
+ return false;
98
+ /* c8 ignore next - always set to something, even if empty */
99
+ const [parsed, committish] = Spec.parseGitSelector(second ?? '');
100
+ if (gitCommittish && committish)
101
+ return committish.startsWith(gitCommittish);
102
+ for (const [k, v] of Object.entries(gitSelectorParsed)) {
103
+ if (parsed[k] !== v)
104
+ return false;
105
+ }
106
+ return true;
107
+ }
108
+ /* c8 ignore start */
109
+ default: {
110
+ throw error('Invalid spec type', { spec });
111
+ }
112
+ }
113
+ /* c8 ignore stop */
114
+ };
115
+ const parseNameVer = (nv) => {
116
+ const at = nv.lastIndexOf('@');
117
+ // if it's 0, means @scoped without a version
118
+ return at <= 0 ?
119
+ [nv, '']
120
+ : [nv.substring(0, at), nv.substring(at + 1)];
121
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/satisfies",
3
3
  "description": "method for determining if a DepID satisfies a Spec",
4
- "version": "1.0.0-rc.23",
4
+ "version": "1.0.0-rc.24",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -12,11 +12,11 @@
12
12
  "email": "support@vlt.sh"
13
13
  },
14
14
  "dependencies": {
15
- "@vltpkg/dep-id": "1.0.0-rc.23",
16
- "@vltpkg/error-cause": "1.0.0-rc.23",
17
- "@vltpkg/semver": "1.0.0-rc.23",
18
- "@vltpkg/spec": "1.0.0-rc.23",
19
- "@vltpkg/workspaces": "1.0.0-rc.23"
15
+ "@vltpkg/dep-id": "1.0.0-rc.24",
16
+ "@vltpkg/error-cause": "1.0.0-rc.24",
17
+ "@vltpkg/semver": "1.0.0-rc.24",
18
+ "@vltpkg/spec": "1.0.0-rc.24",
19
+ "@vltpkg/workspaces": "1.0.0-rc.24"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@eslint/js": "^9.39.1",