简体   繁体   English

使用 libnpm 以编程方式打包和发布 NPM package

[英]Programmatically pack and publish NPM package with libnpm

I'd like to publish to NPM within my CI/build system, so I found libnpmpublish which seems to be the correct tool, but explicitly states that it doesn't pack your code into a tarball, although the publish API requires that you pass it a tarball (as compared to, say, a folder or a path).我想在我的 CI/构建系统中发布到 NPM,所以我发现libnpmpublish这似乎是正确的工具,但明确指出它不会将您的代码打包到 tarball 中,尽管publish API 要求您通过它是一个 tarball(与文件夹或路径相比)。

Their suggested solution is他们建议的解决方案是

Since libnpmpublish does not generate tarballs itself, one way to build your own tarball for publishing is to do npm pack in the directory you wish to pack.由于libnpmpublish本身不会生成 tarball,因此构建自己的 tarball 以进行发布的一种方法是在要打包的目录中执行 npm 打包。 You can then fs.createReadStream('my-proj-1.0.0.tgz' ) and pass that to libnpmpublish , along with require('./package.json') .然后,您可以fs.createReadStream('my-proj-1.0.0.tgz' ) 并将其与require('./package.json')一起传递给libnpmpublish

Is there a programmatic (in Node) way to script this process?是否有编程(在 Node 中)方式来编写此过程的脚本? I looked around the NPM repositories and couldn't find a package that is dedicated to packaging, though I can find this code which seems to implement packing, but is in an archived repository- namely, it's not in libnpm .我查看了 NPM 存储库,找不到专用于打包的 package ,尽管我可以找到似乎实现打包的代码,但在存档的存储库中,即它不在libnpm中。

The closest I can find is npm-packlist which, when given a folder, creates a list of files which can be forwarded to the NPM tar package, as demonstrated in the README for npm-packlist .我能找到的最接近的是npm-packlist ,当给定一个文件夹时,它会创建一个文件列表,这些文件可以转发到 NPM tar package,如npm-packlist的自述文件中所示。

This is a bit of a hack using the command line, but it works for a similar concept where I'm creating and uploading a package via a REST command.这有点使用命令行的技巧,但它适用于类似的概念,我通过 REST 命令创建和上传 package。 I wrap in a promise that returns a stream so that I can easily add that to the formPost data:我包装在一个 promise 中,它返回一个 stream 以便我可以轻松地将其添加到 formPost 数据中:

const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
    exec(`npm pack ${t}`, { cwd: d }, (error, stdout, stderr) => {
        if (error) {
            console.error(error);
            reject(error);
        }
        var f = d + p.sep + stdout.trim();
        if (debug) console.log(`zip file "${f}" created.`);
        resolve(fs.createReadStream(f))
    });
});

Enjoy!享受!

there is now libnpmpack , which will generate a tarball buffer in exactly the format that libnpmpublish expects, so this can be done with:现在有libnpmpack ,它将按照libnpmpublish期望的格式生成一个 tarball 缓冲区,因此可以通过以下方式完成:

const pack = require('libnpmpack')
const { publish } = require('libnpmpublish')

async function packAndPublish(packagePath) {
  // readPackageJson omitted for brevity
  const manifest = readPackageJson(packagePath)

  const tarball = await pack(packagePath)
  return publish(manifest, tarball)
}

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

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