安装我的包时如何复制 JSON 配置文件
How to copy a JSON config file when installing my package
我正在创建一个自定义 npm 包,我想添加一个 JSON 配置文件。
我想在 npm install mypackage
完成后将此文件添加到节点应用程序的根路径中
我知道这是可能的,因为这就是打字稿正在做的事情,当我做 npm install typescript
时,在我的应用程序文件夹中添加了一个 tsconfig.json 文件
我猜这是一个 package.json 配置,但我不知道是哪个。
谢谢
您可以使用 postinstall
脚本实现此目的。
{ "scripts" :
...
"postinstall" : "scripts/postinstall.js"
}
}
这是文档中的 explanation of npm lifecycle events:
For example, if your package.json contains this:
{ "scripts" :
{ "install" : "scripts/install.js"
, "postinstall" : "scripts/install.js"
, "uninstall" : "scripts/uninstall.js"
}
}
then scripts/install.js will be called for the install and post-install stages of the lifecycle, and scripts/uninstall.js will be called when the package is uninstalled. Since scripts/install.js is running for two different phases, it would be wise in this case to look at the npm_lifecycle_event environment variable.
我使用 genechk awnser 解决了这个问题:
我必须向我的 package.json 添加“安装后”脚本,但我还必须在执行脚本之前添加“节点”命令:
"scripts" : [
...
"postinstall" : "node scripts/postinstall.js"
]
我还必须将脚本添加到包中交付的文件中:
"files": [
...
"scripts/**/*"
]
Finlay 我的 postinstall.js 看起来像这样:
const fs = require('fs');
// __dirname is "{ProjectFolder}/node_modules/mypackage/config"
const CONFIG_SRC = `${__dirname}/../config/`;
const CONFIG_DEST = `${__dirname}/../../../`;
const CONFIG_MYPACKAGE = 'mypackageconfig.json';
fs.copyFile(`${CONFIG_SRC}/${CONFIG_MYPACKAGE}`, `${CONFIG_DEST}/${CONFIG_MYPACKAGE}`, (err) => {
if (err) throw err;
console.log(`File ${CONFIG_MYPACKAGE} copied`);
});
另外,为了完全正确,我必须使用遵循相同逻辑并删除文件的 uninstall.js 脚本
我正在创建一个自定义 npm 包,我想添加一个 JSON 配置文件。
我想在 npm install mypackage
完成后将此文件添加到节点应用程序的根路径中
我知道这是可能的,因为这就是打字稿正在做的事情,当我做 npm install typescript
我猜这是一个 package.json 配置,但我不知道是哪个。
谢谢
您可以使用 postinstall
脚本实现此目的。
{ "scripts" :
...
"postinstall" : "scripts/postinstall.js"
}
}
这是文档中的 explanation of npm lifecycle events:
For example, if your package.json contains this:
{ "scripts" : { "install" : "scripts/install.js" , "postinstall" : "scripts/install.js" , "uninstall" : "scripts/uninstall.js" } }
then scripts/install.js will be called for the install and post-install stages of the lifecycle, and scripts/uninstall.js will be called when the package is uninstalled. Since scripts/install.js is running for two different phases, it would be wise in this case to look at the npm_lifecycle_event environment variable.
我使用 genechk awnser 解决了这个问题:
我必须向我的 package.json 添加“安装后”脚本,但我还必须在执行脚本之前添加“节点”命令:
"scripts" : [
...
"postinstall" : "node scripts/postinstall.js"
]
我还必须将脚本添加到包中交付的文件中:
"files": [
...
"scripts/**/*"
]
Finlay 我的 postinstall.js 看起来像这样:
const fs = require('fs');
// __dirname is "{ProjectFolder}/node_modules/mypackage/config"
const CONFIG_SRC = `${__dirname}/../config/`;
const CONFIG_DEST = `${__dirname}/../../../`;
const CONFIG_MYPACKAGE = 'mypackageconfig.json';
fs.copyFile(`${CONFIG_SRC}/${CONFIG_MYPACKAGE}`, `${CONFIG_DEST}/${CONFIG_MYPACKAGE}`, (err) => {
if (err) throw err;
console.log(`File ${CONFIG_MYPACKAGE} copied`);
});
另外,为了完全正确,我必须使用遵循相同逻辑并删除文件的 uninstall.js 脚本