11#!/usr/bin/env node
22
3- const fs = require ( 'fs' ) ;
4- const { execSync } = require ( 'child_process' ) ;
5- const path = require ( 'path' ) ;
3+ import fs from 'fs' ;
4+ import { execSync } from 'child_process' ;
5+ import path from 'path' ;
66
77/**
88 * Script to conditionally publish npm packages based on version type (SNAPSHOT vs release)
@@ -51,19 +51,11 @@ function findPackageJsonFiles(dir, fileList = []) {
5151/**
5252 * Update version in a package.json file
5353 * @param {string } packageJsonPath - Path to package.json
54+ * @param {Object } packageJson - Parsed package.json content
5455 * @param {string } timestamp - Timestamp to replace SNAPSHOT with
5556 * @returns {Object } - {success, originalVersion, newVersion, packageName, isSnapshot}
5657 */
57- function updatePackageVersion ( packageJsonPath , timestamp ) {
58- let packageJson ;
59-
60- try {
61- packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
62- } catch ( error ) {
63- console . error ( `Error reading ${ packageJsonPath } : ${ error . message } ` ) ;
64- return { success : false } ;
65- }
66-
58+ function updatePackageVersion ( packageJsonPath , packageJson , timestamp ) {
6759 const originalVersion = packageJson . version ;
6860 const packageName = packageJson . name ;
6961 const isSnapshot = originalVersion . includes ( 'SNAPSHOT' ) ;
@@ -110,24 +102,40 @@ if (packageJsonFiles.length === 0) {
110102}
111103
112104// Generate a common timestamp for all packages.
113- const timestamp = Math . floor ( Date . now ( ) / 1000 ) ;
105+ const timestamp = Math . floor ( Date . now ( ) / 1000 ) . toString ( ) ;
114106
115107// Determine if we're dealing with SNAPSHOT or release versions
116108// Check the first package.json to determine the mode
117109let isSnapshotMode = false ;
118110try {
119- const firstPackage = JSON . parse ( fs . readFileSync ( packageJsonFiles [ 0 ] , 'utf8' ) ) ;
120- isSnapshotMode = firstPackage . version . includes ( 'SNAPSHOT' ) ;
111+ // Look for the first package.json file with a version.
112+ const firstVersion = packageJsonFiles . map ( packageJsonPath => JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) )
113+ . filter ( packageJson => packageJson . version )
114+ . map ( packageJson => packageJson . version ) [ 0 ] ;
115+ isSnapshotMode = firstVersion . includes ( 'SNAPSHOT' ) ;
121116} catch ( error ) {
122117 console . error ( 'Error determining version mode' ) ;
123118 process . exit ( 1 ) ;
124119}
125120
126-
127121// Step 1: Update all package.json versions
128- const updates = packageJsonFiles . map ( packageJsonPath => {
129- const result = updatePackageVersion ( packageJsonPath , timestamp . toString ( ) ) ;
130- return { ...result , path : packageJsonPath } ;
122+ const updates = packageJsonFiles
123+ . map ( packageJsonPath => {
124+ let packageJson ;
125+
126+ try {
127+ packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
128+ } catch ( error ) {
129+ console . error ( `Error reading ${ packageJsonPath } : ${ error . message } ` ) ;
130+ return { success : false } ;
131+ }
132+
133+ return { packageJsonPath, packageJson, success : true }
134+ } )
135+ . filter ( ( { packageJsonPath, packageJson, success} ) => success && packageJson . version )
136+ . map ( ( { packageJsonPath, packageJson} ) => {
137+ const result = updatePackageVersion ( packageJsonPath , packageJson , timestamp ) ;
138+ return { ...result , path : packageJsonPath } ;
131139} ) ;
132140
133141const failedUpdates = updates . filter ( u => ! u . success ) ;
@@ -137,8 +145,6 @@ if (failedUpdates.length > 0) {
137145 process . exit ( 1 ) ;
138146}
139147
140-
141-
142148try {
143149 if ( isSnapshotMode ) {
144150 execSync (
0 commit comments