npm2git/npm2git.sh

40 lines
1.1 KiB
Bash
Raw Normal View History

# get ready
2018-10-04 01:50:00 +00:00
set -e
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
cp package.json package.json_orig
2018-10-04 23:58:05 +00:00
PKG_VERSION="$(node -e '
2018-10-04 14:48:02 +00:00
const fs = require("fs");
const pkg = JSON.parse(fs.readFileSync("package.json"));
if (pkg.scripts && pkg.scripts.prepare) {
delete pkg.scripts.prepare;
fs.writeFileSync("package.json", JSON.stringify(pkg, null, "\t") + "\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
TEMP_BRANCH="NPM2GIT_${PKG_VERSION}_$(date +'%Y%m%d%H%M%S')"
2018-10-08 05:05:01 +00:00
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-08 05:05:01 +00:00
tar tf "${PKG_TAR}" | cut -c 9- | xargs -d '\n' git add -f
2018-10-04 15:24:42 +00:00
git commit -m "v${PKG_VERSION} @ ${ORIG_COMMIT}"
2018-10-04 01:50:00 +00:00
# return to original state
rm "${PKG_TAR}"
mv -f package.json_orig package.json
git symbolic-ref HEAD "${ORIG_HEAD}"
git reset
# tag commit
git tag "v${PKG_VERSION}" "${TEMP_BRANCH}" -am "v${PKG_VERSION} @ ${ORIG_COMMIT}"
# delete temporary branch
2018-10-08 05:05:01 +00:00
git branch -D "${TEMP_BRANCH}"