Skip to content

Commit

Permalink
wip: added test/lib/pkg.js, proper ws support
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Jul 2, 2021
1 parent 4b00c0f commit e4e480e
Show file tree
Hide file tree
Showing 3 changed files with 612 additions and 13 deletions.
21 changes: 20 additions & 1 deletion docs/content/commands/npm-pkg.md
Expand Up @@ -126,10 +126,29 @@ You can set/get/delete items across your configured workspaces by using the
For example, setting a `funding` value across all configured workspaces
of a project:
```
```bash
npm pkg set funding=https://example.com --ws
```
When using `npm pkg get` to retrieve info from your configured workspaces, the
returned result will be in a json format in which top level keys are the
names of each workspace, the values of these keys will be the result values
returned from each of the configured workspaces, e.g:
```
npm pkg get name version --ws
{
"a": {
"name": "a",
"version": "1.0.0"
},
"b": {
"name": "b",
"version": "1.0.0"
}
}
```
### Configuration
<!-- AUTOGENERATED CONFIG DESCRIPTIONS START -->
Expand Down
34 changes: 22 additions & 12 deletions lib/pkg.js
Expand Up @@ -32,13 +32,6 @@ class Pkg extends BaseCommand {
}

exec (args, cb) {
if (this.npm.config.get('global')) {
throw Object.assign(
new Error(`There's no package.json file to manage on global mode`),
{ code: 'EPKGGLOBAL' }
)
}

this.prefix = this.npm.localPrefix
this.pkg(args).then(() => cb()).catch(cb)
}
Expand All @@ -48,6 +41,13 @@ class Pkg extends BaseCommand {
}

async pkg (args) {
if (this.npm.config.get('global')) {
throw Object.assign(
new Error(`There's no package.json file to manage on global mode`),
{ code: 'EPKGGLOBAL' }
)
}

const [cmd, ..._args] = args
switch (cmd) {
case 'get':
Expand All @@ -63,10 +63,14 @@ class Pkg extends BaseCommand {

async pkgWorkspaces (args, filters) {
await this.setWorkspaces(filters)
for (const workspacePath of this.workspacePaths) {
const result = {}
for (const [workspaceName, workspacePath] of this.workspaces.entries()) {
this.prefix = workspacePath
await this.pkg(args)
result[workspaceName] = await this.pkg(args)
}
// when running in workspaces names, make sure to key by workspace
// name the results of each value retrieved in each ws
this.npm.output(JSON.stringify(result, null, 2))
}

async get (args) {
Expand All @@ -85,7 +89,13 @@ class Pkg extends BaseCommand {
result = result[args]
}

this.npm.output(JSON.stringify(result, null, 2))
// only outputs if not running with workspaces config,
// in case you're retrieving info for workspaces the pkgWorkspaces
// will handle the output to make sure it get keyed by ws name
if (!this.workspaces)
this.npm.output(JSON.stringify(result, null, 2))

return result
}

async set (args) {
Expand All @@ -95,7 +105,7 @@ class Pkg extends BaseCommand {
{ code: 'EPKGSET' }
)

if (!args)
if (!args.length)
throw setError()

const force = this.npm.config.get('force')
Expand All @@ -122,7 +132,7 @@ class Pkg extends BaseCommand {
{ code: 'EPKGDELETE' }
)

if (!args)
if (!args.length)
throw setError()

const pkgJson = await PackageJson.load(this.prefix)
Expand Down

0 comments on commit e4e480e

Please sign in to comment.