gpt4 book ai didi

java - 我应该在 NPM 包中的什么位置配置代码生成?

转载 作者:行者123 更新时间:2023-11-30 11:10:38 24 4
gpt4 key购买 nike

免责声明:我是 Jsonix 的作者和 Jsonix Schema Compiler我正在尝试确定 Jsonix Schema Compiler 应该集成到 NPM package.json 中的规范方式。

jsonix-schema-compiler NPM 包提供了一个基于 Java 的代码生成工具。如果 jsonix-schema-compiler 作为依赖项安装,则它可用于生成 XML<->JS 映射。调用类似于:

java -jar node_modules/jsonix-schema-compiler/lib/jsonix-schema-compiler-full.jar
schema.xsd

这会生成一个类似 Mappings.js 的 JavaScript 文件,它基本上是模块代码的一部分。

理想情况下,上面的 jsonix-schema-compiler 调用(java -jar ... 等等)应该在模块构建期间执行。但它必须在 安装模块依赖项之后执行(否则 node_modules/jsonix-schema-compiler 将丢失)。

我的问题是 - 我应该在哪里规范地配置 NPM 包中的代码生成?

现在我正在 postinstall 脚本中执行此操作,例如:

{
...
"dependencies": {
"jsonix": "x.x.x",
"jsonix-schema-compiler": "x.x.x"
},
"devDependencies" : {
"nodeunit" : "~0.8.6"
},
"scripts": {
"postinstall" : "java -jar node_modules/jsonix-schema-compiler/lib/jsonix-schema-compiler-full.jar schema.xsd",
"test": "nodeunit src/test/javascript/tests.js"
}
}

然而阅读了this :

tl;dr Don't use install. Use a .gyp file for compilation, and prepublish for anything else.

You should almost never have to explicitly set a preinstall or install script. If you are doing this, please consider if there is another option.

我现在很困惑 postinstall 是否也可以。

我想要做的就是能够在安装依赖项之后但在其他事情(如测试或发布)之前执行某个命令行命令。我应该如何规范地做到这一点?

最佳答案

通常人们会在构建步骤中运行诸如 coffeescript-to-javascript 编译器、Ecmascript 6->5 转译器和压缩器之类的东西,这听起来就像你在做的那样。

预发布和安装后的区别在于预发布脚本可能会在您 checkout 的目录中运行,因此可以合理地假设 java 可用并且各种开发依赖可用;而安装后脚本将在每次安装后运行,如果 java(等)不可用,它将失败,就像在极简主义的 docker 图像上一样。因此,您应该将构建步骤放在预发布或类似脚本中。

我个人喜欢在 package.json 中定义脚本“mypublish”以确保所有测试通过、运行构建、确保构建工件存在,然后运行 ​​npm publish。我发现这比 prepublish 更直观,后者旨在用作“我即将发布”的 Hook ,而不是“在发布前进行构建”。

这是一个使用此设置的 package.json:https://github.com/reid/node-jslint/blob/master/package.json这是带有 prepublish 目标的 Makefile:https://github.com/reid/node-jslint/blob/master/Makefile

如果您有更多问题,请告诉我;我有点漫无边际,因为有许多合法的方法可以完成它——只要您避免使用 postinstall 脚本。 ;-)

关于java - 我应该在 NPM 包中的什么位置配置代码生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27649842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com