@@ -37,16 +37,48 @@ function _mkdirp(p: string) {
3737 fs . mkdirSync ( p ) ;
3838}
3939
40+ function _recursiveFileList ( p : string ) : string [ ] {
41+ if ( ! fs . statSync ( p ) . isDirectory ( ) ) {
42+ return [ ] ;
43+ }
44+
45+ const list = fs . readdirSync ( p ) ;
46+
47+ return list
48+ . map ( subpath => {
49+ const subpathList = _recursiveFileList ( path . join ( p , subpath ) ) ;
50+
51+ return [ subpath , ...subpathList . map ( sp => path . join ( subpath , sp ) ) ] ;
52+ } )
53+ // Flatten.
54+ . reduce ( ( acc , curr ) => [ ...acc , ...curr ] , [ ] )
55+ // Filter out directories.
56+ . filter ( sp => ! fs . statSync ( path . join ( p , sp ) ) . isDirectory ( ) ) ;
57+ }
4058
59+
60+ // This method mimics how npm pack tars packages.
4161function _tar ( out : string , dir : string ) {
62+ // NOTE: node-tar does some Magic Stuff depending on prefixes for files
63+ // specifically with @ signs, so we just neutralize that one
64+ // and any such future "features" by prepending `./`
65+
66+ // Without this, the .tar file cannot be opened on Windows.
67+
68+ const files = _recursiveFileList ( dir ) . map ( ( f ) => `./${ f } ` ) ;
69+
4270 return tar . create ( {
4371 gzip : true ,
4472 strict : true ,
4573 portable : true ,
4674 cwd : dir ,
75+ prefix : 'package/' ,
4776 file : out ,
4877 sync : true ,
49- } , [ '.' ] ) ;
78+ // Provide a specific date in the 1980s for the benefit of zip,
79+ // which is confounded by files dated at the Unix epoch 0.
80+ mtime : new Date ( '1985-10-26T08:15:00.000Z' ) ,
81+ } , files ) ;
5082}
5183
5284
0 commit comments