| 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 |