summaryrefslogtreecommitdiff
path: root/deps/npm/lib/commands/pkg.js
blob: 3a8e01f65bc92188933f6beb1fb492d33d79a81f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const PackageJson = require('@npmcli/package-json')
const BaseCommand = require('../base-command.js')
const Queryable = require('../utils/queryable.js')

class Pkg extends BaseCommand {
  static description = 'Manages your package.json'
  static name = 'pkg'
  static usage = [
    'set <key>=<value> [<key>=<value> ...]',
    'get [<key> [<key> ...]]',
    'delete <key> [<key> ...]',
    'set [<array>[<index>].<key>=<value> ...]',
    'set [<array>[].<key>=<value> ...]',
  ]

  static params = [
    'force',
    'json',
    'workspace',
    'workspaces',
  ]

  static ignoreImplicitWorkspace = false

  async exec (args, { prefix } = {}) {
    if (!prefix) {
      this.prefix = this.npm.localPrefix
    } else {
      this.prefix = prefix
    }

    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':
        return this.get(_args)
      case 'set':
        return this.set(_args)
      case 'delete':
        return this.delete(_args)
      default:
        throw this.usageError()
    }
  }

  async execWorkspaces (args, filters) {
    await this.setWorkspaces(filters)
    const result = {}
    for (const [workspaceName, workspacePath] of this.workspaces.entries()) {
      this.prefix = workspacePath
      result[workspaceName] = await this.exec(args, { prefix: workspacePath })
    }
    // 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) {
    const pkgJson = await PackageJson.load(this.prefix)

    const { content } = pkgJson
    let result = !args.length && content

    if (!result) {
      const q = new Queryable(content)
      result = q.query(args)

      // in case there's only a single result from the query
      // just prints that one element to stdout
      if (Object.keys(result).length === 1) {
        result = result[args]
      }
    }

    // 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) {
    const setError = () =>
      this.usageError('npm pkg set expects a key=value pair of args.')

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

    const force = this.npm.config.get('force')
    const json = this.npm.config.get('json')
    const pkgJson = await PackageJson.load(this.prefix)
    const q = new Queryable(pkgJson.content)
    for (const arg of args) {
      const [key, ...rest] = arg.split('=')
      const value = rest.join('=')
      if (!key || !value) {
        throw setError()
      }

      q.set(key, json ? JSON.parse(value) : value, { force })
    }

    pkgJson.update(q.toJSON())
    await pkgJson.save()
  }

  async delete (args) {
    const setError = () =>
      this.usageError('npm pkg delete expects key args.')

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

    const pkgJson = await PackageJson.load(this.prefix)
    const q = new Queryable(pkgJson.content)
    for (const key of args) {
      if (!key) {
        throw setError()
      }

      q.delete(key)
    }

    pkgJson.update(q.toJSON())
    await pkgJson.save()
  }
}

module.exports = Pkg