簡體   English   中英

在 package.json 中運行另一個 yarn/npm 任務,不指定 yarn 或 npm

[英]Run another yarn/npm task within a package.json, without specifying yarn or npm

我的 package.json 中有一個任務“deploy”,需要先調用“build”。 我已經這樣指定了:

"deploy": "紗線運行構建;./deploy.sh",

問題是這個硬編碼yarn作為包管理器。 所以如果有人不使用yarn ,它就行不通。 切換到npm會導致類似的問題。

在不npmyarn的選擇的同時實現這一目標的好方法是什么?

一種簡單的方法是使用npm-run-all包,其文檔說明:

紗線相容性

如果使用 Yarn 調用腳本,npm-run-all 將正確使用 Yarn 執行計划的子腳本。

所以你可以這樣做:

"predeploy": "run-s build",
"deploy": "./deploy.sh",

predeploy步驟將使用 npm 或 yarn,具體取決於您調用deploy任務的方式。

我認為讓 package.json 中的運行保持與包管理器無關是很好的,這樣它們就不會綁定到特定的包管理器,但在一個項目中,同意使用單個包管理器可能是謹慎的,所以你沒有處理沖突的鎖文件。

這可能並不理想,但您可以在項目根目錄下運行.js文件來進行這些檢查...

您可以在您的項目根目錄創建一個名為yarnpm.js (或其他)的文件,並在您的package.json deploy命令中調用該文件。

// package.json (trimmed)
"scripts": {
  "deploy": "node yarnpm",
  "build": "whatever build command you use"
},
// yarnpm.js
const fs = require('fs');

const FILE_NAME = process.argv[1].replace(/^.*[\\\/]/, '');

// Command you wish to run with `{{}}` in place of `npm` or `yarn'
// This would allow you to easily run multiple `npm`/`yarn` commands without much work
// For example, `{{}} run one && {{}} run two
const COMMAND_TO_RUN = '{{}} run build; ./deploy.sh';

try {
  if (fs.existsSync('./package-lock.json')) {  // Check for `npm`
    execute(COMMAND_TO_RUN.replace('{{}}', 'npm'));
  } else if (fs.existsSync('./yarn.lock')) {   // Check for `yarn`
    execute(COMMAND_TO_RUN.replace('{{}}', 'yarn'));
  } else {
    console.log('\x1b[33m', `[${FILE_NAME}] Unable to locate either npm or yarn!`, '\033[0m');
  }
} catch (err) {
  console.log('\x1b[31m', `[${FILE_NAME}] Unable to deploy!`, '\033[0m');
}

function execute(command) { // Helper function, to make running `exec` easier
  require('child_process').exec(command,
    (error, stdout, stderr) => {
      if (error) {
        console.log(`error: ${error.message}`);
        return;
      }
      if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
      }
      console.log(stdout);
    });
}

希望這在某種程度上有所幫助! 干杯。


編輯:

...或者如果你想參數化yarnpm.js腳本,使其易於重用,並將所有“命令”保存在package.json文件中,你可以做這樣的事情..

// package.json (trimmed, parameterized)
"scripts": {
    "deploy": "node yarnpm '{{}} run build; ./deploy.sh'",
    "build": "node build.js"
},
// yarnpm.js (parameterized)
const COMMAND_TO_RUN = process.argv[2]; // Technically, the first 'parameter' is the third index
const FILE_NAME = process.argv[1].replace(/^.*[\\\/]/, '');

if (COMMAND_TO_RUN) {
  const fs = require('fs');

  try {
    if (fs.existsSync('./package-lock.json')) {  // Check for `npm`
      execute(COMMAND_TO_RUN.replace('{{}}', 'npm'));
    } else if (fs.existsSync('./yarn.lock')) {   // Check for `yarn`
      execute(COMMAND_TO_RUN.replace('{{}}', 'yarn'));
    } else {
      console.log('\x1b[33m', `[${FILE_NAME}] Unable to locate either npm or yarn!`, '\033[0m');
    }
  } catch (err) {
    console.log('\x1b[31m', `[${FILE_NAME}] Unable to deploy!`, '\033[0m');
  }

  function execute(command) { // Helper function, to make running `exec` easier
    require('child_process').exec(command,
      (error, stdout, stderr) => {
        if (error) {
          console.log(`error: ${error.message}`);
          return;
        }
        if (stderr) {
          console.log(`stderr: ${stderr}`);
          return;
        }
        console.log(stdout);
      });
  }
} else {
  console.log('\x1b[31m', `[${FILE_NAME}] Requires a single argument!`, '\033[0m')
}

如果在運行前檢查怎么辦?

您可以創建一個名為build.sh的新文件,其內容如下:

# check if current user installed node environment, if not, auto install it.
if command -v node >/dev/null 2>&1; then
    echo "version of node: $(node -v)"
    echo "version of npm: $(npm -v)"
else
    # auto install node environment, suppose platform is centos, 
    # need change this part to apply other platform.
    curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -
    yum -y install nodejs
fi

npm run build

那么你的腳本將是:

{
  "deploy": "./build.sh && ./deploy.sh"
}

所以我想我有一個更簡單的解決方案:

"deploy": "yarn run build || npm run build; ./deploy.sh",

它唯一真正的缺點是在存在紗線的情況下,但構建失敗,然后npm run build也會發生。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM