From 4a97cdfc1ba5f65598a3e43267ffccaf8ce4e73d Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Sun, 10 Sep 2023 22:48:24 -0700 Subject: [PATCH] By default, omit `prepack` and `postpack` scripts --- README.md | 26 ++++++++++++++++++++++++++ action.yml | 23 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/README.md b/README.md index d409689..e4adfda 100644 --- a/README.md +++ b/README.md @@ -79,3 +79,29 @@ the action is complete. ### `commit-email` (default: `github-actions[bot]@users.noreply.github.com`) **Description:** The email to use when pushing the built package to the branch. + +### `omit-scripts` (default: `prepack,postpack`) + +**Description:** Comma separated list of scripts to omit from `package.json` +after packing, or "*" to omit everything. + +**Why is this needed?** Package managers differ in opinion on what scripts to +run when installing dependencies from Git. Some package managers do nothing at +all, and some choose to run `pack` command and the `prepack`/`postpack` scripts +along with it. + +A common reason for using this workflow is to push the already-packed content +to a dist branch in order to support package manages that does not run the +`pack` script when installing a Git dependency. + +However, on package managers that runs the `pack` command, they may encounter +issues when installing from the dist branch. For example, the Ember's v2 addons +has `"prepack": "rollup --config"`, but since the rollup config file is omitted +from the packed tarball, the command will fail. + +Therefore, by default, we omit the `prepack` and `postpack` scripts from the +resulting `package.json` before pushing, in order to normalize this difference +across package managers. Running these scripts should be entirely unnecessary +when installing from the dist branch. + +You can opt-out of this behavior by setting `omit-scripts` to an empty string. diff --git a/action.yml b/action.yml index 33f9668..ab19187 100644 --- a/action.yml +++ b/action.yml @@ -33,6 +33,10 @@ inputs: description: 'Email of the user to use when committing to the branch' required: false default: 'github-actions[bot]@users.noreply.github.com' + omit-scripts: + description: 'Comma separated list of scripts to omit from package.json after packing, or "*" to omit everything' + required: false + default: 'prepack,postpack' runs: using: "composite" @@ -49,6 +53,25 @@ runs: shell: 'bash' run: tar -xvzf *.tgz -C ${{ inputs.tmp-dir }} working-directory: ${{ inputs.working-directory }} + - name: 'Omit Scripts' + shell: 'bash' + env: + OMIT_SCRIPTS: ${{ inputs.omit-scripts }} + OUTPUT_DIR: ${{ inputs.tmp-dir }} + run: | + OUTPUT_FILE="$OUTPUT_DIR/package/package.json" + + if [[ "$OMIT_SCRIPTS" == "*" ]]; then + jq '. | del(.scripts)' package.json > "$OUTPUT_FILE" + elif [[ ! -z "$OMIT_SCRIPTS" ]]; then + JQ_COMMAND="." + IFS=',' read -ra SCRIPTS <<< "$OMIT_SCRIPTS" + for SCRIPT in "${SCRIPTS[@]}"; do + JQ_COMMAND="$JQ_COMMAND | del(.scripts.\"$SCRIPT\")" + done + jq "$JQ_COMMAND" package.json > "$OUTPUT_FILE" + fi + working-directory: ${{ inputs.working-directory }} - name: 'Upload published package contents to branch' shell: 'bash' run: |