@@ -78,46 +78,53 @@ async function extractTarXzBy7zip(file: string, name: string, dest: string, stri
7878 throw new Error ( `Invalid tar file: ${ name } ` )
7979 }
8080 // extract the compression first
81- const tarDir = dirname ( file )
81+ const tarDir = join ( dirname ( file ) , "sevenzip-temp" )
8282 await run7zip ( file , tarDir )
8383 // extract the tar
8484 const tarName = name . slice ( 0 , - 3 )
8585 const tarFile = join ( tarDir , tarName )
8686 await run7zip ( tarFile , tarDir )
8787 await remove ( tarFile )
8888 // move the extracted files to the destination
89- const folderName = tarName . slice ( 0 , - 4 )
90- const folderPath = join ( tarDir , folderName )
91- info ( `Moving ${ folderPath } to ${ dest } ` )
92- await move ( folderPath , dest , { overwrite : true } )
89+ info ( `Moving ${ tarDir } to ${ dest } ` )
90+ const files = await readdir ( tarDir )
91+ await Promise . all (
92+ files . map ( async ( file ) => {
93+ await move ( join ( tarDir , file ) , join ( dest , file ) , { overwrite : true } )
94+ } ) ,
95+ )
96+ await remove ( tarDir )
9397
9498 if ( stripComponents ) {
95- await stripPathComponents ( dest , folderName )
99+ await stripPathComponents ( dest )
96100 }
97101}
98102
99- async function stripPathComponents ( dest : string , folderName : string ) {
103+ async function stripPathComponents ( dest : string ) {
104+ info ( `Stripping path components from ${ dest } ` )
105+
100106 // get all subfolders in the folder
101- const subFolders = await readdir ( join ( dest , folderName ) )
107+ const toStrip = await readdir ( dest )
108+ if ( toStrip . length !== 1 ) {
109+ throw new Error ( `Expected 1 folder in ${ dest } , got ${ toStrip . length } ` )
110+ }
111+ const subFolder = toStrip [ 0 ]
112+ const subFolderPath = join ( dest , subFolder )
113+ const subFolderStat = await stat ( subFolderPath )
114+ if ( ! subFolderStat . isDirectory ( ) ) {
115+ // if the subfolder is not a directory, do nothing
116+ warning ( `Expected ${ subFolderPath } to be a directory, got ${ subFolderStat } .` )
117+ return
118+ }
119+ // for each child of the subfolder, move all files to the destination
120+ const subFiles = await readdir ( subFolderPath )
102121 await Promise . all (
103- subFolders . map ( async ( subFolder ) => {
104- const subFolderPath = join ( dest , subFolder )
105- if ( ! ( await stat ( subFolderPath ) ) . isDirectory ( ) ) {
106- // if the subfolder is not a directory, do nothing
107- return
108- }
109- // for each subfolder, move all files to the destination
110- const subFiles = await readdir ( subFolderPath )
111- await Promise . all (
112- subFiles . map ( ( subFile ) => {
113- return move ( join ( subFolderPath , subFile ) , join ( dest , subFile ) , { overwrite : true } )
114- } ) ,
115- )
116- // remove the subfolder
117- await remove ( subFolderPath )
118- return
122+ subFiles . map ( ( subFile ) => {
123+ return move ( join ( subFolderPath , subFile ) , join ( dest , subFile ) , { overwrite : true } )
119124 } ) ,
120125 )
126+ // remove the subfolder
127+ await remove ( subFolderPath )
121128}
122129
123130async function run7zip ( file : string , dest : string ) {
0 commit comments