diff --git a/etc/pack b/etc/pack index 5d88886..4c83f7d 100755 --- a/etc/pack +++ b/etc/pack @@ -13,15 +13,61 @@ get_command_or_fail() { builtin echo "${RESULT:?\'"${1}"\' command not found}" } +THE_MKTEMP="$(get_command_or_fail mktemp)" THE_NPM="$(get_command_or_fail npm)" +THE_TAR="$(get_command_or_fail tar)" +THE_NODE="$(get_command_or_fail node)" +THE_GZIP="$(get_command_or_fail gzip)" # --------------------------------------------------------------------------------------------------------------------- -"${THE_NPM}" run prepublishOnly --ignore-scripts --if-present +TMP_DIR="$("${THE_MKTEMP}" -d 2> /dev/null || "${THE_MKTEMP}" -d -t 'tmp')" -THE_TGZ="$("${THE_NPM}" run pack:clean)" +if [[ ! "${TMP_DIR}" || ! -d "${TMP_DIR}" ]]; then + builtin echo "Could not create temporary directory" + builtin exit 1 +fi -"${THE_NPM}" publish --ignore-scripts "${THE_TGZ}" +builtin trap 'rm -rf "${TMP_DIR}"' EXIT -"${THE_NPM}" run publish --ignore-scripts --if-present -"${THE_NPM}" run postpublish --ignore-scripts --if-present +# --------------------------------------------------------------------------------------------------------------------- + +THE_DIR="$(builtin pwd)" + +THE_TGZ="$("${THE_NPM}" pack --pack-destination "${TMP_DIR}" --json | "${THE_NODE}" -e "process.stdout.write(JSON.parse(require('fs').readFileSync(0))[0].filename);")" + +builtin pushd "${TMP_DIR}" > /dev/null || exit 1 + +"${THE_TAR}" xzf "${THE_TGZ}" + +"${THE_NODE}" -e " +const fs = require('fs'); + +const package = JSON.parse(fs.readFileSync('package/package.json')); + +delete package.devDependencies; +delete package.files; + +if ('scripts' in package) { + package.scripts = [ + 'preinstall', + 'install', + 'postinstall', + 'prepublish', + 'preprepare', + 'prepare', + 'postprepare', + ].reduce( + (obj, key) => key in package.scripts ? ({ ...obj, [key]: package.scripts[key] }) : obj, + {}, + ); +} + +fs.writeFileSync('package/package.json', JSON.stringify(package, null, 2) + '\n'); +" + +"${THE_TAR}" c package | "${THE_GZIP}" -9 > "${THE_DIR}/${THE_TGZ}" + +builtin popd > /dev/null || exit 1 + +builtin echo "${THE_TGZ}" diff --git a/etc/publish b/etc/publish index 4c83f7d..5d88886 100755 --- a/etc/publish +++ b/etc/publish @@ -13,61 +13,15 @@ get_command_or_fail() { builtin echo "${RESULT:?\'"${1}"\' command not found}" } -THE_MKTEMP="$(get_command_or_fail mktemp)" THE_NPM="$(get_command_or_fail npm)" -THE_TAR="$(get_command_or_fail tar)" -THE_NODE="$(get_command_or_fail node)" -THE_GZIP="$(get_command_or_fail gzip)" # --------------------------------------------------------------------------------------------------------------------- -TMP_DIR="$("${THE_MKTEMP}" -d 2> /dev/null || "${THE_MKTEMP}" -d -t 'tmp')" +"${THE_NPM}" run prepublishOnly --ignore-scripts --if-present -if [[ ! "${TMP_DIR}" || ! -d "${TMP_DIR}" ]]; then - builtin echo "Could not create temporary directory" - builtin exit 1 -fi +THE_TGZ="$("${THE_NPM}" run pack:clean)" -builtin trap 'rm -rf "${TMP_DIR}"' EXIT +"${THE_NPM}" publish --ignore-scripts "${THE_TGZ}" -# --------------------------------------------------------------------------------------------------------------------- - -THE_DIR="$(builtin pwd)" - -THE_TGZ="$("${THE_NPM}" pack --pack-destination "${TMP_DIR}" --json | "${THE_NODE}" -e "process.stdout.write(JSON.parse(require('fs').readFileSync(0))[0].filename);")" - -builtin pushd "${TMP_DIR}" > /dev/null || exit 1 - -"${THE_TAR}" xzf "${THE_TGZ}" - -"${THE_NODE}" -e " -const fs = require('fs'); - -const package = JSON.parse(fs.readFileSync('package/package.json')); - -delete package.devDependencies; -delete package.files; - -if ('scripts' in package) { - package.scripts = [ - 'preinstall', - 'install', - 'postinstall', - 'prepublish', - 'preprepare', - 'prepare', - 'postprepare', - ].reduce( - (obj, key) => key in package.scripts ? ({ ...obj, [key]: package.scripts[key] }) : obj, - {}, - ); -} - -fs.writeFileSync('package/package.json', JSON.stringify(package, null, 2) + '\n'); -" - -"${THE_TAR}" c package | "${THE_GZIP}" -9 > "${THE_DIR}/${THE_TGZ}" - -builtin popd > /dev/null || exit 1 - -builtin echo "${THE_TGZ}" +"${THE_NPM}" run publish --ignore-scripts --if-present +"${THE_NPM}" run postpublish --ignore-scripts --if-present