npm发布-从package.json中删除脚本? [英] npm publish - removing scripts from package.json?

查看:111
本文介绍了npm发布-从package.json中删除脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布我的脚本之前,我在package.json下有许多脚本来编译coffeescript,typescript和仅限开发人员的命令-一旦发布它们就没有意义了.

Before publishing my script, I have a number of scripts under the package.json to compile coffeescript, typescript and developer only commands - which make no sense once it's published.

我想知道在package.json下是否有删除某些脚本的程序?考虑到发布包时,它还会发布package.json.

I was wondering if there is a procedure for removing certain scripts under the package.json ? Considering that when publishing your package, it also publishes package.json.

在发布程序包之前,是否可以删除脚本?

Would this be possible to kind of remove scripts before publishing the package?

发布脚本后,我会删除很多打字稿和coffeescript源文件(因为它们已经被编译),所以我没有构建的脚本对于已发布的软件包毫无意义.

Once I publish my script, I remove a lot of the typescript and coffeescript source files (as they have been compiled), so a script I have for building no makes no sense for the published package.

这可行吗?还是我应该考虑另一种方式?

Is this feasible? Or should I be thinking about another way?

推荐答案

简短答案.

是否可以在发布程序包之前先删除脚本?"

npm不包含从package.json删除脚本的内置功能.<​​/p>


答案很长.

npm does not include a built-in feature to remove scripts from package.json.

这可行吗?还是我应该考虑另一种方式?"

有几个内置功能,称为前钩和后钩可以用来满足您的要求,尽管这是一种相当自定义的方式.相关的钩子是prepublishpostpublish,并在文档中进行了如下描述;

There are a couple of built-in features known as Pre and Post hooks which can be utilized to meet your requirement, albeit in a rather custom way. The pertinent hooks are prepublish and postpublish and are described in the documentation as follows;

预发布:在打包和发布该程序包之前,以及在本地npm install上运行而无需任何参数...

prepublish: Run BEFORE the package is packed and published, as well as on local npm install without any arguments...

后发布:在程序包发布后运行.

postpublish: Run AFTER the package is published.

解决方案的概要是:

  1. 在项目package.json中使用prepublish脚本来调用自定义 nodejs 脚本.该 nodejs 脚本执行以下操作:

  1. Utilize a prepublish script in your projects package.json to invoke a custom nodejs script. This nodejs script performs the following:

  • 读取原始的package.json数据并将其缓存.
  • package.jsonscripts部分中删除特定的脚本/键.
  • 将修订后的数据写回到原始的package.json.
  • Reads the original package.json data and caches it.
  • Removes specific scripts/keys from the scripts section of package.json.
  • Writes the revised data back to the original package.json.

在项目package.json中使用postpublish脚本来调用另一个自定义的 nodejs 脚本.此辅助 nodejs 脚本执行以下操作:

Utilize a postpublish script in your projects package.json to invoke another custom nodejs script. This secondary nodejs script performs the following:

  • package.json的内容恢复为原始状态.
  • Reverts the content of package.json back to it's original state.


代码示例/要点.

  1. 以下 nodejs 脚本将执行上面第一点中提到的任务.我们将其命名为cleanse-pkg.js.

  1. The following nodejs script will carry out the tasks mentioned in point one above. Let's name it cleanse-pkg.js.

cleanse-pkg.js

cleanse-pkg.js

const fs = require('fs');
const path = require('path');

// Define absolute paths for original pkg and temporary pkg.
const ORIG_PKG_PATH = path.resolve(__dirname, '../package.json');
const CACHED_PKG_PATH = path.resolve(__dirname, '../../cached-package.json');

// Obtain original `package.json` contents.
const pkgData = require(ORIG_PKG_PATH);

if (process.argv.length <= 2) {
  throw new Error('Missing npm scripts key/name argument(s)');
}

// Get list of arguments passed to script.
const scriptsToRemove = process.argv[2].split(',');
const devDepsToRemove = process.argv[3] ? process.argv[3].split(',') : [];

// Write/cache the original `package.json` data to `cached-package.json` file.
fs.writeFile(CACHED_PKG_PATH, JSON.stringify(pkgData), function (err) {
  if (err) throw err;
});

// Remove the specified named scripts from the scripts section.
scriptsToRemove.forEach(function (scriptName) {
  delete pkgData.scripts[scriptName];
});

// Remove the specified named pkgs from the devDependencies section.
devDepsToRemove.forEach(function (pkgName) {
  delete pkgData.devDependencies[pkgName];
});

// Overwrite original `package.json` with new data (i.e. minus the specific data).
fs.writeFile(ORIG_PKG_PATH, JSON.stringify(pkgData, null, 2), function (err) {
  if (err) throw err;
});

  • 以下第二个 nodejs 脚本将执行上面第二点提到的任务.我们将此命名为restore-pkg.js.

  • The following secondary nodejs script will carry out the task mentioned in point two above. Let's name this one restore-pkg.js.

    restore-pkg.js

    restore-pkg.js

    const fs = require('fs');
    const path = require('path');
    
    // Define absolute paths for original pkg and temporary pkg.
    const ORIG_PKG_PATH = path.resolve(__dirname, '../package.json');
    const CACHED_PKG_PATH = path.resolve(__dirname, '../../cached-package.json');
    
    // Obtain original/cached contents from `cached-package.json`.
    const pkgData = JSON.stringify(require(CACHED_PKG_PATH), null, 2) + '\n';
    
    // Write data from `cached-package.json` back to original `package.json`.
    fs.writeFile(ORIG_PKG_PATH, pkgData, function (err) {
      if (err) throw err;
    });
    
    // Delete the temporary `cached-package.json` file.
    fs.unlink(CACHED_PKG_PATH, function (err) {
      if (err) throw err;
    });
    


  • 实施和用法.

    1. prepublishpostpublish脚本在项目package.json中定义如下:

    1. The prepublish and postpublish scripts are defined in the projects package.json as follows:

    设计原始package.json

    Contrived original package.json

    {
      ...
      "scripts": {
        "keep": ... ,
        "a": ... ,
        "b": ... ,
        "prepublish": "node .scripts/cleanse-pkg \"a,b,prepublish,postpublish\"",
        "postpublish": "node .scripts/restore-pkg"
      },
      ...
    }
    

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆