1
Fork 0
npm2git/npm2git.sh

40 lines
1.1 KiB
Bash
Raw Normal View History

# get ready
2018-10-03 21:50:00 -04:00
set -e
2018-10-08 01:05:01 -04:00
cd "$(git rev-parse --show-toplevel)"
ORIG_HEAD="$(git symbolic-ref HEAD)"
2018-10-04 11:24:42 -04:00
ORIG_COMMIT="$(git rev-parse HEAD)"
2018-10-04 19:58:05 -04:00
# extract version from package.json, and remove scripts.prepare
cp package.json package.json_orig
2018-10-04 19:58:05 -04:00
PKG_VERSION="$(node -e '
2018-10-04 10:48:02 -04: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 19:58:05 -04:00
console.log(pkg.version);
')"
2018-10-03 21:50:00 -04:00
# create new temporary branch
TEMP_BRANCH="NPM2GIT_${PKG_VERSION}_$(date +'%Y%m%d%H%M%S')"
2018-10-08 01:05:01 -04:00
git checkout --orphan="${TEMP_BRANCH}"
2018-10-04 19:42:58 -04:00
git rm --cached -rf .
2018-10-03 21:50:00 -04:00
# commit the files that should be included in the published package
2018-10-04 19:58:05 -04:00
PKG_TAR="$(npm pack | tail -n 1)"
2018-10-08 01:05:01 -04:00
tar tf "${PKG_TAR}" | cut -c 9- | xargs -d '\n' git add -f
2018-10-04 11:24:42 -04:00
git commit -m "v${PKG_VERSION} @ ${ORIG_COMMIT}"
2018-10-03 21:50:00 -04: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 01:05:01 -04:00
git branch -D "${TEMP_BRANCH}"