@@ -117,7 +117,15 @@ function setPipelineVariable(name, value) {
117117( function main ( ) {
118118 const { packRootArg, customRootArg, latestRootArg} = ensureArgs ( ) ;
119119
120- const repoRoot = process . env . BUILD_SOURCESDIRECTORY || process . cwd ( ) ;
120+ const repoRoot = path . resolve ( __dirname , '..' , '..' ) ;
121+ const vnextPackageJsonPath = path . join ( repoRoot , 'vnext' , 'package.json' ) ;
122+
123+ if ( ! fs . existsSync ( vnextPackageJsonPath ) ) {
124+ console . error ( `[npmGroupByTag] vnext package.json not found at ${ vnextPackageJsonPath } ` ) ;
125+ process . exit ( 1 ) ;
126+ }
127+
128+ console . log ( `[npmGroupByTag] Using repo root ${ repoRoot } ` ) ;
121129 const packRoot = path . resolve ( packRootArg ) ;
122130 const customRoot = path . resolve ( customRootArg ) ;
123131 const latestRoot = path . resolve ( latestRootArg ) ;
@@ -128,15 +136,19 @@ function setPipelineVariable(name, value) {
128136 /** @type {string | null } */
129137 let customTag = null ;
130138 try {
131- const vnextPackageJson = /** @type {PackageJson } */ (
132- readJson ( path . join ( repoRoot , 'vnext' , 'package.json' ) )
133- ) ;
139+ const vnextPackageJson = /** @type {PackageJson } */ ( readJson ( vnextPackageJsonPath ) ) ;
134140 const tagFromVnext = vnextPackageJson ?. beachball ?. defaultNpmTag ;
135141 if ( tagFromVnext && tagFromVnext !== 'latest' ) {
136142 customTag = tagFromVnext ;
137143 }
144+ console . log (
145+ `[npmGroupByTag] vnext defaultNpmTag is ${ tagFromVnext || 'latest' } ${
146+ customTag ? ` (custom tag '${ customTag } ' will be used)` : ' (no custom tag)'
147+ } `,
148+ ) ;
138149 } catch ( e ) {
139- console . warn ( 'Unable to read vnext/package.json to determine custom tag.' ) ;
150+ console . error ( `[npmGroupByTag] Failed to read ${ vnextPackageJsonPath } : ${ e } ` ) ;
151+ process . exit ( 1 ) ;
140152 }
141153
142154 /** @type {string[] } */
@@ -153,28 +165,31 @@ function setPipelineVariable(name, value) {
153165 return ;
154166 }
155167
156- /** @type {Set<string> } */
157- const customTarballs = new Set ( ) ;
168+ /** @type {Map<string, {packageJsonPath: string, name: string, version: string, tag: string, isPrivate: boolean}> } */
169+ const packageMetadata = new Map ( ) ;
170+ for ( const packageJsonPath of findPackageJsons ( repoRoot ) ) {
171+ /** @type {PackageJson | undefined } */
172+ let pkg ;
173+ try {
174+ pkg = /** @type {PackageJson } */ ( readJson ( packageJsonPath ) ) ;
175+ } catch ( e ) {
176+ console . warn ( `[npmGroupByTag] Skipping unreadable package.json ${ packageJsonPath } : ${ e } ` ) ;
177+ continue ;
178+ }
158179
159- if ( customTag ) {
160- for ( const packageJsonPath of findPackageJsons ( repoRoot ) ) {
161- /** @type {PackageJson | undefined } */
162- let pkg ;
163- try {
164- pkg = /** @type {PackageJson } */ ( readJson ( packageJsonPath ) ) ;
165- } catch ( e ) {
166- continue ;
167- }
180+ if ( ! pkg ?. name || ! pkg ?. version ) {
181+ continue ;
182+ }
168183
169- if ( ! pkg ?. name || ! pkg ?. version ) {
170- continue ;
171- }
184+ const metadata = {
185+ packageJsonPath,
186+ name : pkg . name ,
187+ version : pkg . version ,
188+ tag : pkg ?. beachball ?. defaultNpmTag || 'latest' ,
189+ isPrivate : pkg . private === true ,
190+ } ;
172191
173- const pkgTag = pkg ?. beachball ?. defaultNpmTag ;
174- if ( pkgTag === customTag && pkg . private !== true ) {
175- customTarballs . add ( sanitizedTarballName ( pkg . name , pkg . version ) ) ;
176- }
177- }
192+ packageMetadata . set ( sanitizedTarballName ( pkg . name , pkg . version ) , metadata ) ;
178193 }
179194
180195 let customCount = 0 ;
@@ -183,10 +198,22 @@ function setPipelineVariable(name, value) {
183198 for ( const tarball of tarballs ) {
184199 const sourcePath = path . join ( packRoot , tarball ) ;
185200 const normalizedName = normalizePackedTarballName ( tarball ) ;
186- const destinationRoot = customTag && customTarballs . has ( normalizedName ) ? customRoot : latestRoot ;
201+ const metadata = packageMetadata . get ( normalizedName ) ;
202+ const destinationRoot = customTag && metadata && ! metadata . isPrivate && metadata . tag === customTag ? customRoot : latestRoot ;
187203 const destinationPath = path . join ( destinationRoot , tarball ) ;
188204 fs . mkdirSync ( path . dirname ( destinationPath ) , { recursive : true } ) ;
189205 fs . renameSync ( sourcePath , destinationPath ) ;
206+
207+ if ( metadata ) {
208+ const destinationLabel = destinationRoot === customRoot ? 'custom' : 'latest' ;
209+ console . log (
210+ `[npmGroupByTag] ${ tarball } : ${ metadata . name } @${ metadata . version } (${ metadata . packageJsonPath } ) tag=${ metadata . tag } ` +
211+ `private=${ metadata . isPrivate } -> ${ destinationLabel } ` ,
212+ ) ;
213+ } else {
214+ console . warn ( `[npmGroupByTag] ${ tarball } : no package.json metadata found. Defaulting to latest.` ) ;
215+ }
216+
190217 if ( destinationRoot === customRoot ) {
191218 customCount ++ ;
192219 } else {
0 commit comments