npm2git/npm2git.sh

49 lines
1.5 KiB
Bash
Raw Normal View History

2018-10-19 16:14:17 +00:00
#!/bin/sh
# git ready
2018-10-04 01:50:00 +00:00
set -e
C=; O=; S=; case "$1" in c) C=1 ;; cs) C=1; S=1 ;; o) O=1 ;; os) O=1; S=1 ;; *) echo "Usage: $0 c | cs | o | os
c = tag release as child of HEAD
o = tag release as orphan
s = tag HEAD as vX.X.X-src"; exit 1 ;; esac
2018-10-08 05:05:01 +00:00
cd "$(git rev-parse --show-toplevel)"
ORIG_HEAD="$(git symbolic-ref HEAD)"
2018-10-04 15:24:42 +00:00
ORIG_COMMIT="$(git rev-parse HEAD)"
2018-10-04 23:58:05 +00:00
# extract version from package.json, and remove scripts.prepare
2018-10-19 16:14:17 +00:00
cp package.json package.json.bak
2018-10-04 23:58:05 +00:00
PKG_VERSION="$(node -e '
2018-10-19 16:14:17 +00:00
const fs = require("fs");
2018-12-09 00:59:06 +00:00
const str = fs.readFileSync("package.json").toString();
const m = str.match(/\n([\t ]+)/);
const indent = m ? m[1] : "\t";
const pkg = JSON.parse(str);
2018-10-08 18:23:10 +00:00
pkg.scripts && delete pkg.scripts.prepare;
2018-12-09 00:59:06 +00:00
fs.writeFileSync("package.json", JSON.stringify(pkg, null, indent) + (str.endsWith("\n") ? "\n" : ""));
2018-10-04 23:58:05 +00:00
console.log(pkg.version);
')"
2018-10-04 01:50:00 +00:00
# create new temporary branch
2018-10-19 16:14:17 +00:00
TEMP_BRANCH="NPM2GIT-$PKG_VERSION-$(date +%Y%m%d%H%M%S)"
[ $C ] && git checkout -b "$TEMP_BRANCH"
[ $O ] && git checkout --orphan "$TEMP_BRANCH"
2018-10-04 23:42:58 +00:00
git rm --cached -rf .
2018-10-04 01:50:00 +00:00
# commit the files that should be included in the published package
2018-10-04 23:58:05 +00:00
PKG_TAR="$(npm pack | tail -n 1)"
2018-10-19 16:14:17 +00:00
tar tf "$PKG_TAR" | cut -c 9- | xargs -d '\n' git add -f
git commit -m "v$PKG_VERSION @ $ORIG_COMMIT"
2018-10-04 01:50:00 +00:00
# return to original state
2018-10-19 16:14:17 +00:00
rm "$PKG_TAR"
mv -f package.json.bak package.json
git symbolic-ref HEAD "$ORIG_HEAD"
git reset
# tag commit
[ $S ] && git tag "v$PKG_VERSION-src"
2018-10-19 16:14:17 +00:00
git tag "v$PKG_VERSION" "$TEMP_BRANCH" -am "v$PKG_VERSION @ $ORIG_COMMIT"
# delete temporary branch
2018-10-19 16:14:17 +00:00
git branch -D "$TEMP_BRANCH"