VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /proc/self/root/usr/local/lib/node_modules/npm/lib/commands/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/self/root/usr/local/lib/node_modules/npm/lib/commands/set-script.js
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}`)
  }

  async exec (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`)
  }

  async execWorkspaces (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

VaKeR 2022