xref: /expo/packages/@expo/cli/src/utils/tar.ts (revision bb5069cd)
1import spawnAsync from '@expo/spawn-async';
2import tar from 'tar';
3
4import * as Log from '../log';
5
6const debug = require('debug')('expo:utils:tar') as typeof console.log;
7
8/** Extract a tar using built-in tools if available and falling back on Node.js. */
9export async function extractAsync(input: string, output: string): Promise<void> {
10  try {
11    if (process.platform !== 'win32') {
12      debug(`Extracting ${input} to ${output}`);
13      await spawnAsync('tar', ['-xf', input, '-C', output], {
14        stdio: 'inherit',
15      });
16      return;
17    }
18  } catch (error: any) {
19    Log.warn(
20      `Failed to extract tar using native tools, falling back on JS tar module. ${error.message}`
21    );
22  }
23  debug(`Extracting ${input} to ${output} using JS tar module`);
24  // tar node module has previously had problems with big files, and seems to
25  // be slower, so only use it as a backup.
26  await tar.extract({ file: input, cwd: output });
27}
28