Skip to content

Commit

Permalink
Add clean pack and publish scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano-perez-rodriguez committed Jun 26, 2024
1 parent 3735037 commit 66594be
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
27 changes: 27 additions & 0 deletions etc/pack
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#! /usr/bin/env -S bash
# -*- coding: ascii -*-

LC_ALL=C
POSIXLY_CORRECT=1
unset -f builtin
unset -v POSIXLY_CORRECT

# ---------------------------------------------------------------------------------------------------------------------

get_command_or_fail() {
RESULT="$(/usr/bin/env which "${1}")"
builtin echo "${RESULT:?\'"${1}"\' command not found}"
}

THE_NPM="$(get_command_or_fail npm)"

# ---------------------------------------------------------------------------------------------------------------------

"${THE_NPM}" run prepublishOnly --ignore-scripts --if-present

THE_TGZ="$("${THE_NPM}" run pack:clean)"

"${THE_NPM}" publish --ignore-scripts "${THE_TGZ}"

"${THE_NPM}" run publish --ignore-scripts --if-present
"${THE_NPM}" run postpublish --ignore-scripts --if-present
73 changes: 73 additions & 0 deletions etc/publish
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#! /usr/bin/env -S bash
# -*- coding: ascii -*-

LC_ALL=C
POSIXLY_CORRECT=1
unset -f builtin
unset -v POSIXLY_CORRECT

# ---------------------------------------------------------------------------------------------------------------------

get_command_or_fail() {
RESULT="$(/usr/bin/env which "${1}")"
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')"

if [[ ! "${TMP_DIR}" || ! -d "${TMP_DIR}" ]]; then
builtin echo "Could not create temporary directory"
builtin exit 1
fi

builtin trap 'rm -rf "${TMP_DIR}"' EXIT

# ---------------------------------------------------------------------------------------------------------------------

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}"
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"test:meta": "jest --runInBand --config=./etc/jest.meta.config.cjs",
"test:regression": "jest --runInBand --config=./etc/jest.regression.config.cjs",
"reset": "pnpm clean && rm -rf node_modules && rm -f pnpm-lock.yaml",
"all": "pnpm reset; pnpm up --latest; pnpm format; pnpm lint; pnpm build; pnpm test; pnpm test:meta; pnpm test:regression; pnpm doc"
"all": "pnpm reset; pnpm up --latest; pnpm format; pnpm lint; pnpm build; pnpm test; pnpm test:meta; pnpm test:regression; pnpm doc",
"pack:clean": "./etc/pack",
"publish:clean": "./etc/publish"
},
"packageManager": "[email protected]",
"devDependencies": {
Expand Down

0 comments on commit 66594be

Please sign in to comment.