【问题标题】:npm publish and git repo workflownpm 发布和 git repo 工作流程
【发布时间】:2017-05-13 06:06:03
【问题描述】:

我在为我们的 npm repo publish 和 git 工作流程设置工作流程时有些吃力。我已经实现了如下的解决方案,我对此并不满意。

Developer --> 准备推送到 git --> npm 版本(我有一个脚本用 changelog 更新 README.md -- 使用不支持 changelog 的 sinopia) --> 提交和推送 --> 接受并合并 --> git ci 管道 --> 构建、测试并发布到 npm repo。

我的问题是,如果开发人员忘记对 repo 进行版本控制,那么管道就会失败。我可能可以在我的 git 管道中创建一个暂存区域,其中将包含更新的 README 并将 repo 版本化为 git CI 的一部分。但这感觉不对,有几个原因。主要是我宁愿不要通过对源文件的动态更改来污染 GIT。

所以,总结一下。有没有更好的办法?理想情况下,当更改准备好在 GIT 中接受时,我想对 repo 进行版本控制。但我不确定如何。顺便说一句,我们正在使用 gitlab。

【问题讨论】:

  • 可能会添加一个预发布脚本来对 repo 进行版本控制,但是 package.json 将反映更改并且需要提交给 GIt。在我看来,它就像鸡和蛋。任何帮助将不胜感激

标签: git npm version-control continuous-integration gitlab


【解决方案1】:

您可以使用pre-commit hook自动更新文件的版本(假设文件为package.json)。

pre-commit 钩子会在提交更改之前执行,因此您可以通过 shell 脚本获取当前版本,然后使用增量版本替换当前版本。然后 git commit 也会用新版本提交package.json

假设package.json文件格式如下(版本在第三行):

{
    "name": "Hello World",
    "version": 0.0.1,

版本格式为major.minor.patch。次要版本和补丁版本的最大值为 9(您可以指定其他值)。自动增加版本号的shell脚本如下:

#!/bin/sh

if [ -n "$(git status --porcelain)" ]; then 
  echo "there has uncommited changes"; 
else 
{
  echo "no changes to commit!";
  exit
}
fi

line=$(sed '3!d' package.json)
IFS=: read -r var1 var2 <<< "$line"
v=$(echo "$var2" | tr -d '"')
version=$(echo "$v" | tr -d ',')
IFS=. read -r major minor patch <<< "$version"

if  [ $patch != 9 ]
then 
  patch=$((patch+1))
elif [ $minor != 9 ]
then
{
  minor=$((minor+1))
  patch=0
}
else
{
  major=$((major+1))
  minor=0
  patch=0
}
fi

newversion=$major"."$minor"."$patch
echo  "The new version is $newversion"

placestr=$(echo $var1: \"$newversion\",)
sed -i "s/${line}/${placestr}/" package.json
git add package.json

【讨论】:

  • 谢谢玛丽娜。我不清楚“增量版本替换当前版本” - 你是什么意思?我测试了预提交并添加了一个脚本来解析和更新 package.json 中的版本。在提交发生之前一切正常。然后提交只添加更改之前暂存区域中的内容。即,如果此时我开始 git commit -a ..... 并且版本是 1.0.0 - 然后提交。预提交后版本为 1.0.1 - 这未提交。为此,我必须再次提交。所以我永远赶不上。你能详细说明一下吗?
  • 我在回答中添加了详细脚本,请试一试。
  • 谢谢!。最后一个命令是我所缺少的。我在这里发布我的脚本版本。
【解决方案2】:

const fs = require('fs');
const path = require('path');
let execSync = require('child_process').execSync;


let streamToString = (stream, callback) => {
  let str = '';
  stream.on('data', function(chunk) {
    str += chunk;
  });
  stream.on('end', function() {
    callback(str);
  });
};

const run = cmd => {
  return execSync(cmd, function(error, stdout) {

    streamToString(stdout, (data) => {
      console.log('Data ---- ', data);
      return data;
    });
  });
};

const packagejson = fs.readFileSync(
  path.join(__dirname, '..', 'package.json'), 'utf8');

run("cp package.json package.json.bkp");
const json = JSON.parse(packagejson);


// Update package.json

let semver = json.version.split('.');

semver[2] = Number(semver[2]) + 1;

let replacer = /,/gi;
json.version = semver.toString().replace(replacer, '.');

let changelog = {};

changelog.version = json.version;
changelog.author = run('npm config get init.author.name');

fs.writeFileSync(path.join(__dirname, '..', 'package.json'), JSON.stringify(json, null, 2));

if (changelog.author.byteLength <= 1) {
  throw Error('init.author.name is a required npm configuration attribute......');
}
run('cp README.md README.bkp');

const readme = fs.readFileSync(
  path.join(__dirname, '..', 'README.md'), 'utf8');

let header = readme.split('-----------');

console.log('Header ......... ', header);

fs.writeFileSync(
  path.join(__dirname, '..', 'README.md'), header[0] + '-----------' +
  '\r\nVersion ' + changelog.version + '\r\n' + 'Changed By ' + changelog.author + '\r\n' +
  'Change Date ' + new Date(), 'utf8'
);

run('git add package.json');
run('git add README.md');

【讨论】:

  • 我必须像你一样增强 semver。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 2011-09-23
  • 2012-01-14
  • 1970-01-01
  • 2011-03-02
  • 2019-06-08
相关资源
最近更新 更多