@@ -10,7 +10,7 @@ inputs:
1010 required : false
1111 default : " https://registry.npmjs.org/"
1212 package-dir :
13- description : Directory containing package.json to publish
13+ description : Directory containing a package.json to publish, or a parent directory whose immediate children each contain a package.json
1414 required : true
1515 verify-attempts :
1616 description : Number of verification attempts
3535 run : |
3636 set -euo pipefail
3737
38- package_name=$(jq -r '.name' "${PACKAGE_DIR}/package.json")
39- package_version=$(jq -r '.version' "${PACKAGE_DIR}/package.json")
40-
4138 registry_version_exists() {
39+ local package_name="$1"
40+ local package_version="$2"
4241 local encoded_package_name
4342 local version_json
4443 local published_version
@@ -55,22 +54,34 @@ runs:
5554 }
5655
5756 version_exists() {
57+ local package_dir="$1"
58+ local package_name
59+ local package_version
5860 local published_version
61+
62+ package_name=$(jq -r '.name' "${package_dir}/package.json")
63+ package_version=$(jq -r '.version' "${package_dir}/package.json")
5964 published_version=$(npm view "${package_name}@${package_version}" version --registry "$REGISTRY_URL" 2>/dev/null || true)
6065
6166 if [[ "$published_version" == "$package_version" ]]; then
6267 return 0
6368 fi
6469
65- registry_version_exists
70+ registry_version_exists "$package_name" "$package_version"
6671 }
6772
6873 verify_version_exists() {
74+ local package_dir="$1"
75+ local package_name
76+ local package_version
6977 local attempts="$VERIFY_ATTEMPTS"
7078 local delay_seconds="$VERIFY_DELAY"
7179
80+ package_name=$(jq -r '.name' "${package_dir}/package.json")
81+ package_version=$(jq -r '.version' "${package_dir}/package.json")
82+
7283 for attempt in $(seq 1 "$attempts"); do
73- if version_exists; then
84+ if version_exists "$package_dir" ; then
7485 echo "Verified ${package_name}@${package_version} on npm"
7586 return 0
7687 fi
@@ -87,31 +98,61 @@ runs:
8798 return 1
8899 }
89100
90- if version_exists; then
91- echo "${package_name}@${package_version} already exists on npm, skipping"
92- exit 0
93- fi
101+ publish_package() {
102+ local package_dir="$1"
103+ local package_name
104+ local package_version
105+ local publish_log
94106
95- publish_log=$(mktemp)
107+ package_name=$(jq -r '.name' "${package_dir}/package.json")
108+ package_version=$(jq -r '.version' "${package_dir}/package.json")
96109
97- if (cd "$PACKAGE_DIR" && pnpm publish --access public --no-git-checks) 2>&1 | tee "$publish_log"; then
98- verify_version_exists
99- rm -f "$publish_log"
100- exit 0
101- fi
110+ if version_exists "$package_dir"; then
111+ echo "${package_name}@${package_version} already exists on npm, skipping"
112+ return 0
113+ fi
114+
115+ publish_log=$(mktemp)
116+
117+ if (cd "$package_dir" && pnpm publish --access public --no-git-checks) 2>&1 | tee "$publish_log"; then
118+ verify_version_exists "$package_dir"
119+ rm -f "$publish_log"
120+ return 0
121+ fi
102122
103- if grep -Eiq 'cannot publish over the previously published versions|previously published versions' "$publish_log"; then
104- echo "${package_name}@${package_version} was already published according to npm, skipping"
123+ if grep -Eiq 'cannot publish over the previously published versions|previously published versions' "$publish_log"; then
124+ echo "${package_name}@${package_version} was already published according to npm, skipping"
125+ rm -f "$publish_log"
126+ return 0
127+ fi
128+
129+ if version_exists "$package_dir"; then
130+ echo "${package_name}@${package_version} already exists on npm after publish attempt, skipping"
131+ rm -f "$publish_log"
132+ return 0
133+ fi
134+
135+ echo "::error::Failed to publish ${package_name}@${package_version}. Exact version is still missing from npm."
105136 rm -f "$publish_log"
106- exit 0
137+ return 1
138+ }
139+
140+ package_dirs=()
141+ if [[ -f "${PACKAGE_DIR}/package.json" ]]; then
142+ package_dirs+=("$PACKAGE_DIR")
143+ else
144+ shopt -s nullglob
145+ for package_json in "$PACKAGE_DIR"/*/package.json; do
146+ package_dirs+=("$(dirname "$package_json")")
147+ done
107148 fi
108149
109- if version_exists; then
110- echo "${package_name}@${package_version} already exists on npm after publish attempt, skipping"
111- rm -f "$publish_log"
112- exit 0
150+ if [[ "${#package_dirs[@]}" -eq 0 ]]; then
151+ echo "::error::No publishable package.json found in ${PACKAGE_DIR}"
152+ exit 1
113153 fi
114154
115- echo "::error::Failed to publish ${package_name}@${package_version}. Exact version is still missing from npm."
116- rm -f "$publish_log"
117- exit 1
155+ for package_dir in "${package_dirs[@]}"; do
156+ echo "Publishing package from ${package_dir}"
157+ publish_package "$package_dir"
158+ done
0 commit comments