如何从node.js以编程方式运行`yarn tag`? [英] How to run `yarn tag` programmatically from node.js?

查看:28
本文介绍了如何从node.js以编程方式运行`yarn tag`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 node.js 以编程方式运行 yarn 命令,但找不到任何 sdk 或 cli 实用程序.唯一的事情是产生一个新进程,但这很麻烦......

I'd like to run yarn commands programmatically from node.js, but can't find any sdk or cli utility. The only thing is to spawn a new process, but that's hacky...

推荐答案

截至 2019 年 1 月,Yarn 没有可以直接调用的 API.您不能需要 Yarn 并使用类似于 npm 的 yarn 命令

As of January 2019, Yarn does not have an API that you can call directly. You cannot require Yarn and use yarn commands similar to npm

var npm = require('npm');
npm.load(function(err) {
  // handle errors

  // install module ffi
  npm.commands.install(['ffi'], function(er, data) {
    // log errors or data
  });

只能使用node的child_process来执行yarn命令.

You can only use node's child_process to execute the yarn command.

const { exec } = require('child_process');
exec('yarn add package@beta', (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }

  // the *entire* stdout and stderr (buffered)
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

这篇关于如何从node.js以编程方式运行`yarn tag`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆