MCPcopy Index your code
hub / github.com/nodejs/node / setWorkspace

Method setWorkspace

deps/npm/lib/commands/init.js:184–211  ·  view source on GitHub ↗
(pkg, workspacePath)

Source from the content-addressed store, hash-verified

182 }
183
184 async setWorkspace (pkg, workspacePath) {
185 const workspaces = await mapWorkspaces({ cwd: this.npm.localPrefix, pkg })
186
187 // skip setting workspace if current package.json glob already satisfies it
188 for (const wPath of workspaces.values()) {
189 if (wPath === workspacePath) {
190 return
191 }
192 }
193
194 // if a create-pkg didn't generate a package.json at the workspace folder level, it might not be recognized as a workspace by mapWorkspaces, so we're just going to avoid touching the top-level package.json
195 try {
196 statSync(resolve(workspacePath, 'package.json'))
197 } catch {
198 return
199 }
200
201 const pkgJson = await PackageJson.load(this.npm.localPrefix)
202
203 pkgJson.update({
204 workspaces: [
205 ...(pkgJson.content.workspaces || []),
206 posixPath(relative(this.npm.localPrefix, workspacePath)),
207 ],
208 })
209
210 await pkgJson.save()
211 }
212
213 async update (workspacesPaths) {
214 // translate workspaces paths into an array containing workspaces names

Callers 1

execWorkspacesMethod · 0.95

Calls 8

posixPathFunction · 0.85
relativeFunction · 0.85
updateMethod · 0.65
statSyncFunction · 0.50
resolveFunction · 0.50
valuesMethod · 0.45
loadMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected