async function unzipVSCode()

in lib/download.ts [107:153]


async function unzipVSCode(reporter: ProgressReporter, extractDir: string, extractSync: boolean, stream: Readable, format: 'zip' | 'tgz') {
	const stagingFile = path.join(tmpdir(), `vscode-test-${Date.now()}.zip`);

	if (format === 'zip') {
		// note: this used to use Expand-Archive, but this caused a failure
		// on longer file paths on windows. Instead use unzipper, which does
		// not have this limitation.
		//
		// However it has problems that prevent it working on OSX:
		// - https://github.com/ZJONSSON/node-unzipper/issues/216 (avoidable)
		// - https://github.com/ZJONSSON/node-unzipper/issues/115 (not avoidable)
		if (process.platform === 'win32' && extractSync) {
			try {
				await promisify(pipeline)(stream, fs.createWriteStream(stagingFile));
				reporter.report({ stage: ProgressReportStage.ExtractingSynchonrously });
				await spawnDecompressorChild('powershell.exe', [
					'-NoProfile', '-ExecutionPolicy', 'Bypass', '-NonInteractive', '-NoLogo',
					'-Command', `Microsoft.PowerShell.Archive\\Expand-Archive -Path "${stagingFile}" -DestinationPath "${extractDir}"`
				]);
			} finally {
				fs.unlink(stagingFile, () => undefined);
			}
		} else if (process.platform !== 'darwin' && !extractSync) {
			await new Promise((resolve, reject) =>
				stream
					.pipe(extract({ path: extractDir }))
					.on('close', resolve)
					.on('error', reject)
			);
		}  else { // darwin or *nix sync
			try {
				await promisify(pipeline)(stream, fs.createWriteStream(stagingFile));
				reporter.report({ stage: ProgressReportStage.ExtractingSynchonrously });
				await spawnDecompressorChild('unzip', ['-q', stagingFile, '-d', extractDir]);
			} finally {
				fs.unlink(stagingFile, () => undefined);
			}
		}
	} else {
		// tar does not create extractDir by default
		if (!fs.existsSync(extractDir)) {
			fs.mkdirSync(extractDir);
		}

		await spawnDecompressorChild('tar', ['-xzf', '-', '-C', extractDir], stream);
	}
}