export async function download()

in lib/download.ts [173:250]


export async function download(options: Partial<DownloadOptions> = {}): Promise<string> {
	let version = options?.version;
	const {
		platform = systemDefaultPlatform,
		architecture = systemDefaultArchitecture,
		cachePath = defaultCachePath,
		reporter = new ConsoleReporter(process.stdout.isTTY),
		extractSync = false,
	} = options;

	if (version) {
		if (version === 'stable') {
			version = await fetchLatestStableVersion();
		} else {
			/**
			 * Only validate version against server when no local download that matches version exists
			 */
			if (!fs.existsSync(path.resolve(cachePath, `vscode-${platform}-${version}`))) {
				if (!(await isValidVersion(version))) {
					throw Error(`Invalid version ${version}`);
				}
			}
		}
	} else {
		version = await fetchLatestStableVersion();
	}

	reporter.report({ stage: ProgressReportStage.ResolvedVersion, version });

	const downloadedPath = path.resolve(cachePath, `vscode-${platform}-${version}`);
	if (fs.existsSync(downloadedPath)) {
		if (version === 'insiders') {
			reporter.report({ stage: ProgressReportStage.FetchingInsidersMetadata });
			const { version: currentHash, date: currentDate } = insidersDownloadDirMetadata(downloadedPath);

			const { version: latestHash, timestamp: latestTimestamp } = await getLatestInsidersMetadata(
				systemDefaultPlatform
			);
			if (currentHash === latestHash) {
				reporter.report({ stage: ProgressReportStage.FoundMatchingInstall, downloadedPath });
				return Promise.resolve(insidersDownloadDirToExecutablePath(downloadedPath));
			} else {
				try {
					reporter.report({
						stage: ProgressReportStage.ReplacingOldInsiders,
						downloadedPath,
						oldDate: currentDate,
						oldHash: currentHash,
						newDate: new Date(latestTimestamp),
						newHash: latestHash,
					});
					await del.rmdir(downloadedPath);
				} catch (err) {
					reporter.error(err);
					throw Error(`Failed to remove outdated Insiders at ${downloadedPath}.`);
				}
			}
		} else {
			reporter.report({ stage: ProgressReportStage.FoundMatchingInstall, downloadedPath });
			return Promise.resolve(downloadDirToExecutablePath(downloadedPath));
		}
	}

	try {
		const { stream, format } = await downloadVSCodeArchive({ version, architecture, platform, cachePath, reporter });
		await unzipVSCode(reporter, downloadedPath, extractSync, stream, format);
		reporter.report({ stage: ProgressReportStage.NewInstallComplete, downloadedPath })
	} catch (err) {
		reporter.error(err);
		throw Error(`Failed to download and unzip VS Code ${version}`);
	}

	if (version === 'insiders') {
		return Promise.resolve(insidersDownloadDirToExecutablePath(downloadedPath));
	} else {
		return downloadDirToExecutablePath(downloadedPath);
	}
}