xref: /expo/packages/@expo/cli/src/utils/tar.ts (revision 474a7a4b)
18d307f52SEvan Baconimport spawnAsync from '@expo/spawn-async';
28d307f52SEvan Baconimport tar from 'tar';
38d307f52SEvan Bacon
48d307f52SEvan Baconimport * as Log from '../log';
58d307f52SEvan Bacon
6*474a7a4bSEvan Baconconst debug = require('debug')('expo:utils:tar') as typeof console.log;
7*474a7a4bSEvan Bacon
88d307f52SEvan Bacon/** Extract a tar using built-in tools if available and falling back on Node.js. */
98d307f52SEvan Baconexport async function extractAsync(input: string, output: string): Promise<void> {
108d307f52SEvan Bacon  try {
118d307f52SEvan Bacon    if (process.platform !== 'win32') {
12*474a7a4bSEvan Bacon      debug(`Extracting ${input} to ${output}`);
138d307f52SEvan Bacon      await spawnAsync('tar', ['-xf', input, '-C', output], {
148d307f52SEvan Bacon        stdio: 'inherit',
158d307f52SEvan Bacon      });
168d307f52SEvan Bacon      return;
178d307f52SEvan Bacon    }
1829975bfdSEvan Bacon  } catch (error: any) {
198d307f52SEvan Bacon    Log.warn(
2029975bfdSEvan Bacon      `Failed to extract tar using native tools, falling back on JS tar module. ${error.message}`
218d307f52SEvan Bacon    );
228d307f52SEvan Bacon  }
23*474a7a4bSEvan Bacon  debug(`Extracting ${input} to ${output} using JS tar module`);
248d307f52SEvan Bacon  // tar node module has previously had problems with big files, and seems to
258d307f52SEvan Bacon  // be slower, so only use it as a backup.
268d307f52SEvan Bacon  await tar.extract({ file: input, cwd: output });
278d307f52SEvan Bacon}
28