Skip to content

Commit c5582ad

Browse files
fix: add error handling for version extraction in publish workflow
1 parent b3effb7 commit c5582ad

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

.github/workflows/publish-on-next-close.yml

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,34 @@ jobs:
215215
run: |
216216
set -euo pipefail
217217
218+
# Helper function to extract version with error handling
219+
get_lib_version() {
220+
local lib="$1"
221+
node -e "
222+
try {
223+
const pkg = require('./libs/$lib/package.json');
224+
if (!pkg.version) {
225+
console.error('No version field in package.json');
226+
process.exit(1);
227+
}
228+
console.log(pkg.version);
229+
} catch (err) {
230+
console.error('Failed to read package.json:', err.message);
231+
process.exit(1);
232+
}
233+
"
234+
}
235+
218236
PROJECTS="${{ steps.to_publish.outputs.projects }}"
219237
220238
IFS=',' read -ra LIBS <<< "$PROJECTS"
221239
for lib in "${LIBS[@]}"; do
222240
if [ -f "libs/$lib/package.json" ]; then
223-
LIB_VERSION=$(node -e "console.log(require('./libs/$lib/package.json').version)")
241+
LIB_VERSION=$(get_lib_version "$lib")
242+
if [ -z "$LIB_VERSION" ]; then
243+
echo "Error: Failed to extract version for $lib"
244+
exit 1
245+
fi
224246
TAG_NAME="${lib}@${LIB_VERSION}"
225247
226248
echo "Creating GitHub Release for $TAG_NAME..."
@@ -249,6 +271,24 @@ jobs:
249271
if: steps.to_publish.outputs.projects != ''
250272
shell: bash
251273
run: |
274+
# Helper function to extract version with error handling
275+
get_lib_version() {
276+
local lib="$1"
277+
node -e "
278+
try {
279+
const pkg = require('./libs/$lib/package.json');
280+
if (!pkg.version) {
281+
console.error('No version field in package.json');
282+
process.exit(1);
283+
}
284+
console.log(pkg.version);
285+
} catch (err) {
286+
console.error('Failed to read package.json:', err.message);
287+
process.exit(1);
288+
}
289+
"
290+
}
291+
252292
echo "## Publish Summary" >> $GITHUB_STEP_SUMMARY
253293
echo "" >> $GITHUB_STEP_SUMMARY
254294
echo "**Published projects:** ${{ steps.to_publish.outputs.projects }}" >> $GITHUB_STEP_SUMMARY
@@ -258,7 +298,7 @@ jobs:
258298
IFS=',' read -ra LIBS <<< "${{ steps.to_publish.outputs.projects }}"
259299
for lib in "${LIBS[@]}"; do
260300
if [ -f "libs/$lib/package.json" ]; then
261-
LIB_VERSION=$(node -e "console.log(require('./libs/$lib/package.json').version)")
301+
LIB_VERSION=$(get_lib_version "$lib" 2>/dev/null) || LIB_VERSION="unknown"
262302
echo "- ${lib}@${LIB_VERSION}" >> $GITHUB_STEP_SUMMARY
263303
fi
264304
done

0 commit comments

Comments
 (0)