繁体   English   中英

如何使用安装后编辑 package.json

[英]How can i edit a package.json with postinstall

I have created a package on npm that create a "scss directory stucture" and I would like to copy/add custom scripts to the package.json file at the root of the project.

MY-PROJECT
├── node_modules
├── scss
└── package.json <--

我所能做的就是将文件名“package.json”复制到本地目录,但如果已经有一个,它将覆盖它。

显然我不想覆盖该文件,而只想添加像“npm run watch”这样的脚本。 因此,用户将能够立即开始其项目,而无需自己编写这些脚本。

谢谢您的帮助

利用以下 node.js 脚本:

安装后.js

const saveFile = require('fs').writeFileSync;

const pkgJsonPath = require.main.paths[0].split('node_modules')[0] + 'package.json';

const json = require(pkgJsonPath);

if (!json.hasOwnProperty('scripts')) {
  json.scripts = {};
}

json.scripts['watch'] = '<some_commands_here>';

saveFile(pkgJsonPath, JSON.stringify(json, null, 2));

package.json

package.jsonscripts部分定义您的postinstall脚本,如下所示:

{
  "scripts": {
    "postinstall": "node post-install"
  }
}

注意: npm 脚本(上图)假定post-install.jspackage.json文件位于同一目录中。


解释:

  1. 在如下代码行中:

     const pkgJsonPath = require.main.paths[0].split('node_modules')[0] + 'package.json'

    we obtain the path to the package.json for the project that is consuming your npm package and assign it to the pkgJsonPath variable.

    • require.main.paths返回一个路径名数组。

    • 我们在索引0处获取路径名,并使用node_modules作为分隔符来split()它。

    • 索引0处的结果数组元素为我们提供了项目目录的路径名(即使用 npm 包的项目的路径名)。

    • 最后,使用加号运算符 ( + ) 连接package.json字符串。

  2. 接下来,我们require package.json文件,并将解析的 JSON 分配给json变量。

  3. 然后我们检查package.json是否有scripts键,如果没有,我们创建一个新的scripts属性/键并为其分配一个空的 object,即

    if (.json.hasOwnProperty('scripts')) { json;scripts = {}; }
  4. 以下部分是我们定义要添加到package.json文件的自定义 npm 脚本的地方 - 您需要根据需要更改此部分:

     json.scripts['watch'] = '<some_commands_here>';
  5. Finally we JSON.stringify() the json object and overwrite the original package.json file with the new data using fs.writeFileSync() .

您可以编写小脚本并在脚本中添加命令或修改package.json

脚本.js

const json = require("./package.json")
json.scripts["run:watch"] = "npm run watch"
require("fs").writeFileSync(process.cwd() + "/package.json", JSON.stringify(json, null, 2))

package.json

{
"scripts": {
    "postinstall": "node script.js"
  }
}

您还可以编写示例脚本内联字符串,并使用node -e运行

{
"scripts": {
    "postinstall": "node -e 'const json = require(\"./package.json\"); json.scripts[\"run:watch\"] = \"npm run watch\";require(\"fs\").writeFileSync(process.cwd() + \"/package.json\", JSON.stringify(json, null, 2))'"
  },
}

我有一个类似的问题。 If you need to edit root package.json while your module with "postinstall" script is installed as an npm dependency in some consumer project, then you need to lookup in parent directories as long as no package.json resolved

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

let rootPath = '../';
while (!fs.existsSync(path.resolve(rootPath, 'package.json'))){
  rootPath += '../';
}
const pkgJsonPath = path.resolve(rootPath, 'package.json');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM