1+ #!/usr/bin/env node
2+
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' ) ;
5+
6+ /**
7+ * Script to prepare types packages for publishing by modifying their dist/package.json
8+ * to use the correct publishing name and dist-tag from x-publish metadata
9+ */
10+
11+ function preparePackageForPublish ( packageDir ) {
12+ const packageJsonPath = path . join ( packageDir , 'package.json' ) ;
13+ const distPackageJsonPath = path . join ( packageDir , 'dist' , 'package.json' ) ;
14+
15+ if ( ! fs . existsSync ( packageJsonPath ) ) {
16+ console . error ( `❌ Package.json not found: ${ packageJsonPath } ` ) ;
17+ return false ;
18+ }
19+
20+ if ( ! fs . existsSync ( distPackageJsonPath ) ) {
21+ console . error ( `❌ Dist package.json not found: ${ distPackageJsonPath } ` ) ;
22+ return false ;
23+ }
24+
25+ try {
26+ const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
27+ const distPackageJson = JSON . parse ( fs . readFileSync ( distPackageJsonPath , 'utf8' ) ) ;
28+
29+ if ( ! packageJson [ 'x-publish' ] ) {
30+ console . error ( `❌ No x-publish metadata found in ${ packageDir } ` ) ;
31+ return false ;
32+ }
33+
34+ const { publishName, distTag } = packageJson [ 'x-publish' ] ;
35+
36+ if ( ! publishName ) {
37+ console . error ( `❌ No publishName found in x-publish metadata for ${ packageDir } ` ) ;
38+ return false ;
39+ }
40+
41+ // Modify the dist package.json
42+ distPackageJson . name = publishName ;
43+
44+ // Add dist-tag to publishConfig if specified
45+ if ( distTag ) {
46+ if ( ! distPackageJson . publishConfig ) {
47+ distPackageJson . publishConfig = { } ;
48+ }
49+ distPackageJson . publishConfig . tag = distTag ;
50+ }
51+
52+ // Remove x-publish metadata from the dist version
53+ delete distPackageJson [ 'x-publish' ] ;
54+
55+ // Write the modified package.json back to dist
56+ fs . writeFileSync ( distPackageJsonPath , JSON . stringify ( distPackageJson , null , 2 ) + '\n' ) ;
57+
58+ console . log ( `✅ Prepared ${ packageDir } for publishing as ${ publishName } ${ distTag ? ` with tag ${ distTag } ` : '' } ` ) ;
59+ return true ;
60+
61+ } catch ( error ) {
62+ console . error ( `❌ Error preparing ${ packageDir } : ${ error . message } ` ) ;
63+ return false ;
64+ }
65+ }
66+
67+ function main ( ) {
68+ const typesDir = path . join ( __dirname , '..' , 'types' ) ;
69+
70+ if ( ! fs . existsSync ( typesDir ) ) {
71+ console . error ( '❌ Types directory not found' ) ;
72+ process . exit ( 1 ) ;
73+ }
74+
75+ const typesPackages = fs . readdirSync ( typesDir )
76+ . filter ( dir => fs . statSync ( path . join ( typesDir , dir ) ) . isDirectory ( ) )
77+ . sort ( ) ;
78+
79+ console . log ( `📦 Found ${ typesPackages . length } types packages: ${ typesPackages . join ( ', ' ) } \n` ) ;
80+
81+ let successCount = 0 ;
82+
83+ for ( const packageName of typesPackages ) {
84+ const packagePath = path . join ( typesDir , packageName ) ;
85+ console . log ( `🔧 Preparing types/${ packageName } ...` ) ;
86+
87+ if ( preparePackageForPublish ( packagePath ) ) {
88+ successCount ++ ;
89+ }
90+ console . log ( '' ) ;
91+ }
92+
93+ console . log ( '==================================================' ) ;
94+ console . log ( `Prepare Summary:` ) ;
95+ console . log ( `✅ Successful: ${ successCount } ` ) ;
96+ console . log ( `❌ Failed: ${ typesPackages . length - successCount } ` ) ;
97+ console . log ( `📦 Total packages: ${ typesPackages . length } ` ) ;
98+
99+ if ( successCount < typesPackages . length ) {
100+ process . exit ( 1 ) ;
101+ }
102+ }
103+
104+ if ( require . main === module ) {
105+ main ( ) ;
106+ }
107+
108+ module . exports = { preparePackageForPublish } ;
0 commit comments