codekingpro/portable-devtools
114k
1// Alternate versions of different classes that we use for isolated mode
2const CaseInsensitiveMap = require('./case-insensitive-map.js')
3const { resolve } = require('node:path')
4
5// fake lib/inventory.js
6class IsolatedInventory extends Map {
7 query () {
8 return []
9 }
10}
11
12// fake lib/node.js
13class IsolatedNode {
14 binPaths = []
15 children = new CaseInsensitiveMap()
16 edgesIn = new Set()
17 edgesOut = new CaseInsensitiveMap()
18 fsChildren = new Set()
19 hasShrinkwrap = false
20 integrity = null
21 inventory = new IsolatedInventory()
22 isInStore = false
23 linksIn = new Set()
24 meta = { loadedFromDisk: false }
25 optional = false
26 parent = null
27 root = null
28 tops = new Set()
29 workspaces = new Map()
30
31 constructor (options) {
32 this.location = options.location
33 this.name = options.name
34 this.package = options.package
35 this.path = options.path
36 this.realpath = !this.isLink ? this.path : resolve(options.realpath)
37
38 if (options.parent) {
39 this.parent = options.parent
40 }
41 if (options.resolved) {
42 this.resolved = options.resolved
43 }
44 if (options.root) {
45 this.root = options.root
46 }
47 if (options.isInStore) {
48 this.isInStore = true
49 }
50 if (options.optional) {
51 this.optional = true
52 }
53 }
54
55 get isRoot () {
56 return this === this.root
57 }
58
59 // The idealGraph is where this is set to true
60 get isProjectRoot () {
61 return false
62 }
63
64 get inDepBundle () {
65 return false
66 }
67
68 get isLink () {
69 return false
70 }
71
72 get isTop () {
73 return !this.parent
74 }
75
76 /* istanbul ignore next -- emulate lib/node.js */
77 get global () {
78 return false
79 }
80
81 get globalTop () {
82 return false
83 }
84
85 /* istanbul ignore next -- emulate lib/node.js */
86 set target (t) {
87 // nop
88 // In the real lib/node.js this throws in debug mode
89 }
90
91 get target () {
92 return this
93 }
94
95 /* istanbul ignore next -- emulate lib/node.js */
96 getBundler () {
97 return null
98 }
99
100 /* istanbul ignore next -- emulate lib/node.js */
101 get hasInstallScript () {
102 const { hasInstallScript, scripts } = this.package
103 const { install, preinstall, postinstall } = scripts || {}
104 return !!(hasInstallScript || install || preinstall || postinstall)
105 }
106
107 get version () {
108 return this.package.version
109 }
110}
111
112// fake lib/link.js
113class IsolatedLink extends IsolatedNode {
114 #target
115 isStoreLink = false
116
117 constructor (options) {
118 super(options)
119 this.#target = options.target
120 if (options.isStoreLink) {
121 this.isStoreLink = true
122 }
123 }
124
125 get isLink () {
126 return true
127 }
128
129 set target (t) {
130 this.#target = t
131 }
132
133 get target () {
134 return this.#target
135 }
136}
137
138module.exports = { IsolatedNode, IsolatedLink }
139 