Welcome to mirror list, hosted at ThFree Co, Russian Federation.

set-script.js « lib - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 185c4cc901d2eb718b7dc587d1b980f58dd58166 (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
const { resolve } = require('path')
const log = require('npmlog')
const rpj = require('read-package-json-fast')
const PackageJson = require('@npmcli/package-json')

const BaseCommand = require('./base-command.js')
class SetScript extends BaseCommand {
  /* istanbul ignore next - see test/lib/load-all-commands.js */
  static get description () {
    return 'Set tasks in the scripts section of package.json'
  }

  /* istanbul ignore next - see test/lib/load-all-commands.js */
  static get params () {
    return ['workspace', 'workspaces', 'include-workspace-root']
  }

  /* istanbul ignore next - see test/lib/load-all-commands.js */
  static get name () {
    return 'set-script'
  }

  /* istanbul ignore next - see test/lib/load-all-commands.js */
  static get usage () {
    return ['[<script>] [<command>]']
  }

  async completion (opts) {
    const argv = opts.conf.argv.remain
    if (argv.length === 2) {
      // find the script name
      const json = resolve(this.npm.localPrefix, 'package.json')
      const { scripts = {} } = await rpj(json).catch(er => ({}))
      return Object.keys(scripts)
    }
  }

  validate (args) {
    if (process.env.npm_lifecycle_event === 'postinstall')
      throw new Error('Scripts can’t set from the postinstall script')

    // Parse arguments
    if (args.length !== 2)
      throw new Error(`Expected 2 arguments: got ${args.length}`)
  }

  exec (args, cb) {
    this.setScript(args).then(() => cb()).catch(cb)
  }

  async setScript (args) {
    this.validate(args)
    const warn = await this.doSetScript(this.npm.localPrefix, args[0], args[1])
    if (warn)
      log.warn('set-script', `Script "${args[0]}" was overwritten`)
  }

  execWorkspaces (args, filters, cb) {
    this.setScriptWorkspaces(args, filters).then(() => cb()).catch(cb)
  }

  async setScriptWorkspaces (args, filters) {
    this.validate(args)
    await this.setWorkspaces(filters)

    for (const [name, path] of this.workspaces) {
      try {
        const warn = await this.doSetScript(path, args[0], args[1])
        if (warn) {
          log.warn('set-script', `Script "${args[0]}" was overwritten`)
          log.warn(`  in workspace: ${name}`)
          log.warn(`  at location: ${path}`)
        }
      } catch (err) {
        log.error('set-script', err.message)
        log.error(`  in workspace: ${name}`)
        log.error(`  at location: ${path}`)
        process.exitCode = 1
      }
    }
  }

  // returns a Boolean that will be true if
  // the requested script was overwritten
  // and false if it was set as a new script
  async doSetScript (path, name, value) {
    let warn = false

    const pkgJson = await PackageJson.load(path)
    const { scripts } = pkgJson.content

    const overwriting =
      scripts
        && scripts[name]
        && scripts[name] !== value

    if (overwriting)
      warn = true

    pkgJson.update({
      scripts: {
        ...scripts,
        [name]: value,
      },
    })

    await pkgJson.save()

    return warn
  }
}
module.exports = SetScript