How to run `yarn tag` programmatically from node.js?

对着背影说爱祢 提交于 2020-04-16 03:56:26

问题


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...


回答1:


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
  });

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}`);
});


来源:https://stackoverflow.com/questions/54250231/how-to-run-yarn-tag-programmatically-from-node-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!