package-script

2019-08-14 admin

package-script是什么

什么是package-script,Spawn external process in additional to the package install

package-script使用教程帮助文档

package-script

Add a cross OS nodejs call to your package.json script section for spawning an external commands. e.g. install bower or grunt that are global packages as part of your module.

It’s obvious that you can use this package for running multiple synchronized commands by configuration.

Note: In some cases, if you can somehow use global packages as a module e.g. require(“bower”) instead of using it globaly and avoid installing it in your package script it would be a best practice. But sometimes it’s inevetable…

Blog

Some more information NodeJS Corner blog

How To

First add a script section to your package.json file (optional)

Add a script section to your package.json file. You can set the script to run in any phase according to the package.json docs

 "scripts": {
    "install": "node installer.js"
  }

Then, in your javascript file:

require('package-script').spawn([
      {
          command: "npm",
          args: ["install", "-g", "grunt-cli"]
      },
      {
          command: "npm",
          args: ["install", "-g", "bower"]
      }

  ]);

Or check the install/uninstall methods

require('package-script').install([
        {
            admin:false,
            name: "q"
        }
    ], {callback: function() {
        // callback functionality in here
     });

Note: The install and uninstall methods have a validation process for checking if the package already installed/uninstalled

version 0.0.7

  • depth and debug attributes were added to the optional arguments of the install/uninstall methods
  • js.utils dependency upgrade (npm list info, improved)
  • local Object utils removed, using js.utils instead
  • MAC is now supported

version 0.0.5

  • issue #1 fixed - Checking if packages already exist See install and uninstall methods, also look into js.utils package (NPM)
  • Install and UnInstalled methods were added
  • callback support for the global methods added

version 0.0.4

  • issue #2 fixed - switching log off
  • Initial configuration added, it can be set via the spawn method or using a separate init method

version 0.0.3

  • An admin configuration property that enables(default)/disables the admin cli prompt
  • A spawnopt configuration property that is the spawn options

Reference

  • init(config)

    • config {Object} The initial configuration
      • log: Switching the logger off/on optional values: [true/false]
  • spawn(config, init, callback)

    • config {Array} List of Objects to launch Launches one or more processes according to the given configuration:
      • command {String} The command to run
      • args {Array} List of string arguments
      • spawnopt {Object} The spawn options
      • admin {boolean} The boolean flag for running the command as administrator (Linux support only for now)
    • init {Object} The initial configuration
      • log: Switching the logger off/on optional values: [true/false]
    • callback {Function} The callback functionality
  • install(config, opt)

    • config {Array} List of packages configuration to be installed

      • name {String} The package name
      • spawnopt {Object} The spawn options
      • admin {boolean} The boolean flag for running the command as administrator (Linux support only for now)
    • opt {Object} The optional configuration

      • init {Object} The initial configuration
        • global {Boolean} specify if the given packages are global or not
        • log: Switching the logger off/on optional values: [true/false]
      • debug {Number}
        • 0: set debug to off (default)
        • 1: set debug to on
      • depth {String} The npm tree info dependencies hierarchy depth (see ‘npm list’ for more info)
      • callback {Function} The callback functionality
  • uninstall(config, opt)

    • config {Array} List of packages configuration to be installed

      • name {String} The package name
      • spawnopt {Object} The spawn options
      • admin {boolean} The boolean flag for running the command as administrator (Linux support only for now)
    • opt {Object} The optional configuration

      • init {Object} The initial configuration
        • global {Boolean} specify if the given packages are global or not
        • log: Switching the logger off/on optional values: [true/false]
      • debug {Number}
        • 0: set debug to off (default)
        • 1: set debug to on
      • depth {String} The npm tree info dependencies hierarchy depth (see ‘npm list’ for more info)
      • callback {Function} The callback functionality

See child_process.spawn for more information

Troubleshooting

Npm install failed with “cannot run in wd”

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-72434.html

文章标题:package-script

回到顶部