npm2git/npm2git.sh

42 lines
1.2 KiB
Bash
Raw Normal View History

2018-10-04 01:50:00 +00:00
# bail on failures
set -e
# go to root of repository
cd $(git rev-parse --show-toplevel)
# get commit id
2018-10-04 15:24:42 +00:00
ORIG_COMMIT="$(git rev-parse HEAD)"
# extract package name and version from package.json, remove scripts.prepare
ORIG_PKG="$(cat package.json)"
2018-10-04 15:24:42 +00:00
read PKG_NAME 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");
}
console.log(pkg.name + " " + pkg.version);
2018-10-04 10:24:50 +00:00
')
2018-10-04 01:50:00 +00:00
# determine current branch name, and create new temporary branch
ORIG_BRANCH=$(git symbolic-ref --short HEAD)
2018-10-04 15:24:42 +00:00
TEMP_BRANCH=RELEASE_${PKG_VERSION}_$(date +'%Y%m%d%H%M%S')
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
2018-10-04 23:42:58 +00:00
# track the files that should be included in the published package
2018-10-04 01:50:00 +00:00
npm pack
2018-10-04 15:24:42 +00:00
git add -f $(tar tf ${PKG_NAME}-${PKG_VERSION}.tgz | cut -c 9-)
rm ${PKG_NAME}-${PKG_VERSION}.tgz
2018-10-04 01:50:00 +00:00
2018-10-04 10:23:32 +00:00
# commit and tag
2018-10-04 15:24:42 +00:00
git commit -m "v${PKG_VERSION} @ ${ORIG_COMMIT}"
2018-10-04 23:42:58 +00:00
git tag v${PKG_VERSION} -am "v${PKG_VERSION} @ ${ORIG_COMMIT}"
2018-10-04 01:50:00 +00:00
# return to original state
git reset ${ORIG_BRANCH}
git checkout ${ORIG_BRANCH}
2018-10-04 10:24:50 +00:00
cat <<< "${ORIG_PKG}" > package.json
2018-10-04 01:50:00 +00:00
git branch -D ${TEMP_BRANCH}