npm发布-从package.json中删除脚本? [英] npm publish - removing scripts from 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
.
这可行吗?还是我应该考虑另一种方式?"
有几个内置功能,称为前钩和后钩可以用来满足您的要求,尽管这是一种相当自定义的方式.相关的钩子是prepublish
和postpublish
,并在文档中进行了如下描述;
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.
解决方案的概要是:
-
在项目
package.json
中使用prepublish
脚本来调用自定义 nodejs 脚本.该 nodejs 脚本执行以下操作:
Utilize a
prepublish
script in your projectspackage.json
to invoke a custom nodejs script. This nodejs script performs the following:
- 读取原始的
package.json
数据并将其缓存. - 从
package.json
的scripts
部分中删除特定的脚本/键. - 将修订后的数据写回到原始的
package.json
.
- Reads the original
package.json
data and caches it. - Removes specific scripts/keys from the
scripts
section ofpackage.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.
代码示例/要点.
-
以下 nodejs 脚本将执行上面第一点中提到的任务.我们将其命名为
cleanse-pkg.js
.
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;
});
实施和用法.
-
prepublish
和postpublish
脚本在项目package.json
中定义如下:
The
prepublish
andpostpublish
scripts are defined in the projectspackage.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"
},
...
}
-
请注意
prepublish
脚本中的\"a,b,prepublish,postpublish\"
部分.这定义了传递给cleanse-pkg.js
的参数(即,它列出了要在发布前删除的每个脚本的名称).每个要删除的命名脚本都必须是;作为单个字符串提供,用逗号分隔,并且不能包含空格.Note the
\"a,b,prepublish,postpublish\"
part in theprepublish
script. This defines the argument to pass tocleanse-pkg.js
(i.e. it lists the names of each script to be removed before publishing). Each named script to be removed must be; provided as a single string, be separated with commas, and must not include spaces.cleanse-pkg.js
和restore-pkg.js
都位于名为.scripts
的隐藏文件夹中,该文件夹本身位于项目目录的顶层(即与项目package.json
处于同一级别).可以根据需要重新放置两个 nodejs 脚本,并根据需要在各自的 npm-script .Both
cleanse-pkg.js
andrestore-pkg.js
reside in a hidden folder named.scripts
, which itself resides at the top level of the projects directory, (i.e. at the same level as the projectspackage.json
). Both nodejs scripts can be relocated as preferred, and the paths to them redefined as necessary in the respective npm-script .鉴于上面的人为设计的
package.json
,在最终发布的tarball中实际的package.json
内容如下:Given the contrived
package.json
above, the actualpackage.json
contents in the resultant published tarball will be as follows:结果/已发布的package.json
Resultant/published package.json
{ ... "scripts": { "keep": ... }, ... }
-
这次我们假设原始的
package.json
如下:
devDependencies部分中的软件包.
也许项目
package.json
的devDependencies
部分中列出了一些软件包,您也希望将它们从已发布的package.json
中删除.
Packages in the devDependencies section.
Maybe there are packages listed in the
devDependencies
section of your projectspackage.json
that you also want removed in the publishedpackage.json
.(注意: devDependencies 部分中列出的所有软件包都是用户通过npm-registry安装时未下载).
(Note: Any packages listed in the devDependencies section are not downloaded when the user installs via the npm-registry though).
但是,也许您还是想删除它们.如果这是必需的,则
cleanse-pkg.js
还接受可选的第二个参数.此参数类似于第一个参数,由此必须从devDependencies
节中删除的每个命名包都必须是:作为单个字符串提供,用逗号分隔,并且不能包含空格.However, perhaps you'd like to remove them anyway. If that's a requirement, then
cleanse-pkg.js
also accepts an optional second argument. This argument is analogous to the first argument, whereby each named package to be removed from thedevDependencies
section must be; provided as a single string, be separated with commas, and must not include spaces.设计原始package.json
Contrived original package.json
{ ... "scripts": { "keep": ... , "a": ... , "b": ... , "prepublish": "node .scripts/cleanse-pkg \"a,b,prepublish,postpublish\" \"x,z\"", "postpublish": "node .scripts/restore-pkg" }, "devDependencies": { "x": "^1.0.2", "y": "^0.8.1", "z": "^0.8.1" }, ... }
- 请注意在
prepublish
脚本中添加的第二个\"x,z\"
自变量,用于指定要从devDependecies
部分中忽略的软件包. - Note the additional second
\"x,z\"
argument added to theprepublish
script to specify which packages to omit from thedevDependecies
section.
这一次,考虑到上面的人为作法的
package.json
,在最终发布的tarball中实际的package.json
内容将如下所示:This time, given the contrived
package.json
above, the actualpackage.json
contents in the resultant published tarball will be as follows:结果/已发布的package.json
Resultant/published package.json
{ ... "scripts": { "keep": ... }, "devDependencies": { "y": "^0.8.1" }, ... }
- 在项目目录中,不带任何参数.例如.
npm publish
. - 指定项目
package.json
的路径.例如.npm publish path/to/package.json
. - From within the project directory without any argument(s). E.g.
npm publish
. - Specifying a the path to your projects
package.json
. E.g.npm publish path/to/package.json
. To prevent both utility nodejs scripts, (
cleanse-pkg.js
andrestore-pkg.js
), from being published they should be added to your projects .npmignore file. Given the location of both these files explained above you can add a.scripts
entry in.npmignore
.- 原始
package.json
文件的绝对路径. - 应该写入临时
cached-package.json
文件的绝对路径.目前,此文件已从项目目录上一级上/下保存,以避免被发布.) - An absolute path to the original
package.json
file. - An absolute path to where the temporary
cached-package.json
file should be written. Currently this is saved one-level-up/outside from the project directory to avoid it being published).
正在跑步
此解决方案假定 npm发布将使用以下方法之一运行:
Running
This solution assumes npm publish will be run utlizing one of the following methods:
通过提供gzip压缩tar归档文件的url或文件路径来工作,该压缩文件只包含一个包含package.json
文件的单个文件夹.
This will not work by providing a url or file path to a gzipped tar archive containing a single folder with a package.json
file inside.
两个实用程序 nodejs 脚本cleanse-pkg.js
和restore-pkg.js
均定义:
Both utility nodejs scripts, cleanse-pkg.js
and restore-pkg.js
, define:
如果您选择将cleanse-pkg.js
和restore-pkg.js
存储到与上述位置不同的位置,则需要根据需要在两个文件中重新定义以下代码段中的路径.
If you choose to store cleanse-pkg.js
and restore-pkg.js
to a different location than the one described above, the paths in following code snippet will need to redefined as necessary in both files.
const ORIG_PKG_PATH = path.resolve(__dirname, '../package.json');
const CACHED_PKG_PATH = path.resolve(__dirname, '../../cached-package.json');
这篇关于npm发布-从package.json中删除脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!