1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+ const { execSync } = require ( 'child_process' ) ;
4+ 5+ const TMP_DIR = 'build-tmp' ;
6+ const DIST_DIR = 'dist' ;
7+ const PACKAGE_JSON = 'package.json' ;
8+ 9+ function cleanTmp ( ) {
10+ if ( fs . existsSync ( TMP_DIR ) ) {
11+ fs . rmSync ( TMP_DIR , { recursive : true , force : true } ) ;
12+ }
13+ }
14+ 15+ function run ( cmd , options = { } ) {
16+ console . log ( `$ ${ cmd } ` ) ;
17+ execSync ( cmd , { stdio : 'inherit' , ...options } ) ;
18+ }
19+ 20+ function copyDistToTmp ( ) {
21+ run ( `rsync -a ${ DIST_DIR } / ${ TMP_DIR } /` ) ;
22+ }
23+ 24+ function copyExtraFiles ( ) {
25+ const extras = [ 'react-app-env.d.ts' ] ;
26+ extras . forEach ( ( file ) => {
27+ if ( fs . existsSync ( file ) ) {
28+ fs . copyFileSync ( file , path . join ( TMP_DIR , file ) ) ;
29+ }
30+ } ) ;
31+ }
32+ 33+ function sanitizePackageJson ( ) {
34+ const original = JSON . parse ( fs . readFileSync ( PACKAGE_JSON , 'utf8' ) ) ;
35+ const cleaned = {
36+ name : original . name ,
37+ version : original . version ,
38+ description : original . description ,
39+ author : original . author ,
40+ license : original . license ,
41+ main : original . main || 'index.js' ,
42+ types : original . types || 'index.d.ts' ,
43+ peerDependencies : original . peerDependencies ,
44+ dependencies : original . dependencies ,
45+ } ;
46+ 47+ fs . writeFileSync (
48+ path . join ( TMP_DIR , 'package.json' ) ,
49+ JSON . stringify ( cleaned , null , 2 ) ,
50+ 'utf8'
51+ ) ;
52+ }
53+ 54+ function pack ( ) {
55+ run ( 'npm pack' , { cwd : TMP_DIR } ) ;
56+ const tarball = fs . readdirSync ( TMP_DIR ) . find ( f => f . endsWith ( '.tgz' ) ) ;
57+ fs . renameSync ( path . join ( TMP_DIR , tarball ) , path . join ( '.' , tarball ) ) ;
58+ console . log ( `✅ Packed to ./${ tarball } ` ) ;
59+ }
60+ 61+ function buildTmpAndPack ( ) {
62+ cleanTmp ( ) ;
63+ run ( 'pnpm run build' ) ;
64+ copyDistToTmp ( ) ;
65+ sanitizePackageJson ( ) ;
66+ pack ( ) ;
67+ cleanTmp ( ) ;
68+ }
69+ 70+ buildTmpAndPack ( ) ;
0 commit comments