如何将npm命令与shell命令连接起来

时间:2019-08-28 08:42:03

标签: javascript git shell npm

我有一个提供提交哈希值的shell脚本,我想运行npm version并希望与哈希值连接。

因此,将shell命令放入js文件并与node一起运行是可行的。由于命令很短,所以我想内联运行。

这有效

npm --no-git-tag-version version $(node bump.js)

但想一行运行

npm --no-git-tag-version version + git rev-parse --short HEAD

这是我的bump.js文件

const shell = require('shelljs');
const { version } = require('../package.json');

if (!shell.which('git')) {
  shell.echo('Sorry, this script requires git');
  shell.exit(1);
}

// npm --no-git-tag-version version $(node config/bump.js)
// Ex: npm --no-git-tag-version version  "10.1.6-develop-80a3053"

let commitHash = '';
if (commitHash = shell.exec('git rev-parse --short HEAD', { silent: true })) {
  // Getting the base version from package.json
  const baseVersion = version.match(/[\d+]{1,}\.[\d+]{1,}\.[\d+]{1,}/)[0];

  // Concatenating base-version with develop-commit-hash
  const hashedVersion = `${baseVersion}-develop-${commitHash}`;
  shell.echo(`npm --no-git-tag-version version ${hashedVersion}`, { silent: true });

}

shell.exit(0);

0 个答案:

没有答案