diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2021-03-07 20:13:58 +0300 |
---|---|---|
committer | Gar <gar+gh@danger.computer> | 2021-03-22 20:22:38 +0300 |
commit | e1b3b318f095a7e1a7cc4b131907de4955275d9d (patch) | |
tree | cb2cf7f6fa70a6eb546431f10bbe8124febe1db5 /lib/workspaces | |
parent | b876442241b9d366a0541714bbee1ae50d6746fd (diff) |
feat: add exec workspaces
Add workspaces support to `npm exec`
- Refactored logic to read and filter workspaces into
`lib/workspaces/get-workspaces.js`
- Added location context message when entering interactive
shell using `npm exec` (with no args)
- Add ability to execute a package in the context of each
configured workspace
Fixes: https://github.com/npm/statusboard/issues/288
PR-URL: https://github.com/npm/cli/pull/2886
Credit: @ruyadorno
Close: #2886
Reviewed-by: @wraithgar
Diffstat (limited to 'lib/workspaces')
-rw-r--r-- | lib/workspaces/get-workspaces.js | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/workspaces/get-workspaces.js b/lib/workspaces/get-workspaces.js new file mode 100644 index 000000000..64812d540 --- /dev/null +++ b/lib/workspaces/get-workspaces.js @@ -0,0 +1,33 @@ +const { resolve } = require('path') +const mapWorkspaces = require('@npmcli/map-workspaces') +const minimatch = require('minimatch') +const rpj = require('read-package-json-fast') + +const getWorkspaces = async (filters, { path }) => { + const pkg = await rpj(resolve(path, 'package.json')) + const workspaces = await mapWorkspaces({ cwd: path, pkg }) + const res = filters.length ? new Map() : workspaces + + for (const filterArg of filters) { + for (const [workspaceName, workspacePath] of workspaces.entries()) { + if (filterArg === workspaceName + || resolve(path, filterArg) === workspacePath + || minimatch(workspacePath, `${resolve(path, filterArg)}/*`)) + res.set(workspaceName, workspacePath) + } + } + + if (!res.size) { + let msg = '!' + if (filters.length) { + msg = `:\n ${filters.reduce( + (res, filterArg) => `${res} --workspace=${filterArg}`, '')}` + } + + throw new Error(`No workspaces found${msg}`) + } + + return res +} + +module.exports = getWorkspaces |