@preventive/deptree 1.0.0-alpha.1 → 1.0.0-alpha.11
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.
- package/LICENSE-APACHE +202 -0
- package/bin/compare.js +132 -0
- package/bin/deptree.js +211 -0
- package/bin/npmrc.js +21 -0
- package/cargo.d.ts +223 -0
- package/cargo.js +6 -0
- package/npm.d.ts +325 -0
- package/npm.js +7 -0
- package/package.json +92 -11
- package/pnpm.d.ts +401 -81
- package/pnpm.js +4 -9
- package/soldeer.d.ts +235 -0
- package/soldeer.js +6 -0
- package/src/cargo/crate.js +183 -0
- package/src/cargo/inputs.js +81 -0
- package/src/cargo/members.js +90 -0
- package/src/cargo/registry.js +65 -0
- package/src/cargo/tree.js +125 -0
- package/src/cargo/workspace.js +177 -0
- package/src/concurrent.js +32 -0
- package/src/error.js +8 -10
- package/src/glob.js +55 -0
- package/src/hash.js +51 -16
- package/src/manifest.js +52 -0
- package/src/matcher.js +60 -10
- package/src/mount.js +124 -42
- package/src/npm/bins.js +54 -0
- package/src/npm/compat.js +97 -0
- package/src/npm/dev-engines.js +52 -0
- package/src/npm/graph.js +68 -0
- package/src/npm/inputs.js +75 -0
- package/src/npm/manifests.js +130 -0
- package/src/npm/package.js +96 -0
- package/src/npm/ratio.js +34 -0
- package/src/npm/settings.js +67 -0
- package/src/npm/tree.js +139 -0
- package/src/npm/versions.js +20 -0
- package/src/npmrc.js +24 -0
- package/src/patch.js +26 -45
- package/src/pnpm/bins.js +202 -322
- package/src/pnpm/checks.js +8 -35
- package/src/pnpm/commands.js +230 -0
- package/src/pnpm/filename.js +19 -16
- package/src/pnpm/frozen.js +29 -73
- package/src/pnpm/gitignore.js +138 -0
- package/src/pnpm/glob.js +65 -0
- package/src/pnpm/graph.js +26 -34
- package/src/pnpm/hoist.js +96 -59
- package/src/pnpm/hoisted.js +234 -0
- package/src/pnpm/hook.js +26 -31
- package/src/pnpm/inputs.js +177 -0
- package/src/pnpm/install.js +123 -124
- package/src/pnpm/local.js +95 -40
- package/src/pnpm/minimatch.js +319 -0
- package/src/pnpm/nm-hoist-graph.js +299 -0
- package/src/pnpm/nm-hoist.js +149 -0
- package/src/pnpm/npm-packlist.js +217 -0
- package/src/pnpm/overrides.js +11 -51
- package/src/pnpm/package.js +123 -0
- package/src/pnpm/packlist.js +176 -81
- package/src/pnpm/patches.js +18 -25
- package/src/pnpm/projects.js +132 -89
- package/src/pnpm/readers.js +122 -77
- package/src/pnpm/settings.js +74 -95
- package/src/pnpm/tree.js +216 -237
- package/src/pnpm/uptodate.js +16 -28
- package/src/pnpm/workspace.js +152 -126
- package/src/project.js +56 -0
- package/src/soldeer/config.js +63 -0
- package/src/soldeer/git.js +52 -0
- package/src/soldeer/inputs.js +30 -0
- package/src/soldeer/tree.js +83 -0
- package/src/soldeer/zip.js +121 -0
- package/src/tarball.js +123 -131
- package/src/yarn1/bins.js +28 -0
- package/src/yarn1/compat.js +67 -0
- package/src/yarn1/hoist.js +249 -0
- package/src/yarn1/inputs.js +83 -0
- package/src/yarn1/manifest.js +64 -0
- package/src/yarn1/package.js +206 -0
- package/src/yarn1/peers.js +51 -0
- package/src/yarn1/requests.js +74 -0
- package/src/yarn1/resolve.js +289 -0
- package/src/yarn1/settings.js +95 -0
- package/src/yarn1/tree.js +219 -0
- package/yarn1.d.ts +368 -0
- package/yarn1.js +7 -0
- package/src/pnpm/npmrc.js +0 -31
- /package/{LICENSE → LICENSE-MIT} +0 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/bin/compare.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// The two sides `deptree compare` sets beside each other, what is on disk
|
|
2
|
+
// and what deptree builds, each a map from a path under the folders an
|
|
3
|
+
// install makes to its type, mode, and bytes or link target; and how they
|
|
4
|
+
// differ.
|
|
5
|
+
|
|
6
|
+
import { Buffer } from 'node:buffer'
|
|
7
|
+
import { lstatSync, readFileSync, readdirSync, readlinkSync, statSync } from 'node:fs'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
import { diff } from '@preventive/diff'
|
|
10
|
+
import { compareNames, dirname, normalize } from '@preventive/vfs/path.js'
|
|
11
|
+
import { typeOf } from '../src/project.js'
|
|
12
|
+
|
|
13
|
+
const kindOf = (st) => (st.isSymbolicLink() ? 'symlink' : st.isDirectory() ? 'directory' : st.isFile() ? 'file' : 'other')
|
|
14
|
+
|
|
15
|
+
// The project's directory as deptree reads one, by paths from `/`, never
|
|
16
|
+
// above it, its names in code point order, as a Vfs lists them, and its
|
|
17
|
+
// modes with the setuid, setgid and sticky bits, as a Vfs keeps them.
|
|
18
|
+
export function projectView(root) {
|
|
19
|
+
const at = (path) => join(root, normalize(`/${path}`))
|
|
20
|
+
return {
|
|
21
|
+
readdir: (path) => readdirSync(at(path)).sort(compareNames),
|
|
22
|
+
lstat(path) {
|
|
23
|
+
const st = lstatSync(at(path))
|
|
24
|
+
return { type: kindOf(st), mode: st.mode & 0o7777 }
|
|
25
|
+
},
|
|
26
|
+
stat: (path) => ({ type: kindOf(statSync(at(path))) }),
|
|
27
|
+
readFile: (path) => readFileSync(at(path)),
|
|
28
|
+
readlink: (path) => readlinkSync(at(path)),
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// What deptree never builds: the .bin directories, each package manager's
|
|
33
|
+
// record of its install, and the .git of a dependency Soldeer clones.
|
|
34
|
+
const NOT_BUILT = /(?:^|\/)node_modules\/(?:\.bin|\.modules\.yaml|\.pnpm-workspace-state(?:-v\d+)?\.json|\.pnpm\/lock\.yaml|\.package-lock\.json|\.yarn-integrity)(?:\/|$)|^dependencies\/[^/]+\/\.git(?:\/|$)/u
|
|
35
|
+
|
|
36
|
+
// Everything under each of `dirs` as `view` has it, the disk's projectView
|
|
37
|
+
// or the tree's Vfs: links not followed, every file read whole, and a
|
|
38
|
+
// folder not there left out. A directory deptree never builds is one entry,
|
|
39
|
+
// not entered, as only it is ever listed.
|
|
40
|
+
export function readSide(view, dirs) {
|
|
41
|
+
const entries = new Map()
|
|
42
|
+
const stack = dirs.filter((dir) => typeOf(view, dir, false) !== undefined)
|
|
43
|
+
while (stack.length > 0) {
|
|
44
|
+
const path = stack.pop()
|
|
45
|
+
const { type, mode } = view.lstat(path)
|
|
46
|
+
const entry = { type, mode }
|
|
47
|
+
if (type === 'file') entry.data = view.readFile(path)
|
|
48
|
+
if (type === 'symlink') entry.target = view.readlink(path)
|
|
49
|
+
entries.set(path, entry)
|
|
50
|
+
if (type === 'directory' && !NOT_BUILT.test(path)) for (const name of view.readdir(path)) stack.push(`${path}/${name}`)
|
|
51
|
+
}
|
|
52
|
+
return entries
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// The paths on disk alone that hold nothing Node resolves: what deptree never
|
|
56
|
+
// builds, and directories of nothing else, as pnpm leaves a node_modules of a
|
|
57
|
+
// .bin of links to what it removed, a scope once its last package goes, or
|
|
58
|
+
// the hoisted linker's .pnpm of its state alone.
|
|
59
|
+
export function leftOut(changes, disk) {
|
|
60
|
+
const left = new Set(changes.filter(({ mark }) => mark === '-').map(({ path }) => path))
|
|
61
|
+
for (const [path, { type }] of disk) {
|
|
62
|
+
if (left.size === 0) break
|
|
63
|
+
if (type === 'directory' || NOT_BUILT.test(path)) continue
|
|
64
|
+
// Nothing listed is under another listed, so the first one up is its.
|
|
65
|
+
for (let at = path; at !== '.'; at = dirname(at)) {
|
|
66
|
+
if (left.delete(at)) break
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return left
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A package's directory in node_modules/.pnpm on disk alone, which pnpm keeps
|
|
73
|
+
// for a while after no install has it.
|
|
74
|
+
export const leftBehind = ({ mark, type, path }) => mark === '-' && type === 'directory' && /(?:^|\/)node_modules\/\.pnpm\/[^/]*@[^/]*$/u.test(path)
|
|
75
|
+
|
|
76
|
+
// Walk order, as Vfs.walk goes: a `/` sorts before anything a name can hold.
|
|
77
|
+
export const byPath = (a, b) => compareNames(a.replaceAll('/', '\0'), b.replaceAll('/', '\0'))
|
|
78
|
+
|
|
79
|
+
const octal = (mode) => mode.toString(8).padStart(3, '0')
|
|
80
|
+
|
|
81
|
+
function changeOf(disk, tree) {
|
|
82
|
+
if (disk === undefined) return { mark: '+', type: tree.type }
|
|
83
|
+
if (tree === undefined) return { mark: '-', type: disk.type }
|
|
84
|
+
if (disk.type !== tree.type) return { mark: '~', type: tree.type, what: `${disk.type} on disk, ${tree.type} in the tree` }
|
|
85
|
+
const what = []
|
|
86
|
+
const content = disk.type === 'file' && Buffer.compare(disk.data, tree.data) !== 0
|
|
87
|
+
if (disk.type === 'symlink' && disk.target !== tree.target) what.push(`link to ${disk.target} on disk, to ${tree.target} in the tree`)
|
|
88
|
+
if (content) what.push('content')
|
|
89
|
+
if (disk.type === 'file' && disk.mode !== tree.mode) what.push(`mode ${octal(disk.mode)} on disk, ${octal(tree.mode)} in the tree`)
|
|
90
|
+
return what.length === 0 ? undefined : { mark: '~', type: tree.type, what: what.join('; '), content }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// How the disk differs from the tree, in walk order. Nothing is listed under
|
|
94
|
+
// a path only one side has, or of another type on each, as only a directory
|
|
95
|
+
// has anything in it; a directory's own mode is the umask's, not compared.
|
|
96
|
+
export function difference(disk, tree) {
|
|
97
|
+
const changes = []
|
|
98
|
+
for (const path of new Set([...disk.keys(), ...tree.keys()])) {
|
|
99
|
+
if (disk.get(dirname(path))?.type !== tree.get(dirname(path))?.type) continue
|
|
100
|
+
const change = changeOf(disk.get(path), tree.get(path))
|
|
101
|
+
if (change !== undefined) changes.push({ path, ...change })
|
|
102
|
+
}
|
|
103
|
+
return changes.sort((a, b) => byPath(a.path, b.path))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Text is UTF-8 with no NUL in it, as diff takes a NUL for a binary file's.
|
|
107
|
+
const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
|
|
108
|
+
function textOf(bytes) {
|
|
109
|
+
if (bytes.includes(0)) return undefined
|
|
110
|
+
try {
|
|
111
|
+
return decoder.decode(bytes)
|
|
112
|
+
} catch {
|
|
113
|
+
return undefined
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// A name for patch and git apply to read back whole: in C quotes, as git
|
|
118
|
+
// writes one, where it holds a control character, a quote or a backslash,
|
|
119
|
+
// each such byte escaped; and, unlike git, where it holds a space, as patch
|
|
120
|
+
// strips one that ends a name from before the tab git puts after it.
|
|
121
|
+
const UNQUOTED = /[\p{Cc}"\\]/gu
|
|
122
|
+
const ESCAPES = { '\t': '\\t', '\n': '\\n', '"': '\\"', '\\': '\\\\' }
|
|
123
|
+
const octets = (char) => [...Buffer.from(char)].map((byte) => `\\${byte.toString(8).padStart(3, '0')}`).join('')
|
|
124
|
+
const cQuoted = (name) => (/[\p{Cc}"\\ ]/u.test(name) ? `"${name.replaceAll(UNQUOTED, (char) => ESCAPES[char] ?? octets(char))}"` : name)
|
|
125
|
+
|
|
126
|
+
// A unified diff from disk to the tree, labelled for `patch -p1`.
|
|
127
|
+
export function patchOf(path, disk, tree) {
|
|
128
|
+
const [from, to] = [cQuoted(`a/${path}`), cQuoted(`b/${path}`)]
|
|
129
|
+
const [a, b] = [textOf(disk), textOf(tree)]
|
|
130
|
+
if (a === undefined || b === undefined) return `Binary files ${from} and ${to} differ\n`
|
|
131
|
+
return `--- ${from}\n+++ ${to}\n${diff(a, b)}`
|
|
132
|
+
}
|
package/bin/deptree.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The deptree command: `compare` sets what a package manager installed
|
|
3
|
+
// beside the tree deptree builds; see USAGE.
|
|
4
|
+
|
|
5
|
+
import { resolve } from 'node:path'
|
|
6
|
+
import process from 'node:process'
|
|
7
|
+
import { parseArgs, styleText } from 'node:util'
|
|
8
|
+
import { diffLineStyles } from '@preventive/diff/color.js'
|
|
9
|
+
import { createClient } from '@preventive/upstream/github.js'
|
|
10
|
+
import { defaultCacheDir, setCacheDir } from '@preventive/upstream/npm.js'
|
|
11
|
+
import { join } from '@preventive/vfs/path.js'
|
|
12
|
+
import { buildCargoTree } from '../cargo.js'
|
|
13
|
+
import { buildNpmTree, findNpmWorkspaces } from '../npm.js'
|
|
14
|
+
import { DeptreeError, LockfileError, buildPnpmTree, findPnpmProjects } from '../pnpm.js'
|
|
15
|
+
import { TomlError, buildSoldeerTree } from '../soldeer.js'
|
|
16
|
+
import { buildYarn1Tree, findYarn1Workspaces } from '../yarn1.js'
|
|
17
|
+
import { escaped } from '../src/error.js'
|
|
18
|
+
import { typeOf } from '../src/project.js'
|
|
19
|
+
import { difference, leftBehind, leftOut, patchOf, projectView, readSide } from './compare.js'
|
|
20
|
+
import { userToken } from './npmrc.js'
|
|
21
|
+
|
|
22
|
+
const USAGE = `Usage: deptree compare [options] [<dir>]
|
|
23
|
+
|
|
24
|
+
Builds the tree the lockfile in <dir> (by default .) installs, from
|
|
25
|
+
pnpm-lock.yaml, package-lock.json, yarn.lock, soldeer.lock or Cargo.lock,
|
|
26
|
+
and lists where it differs from what is on disk, every node_modules of the
|
|
27
|
+
project, Soldeer's dependencies or cargo's vendor:
|
|
28
|
+
|
|
29
|
+
+ <path> only in the tree
|
|
30
|
+
- <path> only on disk
|
|
31
|
+
~ <path> in both, but of another type, content, mode or link target
|
|
32
|
+
|
|
33
|
+
A directory one side has alone is one line, ending in /. Left out unless
|
|
34
|
+
--all: .bin directories, state files, Soldeer's .git, and directories on
|
|
35
|
+
disk holding nothing else. Exits 0 where the two are the same, 1 where
|
|
36
|
+
they differ, 2 on trouble.
|
|
37
|
+
|
|
38
|
+
Tarballs, zips and crates are kept in ${defaultCacheDir ?? 'no cache, as no home directory is known'}.
|
|
39
|
+
A scoped package is fetched with NPM_TOKEN, else with a ~/.npmrc line of
|
|
40
|
+
nothing but //registry.npmjs.org/:_authToken=npm_…; a Soldeer git
|
|
41
|
+
dependency with GITHUB_TOKEN or GH_TOKEN, else anonymously.
|
|
42
|
+
|
|
43
|
+
--pnpm, --yarn, --npm, --soldeer, --cargo <version>
|
|
44
|
+
the one that installed: by default the one
|
|
45
|
+
packageManager pins, or Soldeer 0.12.0; npm's and
|
|
46
|
+
cargo's are needed
|
|
47
|
+
--node <version> the Node it installed with; by default this one
|
|
48
|
+
--metadata fetch each package's version document from npm's
|
|
49
|
+
registry too, and hold its tarball's dist to it
|
|
50
|
+
--all list what is left out too
|
|
51
|
+
--diff follow each file of other content with a unified diff,
|
|
52
|
+
for patch -p1 in <dir>
|
|
53
|
+
-h, --help show this message
|
|
54
|
+
`
|
|
55
|
+
|
|
56
|
+
const OPTIONS = {
|
|
57
|
+
pnpm: { type: 'string' },
|
|
58
|
+
yarn: { type: 'string' },
|
|
59
|
+
npm: { type: 'string' },
|
|
60
|
+
soldeer: { type: 'string' },
|
|
61
|
+
cargo: { type: 'string' },
|
|
62
|
+
node: { type: 'string' },
|
|
63
|
+
metadata: { type: 'boolean', default: false },
|
|
64
|
+
all: { type: 'boolean' },
|
|
65
|
+
diff: { type: 'boolean' },
|
|
66
|
+
help: { type: 'boolean', short: 'h' },
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Each package manager by its version flag: its lockfile, the folder it
|
|
70
|
+
// installs into in each project it finds, and the version where the flag is
|
|
71
|
+
// left out and nothing pins one, or, `unpinned`, that the flag is needed.
|
|
72
|
+
const MANAGERS = {
|
|
73
|
+
pnpm: { lockfile: 'pnpm-lock.yaml', folder: 'node_modules', projects: findPnpmProjects, build: buildPnpmTree },
|
|
74
|
+
yarn: { lockfile: 'yarn.lock', folder: 'node_modules', projects: findYarn1Workspaces, build: buildYarn1Tree },
|
|
75
|
+
npm: { lockfile: 'package-lock.json', folder: 'node_modules', unpinned: true, projects: findNpmWorkspaces, build: buildNpmTree },
|
|
76
|
+
soldeer: {
|
|
77
|
+
lockfile: 'soldeer.lock',
|
|
78
|
+
folder: 'dependencies',
|
|
79
|
+
version: '0.12.0',
|
|
80
|
+
projects: () => ['.'],
|
|
81
|
+
build: (options) => buildSoldeerTree({ ...options, github: createClient({ token: process.env.GITHUB_TOKEN || process.env.GH_TOKEN || null }) }),
|
|
82
|
+
},
|
|
83
|
+
cargo: { lockfile: 'Cargo.lock', folder: 'vendor', unpinned: true, projects: () => ['.'], build: buildCargoTree },
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function main(argv) {
|
|
87
|
+
const { values, positionals } = parseArgs({ args: argv, options: OPTIONS, allowPositionals: true })
|
|
88
|
+
if (values.help) {
|
|
89
|
+
process.stdout.write(USAGE)
|
|
90
|
+
return 0
|
|
91
|
+
}
|
|
92
|
+
const [command, dir = '.', ...rest] = positionals
|
|
93
|
+
if (command !== 'compare' || rest.length > 0) return fail(USAGE)
|
|
94
|
+
const project = projectView(resolve(dir))
|
|
95
|
+
const name = managerOf(project, dir, values)
|
|
96
|
+
const manager = MANAGERS[name]
|
|
97
|
+
if (defaultCacheDir !== undefined) setCacheDir()
|
|
98
|
+
// upstream reads it for each request, and sends it for a scoped package.
|
|
99
|
+
process.env.NPM_TOKEN ||= userToken() ?? ''
|
|
100
|
+
// This machine, which installed what is on disk; npm takes no libc where
|
|
101
|
+
// there is none to find.
|
|
102
|
+
const libc = libcOf()
|
|
103
|
+
const host = { [name]: values[name] ?? manager.version, node: values.node ?? process.versions.node, os: process.platform, cpu: process.arch, ...(name === 'npm' && libc === 'unknown' ? {} : { libc }) }
|
|
104
|
+
// The disk first, as it is before anything is fetched.
|
|
105
|
+
const dirs = manager.projects({ project, host, os: host.os }).map((at) => join(at, manager.folder))
|
|
106
|
+
const disk = readSide(project, dirs)
|
|
107
|
+
const files = [...disk.values()].filter((entry) => entry.type === 'file')
|
|
108
|
+
note(`${manager.folder} on disk: ${files.length} files, ${size(files.reduce((sum, file) => sum + file.data.length, 0))}`)
|
|
109
|
+
const tree = await manager.build({ project, host, metadata: values.metadata })
|
|
110
|
+
note(`${manager.lockfile}: ${tree.stats.files} files, ${size(tree.stats.bytes)} built`)
|
|
111
|
+
const built = readSide(tree.vfs, dirs)
|
|
112
|
+
const changes = difference(disk, built)
|
|
113
|
+
const left = values.all ? new Set() : leftOut(changes, disk)
|
|
114
|
+
const listed = changes.filter((change) => !left.has(change.path))
|
|
115
|
+
const patch = ({ path, content }) => (values.diff && content ? painted(patchOf(path, disk.get(path).data, built.get(path).data)) : '')
|
|
116
|
+
process.stdout.write(listed.map((change) => line(change) + patch(change)).join(''))
|
|
117
|
+
note(summary(listed, left.size))
|
|
118
|
+
if (name === 'pnpm') pruneHint(listed)
|
|
119
|
+
return listed.length === 0 ? 0 : 1
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// The one a version flag names, or else the one whose lockfile is there.
|
|
123
|
+
function managerOf(project, dir, values) {
|
|
124
|
+
const names = Object.keys(MANAGERS)
|
|
125
|
+
const named = names.filter((name) => values[name] !== undefined)
|
|
126
|
+
const [name, ...others] = named.length > 0 ? named : names.filter((n) => typeOf(project, MANAGERS[n].lockfile) === 'file')
|
|
127
|
+
if (name === undefined) fail(`deptree: ${dir}: no ${names.map((n) => MANAGERS[n].lockfile).join(', ')}\n`)
|
|
128
|
+
if (others.length > 0) fail(`deptree: ${dir}: give one of --${[name, ...others].join(', --')}, for the package manager that installed\n`)
|
|
129
|
+
if (MANAGERS[name].unpinned && values[name] === undefined) fail(`deptree: ${MANAGERS[name].lockfile}: give --${name} <version>, the ${name} that installed, which no file pins\n`)
|
|
130
|
+
return name
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// As pnpm's detect-libc tells it, from Node's report, which leaves out the
|
|
134
|
+
// network, whose reverse DNS can stall.
|
|
135
|
+
function libcOf() {
|
|
136
|
+
if (process.platform !== 'linux') return 'unknown'
|
|
137
|
+
process.report.excludeNetwork = true
|
|
138
|
+
const { header, sharedObjects } = process.report.getReport()
|
|
139
|
+
if (header.glibcVersionRuntime) return 'glibc'
|
|
140
|
+
return sharedObjects.some((file) => file.includes('libc.musl-') || file.includes('ld-musl-')) ? 'musl' : 'unknown'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const MARKS = { '+': 'green', '-': 'red', '~': 'yellow' }
|
|
144
|
+
|
|
145
|
+
// A path is whatever a tarball spells: escaped, so that no name can act on a
|
|
146
|
+
// terminal or break a line, a backslash doubled first so an escape reads as one.
|
|
147
|
+
const shown = (text) => escaped(text.replaceAll('\\', '\\\\'))
|
|
148
|
+
|
|
149
|
+
function line({ mark, path, type, what }) {
|
|
150
|
+
const slash = mark !== '~' && type === 'directory' ? '/' : ''
|
|
151
|
+
const why = what === undefined ? '' : ` (${shown(what)})`
|
|
152
|
+
return `${styleText(MARKS[mark], `${mark} ${shown(path)}${slash}`, { stream: process.stdout })}${why}\n`
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// On a terminal, a diff's lines escaped as names are, but for tabs and
|
|
156
|
+
// backslashes, and painted; elsewhere byte for byte, for patch to apply.
|
|
157
|
+
function painted(patch) {
|
|
158
|
+
if (!process.stdout.isTTY) return patch
|
|
159
|
+
const styles = diffLineStyles(patch)
|
|
160
|
+
return patch.split('\n').map((text, i) => {
|
|
161
|
+
const plain = text.split('\t').map(escaped).join('\t')
|
|
162
|
+
return styles?.[i] ? styleText(styles[i], plain, { stream: process.stdout }) : plain
|
|
163
|
+
}).join('\n')
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function summary(changes, left) {
|
|
167
|
+
const count = (mark) => changes.filter((change) => change.mark === mark).length
|
|
168
|
+
const counts = changes.length === 0 ? 'the same' : `${count('+')} only in the tree, ${count('-')} only on disk, ${count('~')} different`
|
|
169
|
+
return left === 0 ? counts : `${counts}; ${left} left out (--all lists them)`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function pruneHint(changes) {
|
|
173
|
+
const count = changes.filter(leftBehind).length
|
|
174
|
+
if (count === 0) return
|
|
175
|
+
const [what, them] = count === 1 ? ['a package', 'it'] : [`${count} packages`, 'them']
|
|
176
|
+
note(`${what} in node_modules/.pnpm that the lockfile no longer installs, as pnpm keeps for a while: \`pnpm prune\` removes ${them}, and so does every install with modulesCacheMaxAge: 0 in pnpm-workspace.yaml`)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const UNITS = ['B', 'KiB', 'MiB', 'GiB']
|
|
180
|
+
function size(bytes) {
|
|
181
|
+
const unit = Math.max(0, UNITS.findLastIndex((_, i) => bytes >= 1024 ** i))
|
|
182
|
+
return unit === 0 ? `${bytes} B` : `${(bytes / 1024 ** unit).toFixed(1)} ${UNITS[unit]}`
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// A refusal says what and where: deptree's or its lockfile reader's, the
|
|
186
|
+
// system's on a call it names, parseArgs's of an option, or a host deptree
|
|
187
|
+
// takes no version of from what is there. Anything else is a bug, and shows
|
|
188
|
+
// its stack, though Node gave it a code.
|
|
189
|
+
const REFUSALS = [DeptreeError, LockfileError, TomlError]
|
|
190
|
+
const refused = (error) => REFUSALS.some((type) => error instanceof type) || typeof error?.syscall === 'string' || String(error?.code).startsWith('ERR_PARSE_ARGS_') || (error instanceof TypeError && error.message.startsWith('host.'))
|
|
191
|
+
function describe(error) {
|
|
192
|
+
if (!refused(error)) return error?.stack ?? String(error)
|
|
193
|
+
const lines = [error.message]
|
|
194
|
+
for (let cause = error.cause; cause instanceof Error; cause = cause.cause) {
|
|
195
|
+
if (!lines.some((text) => text.includes(cause.message))) lines.push(` ${cause.message}`)
|
|
196
|
+
}
|
|
197
|
+
return lines.join('\n')
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const note = (text) => process.stderr.write(`${text}\n`)
|
|
201
|
+
const fail = (message) => {
|
|
202
|
+
process.stderr.write(message)
|
|
203
|
+
process.exit(2)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
process.exitCode = await main(process.argv.slice(2))
|
|
208
|
+
} catch (error) {
|
|
209
|
+
note(`deptree: ${describe(error)}`)
|
|
210
|
+
process.exitCode = 2
|
|
211
|
+
}
|
package/bin/npmrc.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The npm registry's token from ~/.npmrc, for bin/deptree.js alone: the
|
|
2
|
+
// library reads no .npmrc but the project's.
|
|
3
|
+
|
|
4
|
+
import { readFileSync } from 'node:fs'
|
|
5
|
+
import { homedir } from 'node:os'
|
|
6
|
+
import { isAbsolute, join } from 'node:path'
|
|
7
|
+
|
|
8
|
+
// A line of nothing but the registry's token, as `npm login` writes it.
|
|
9
|
+
const TOKEN_LINE = /^\/\/registry\.npmjs\.org\/:_authToken=(npm_[\dA-Za-z]+)$/u
|
|
10
|
+
|
|
11
|
+
// The last such line's, as npm takes the last of a key.
|
|
12
|
+
export const tokenIn = (text) => text.split(/\r?\n/u).map((line) => TOKEN_LINE.exec(line)?.[1]).findLast((token) => token !== undefined)
|
|
13
|
+
|
|
14
|
+
export function userToken(home = homedir()) {
|
|
15
|
+
if (!isAbsolute(home)) return undefined
|
|
16
|
+
try {
|
|
17
|
+
return tokenIn(readFileSync(join(home, '.npmrc'), 'utf8'))
|
|
18
|
+
} catch {
|
|
19
|
+
return undefined
|
|
20
|
+
}
|
|
21
|
+
}
|