build/azure-pipelines/common/createAsset.ts/main
async function main(): Promise<void> {
	const [, , product, os, arch, unprocessedType, fileName, filePath] = process.argv;
	// getPlatform needs the unprocessedType
	const platform = getPlatform(product, os, arch, unprocessedType);
	const type = getRealType(unprocessedType);
	const quality = getEnv('VSCODE_QUALITY');
	const commit = process.env['VSCODE_DISTRO_COMMIT'] || getEnv('BUILD_SOURCEVERSION');

	console.log('Creating asset...');

	const stat = await new Promise<fs.Stats>((c, e) => fs.stat(filePath, (err, stat) => err ? e(err) : c(stat)));
	const size = stat.size;

	console.log('Size:', size);

	const stream = fs.createReadStream(filePath);
	const [sha1hash, sha256hash] = await Promise.all([hashStream('sha1', stream), hashStream('sha256', stream)]);

	console.log('SHA1:', sha1hash);
	console.log('SHA256:', sha256hash);

	const blobName = commit + '/' + fileName;

	const storagePipelineOptions: StoragePipelineOptions = { retryOptions: { retryPolicyType: StorageRetryPolicyType.EXPONENTIAL, maxTries: 6, tryTimeoutInMs: 10 * 60 * 1000 } };

	const credential = new ClientSecretCredential(process.env['AZURE_TENANT_ID']!, process.env['AZURE_CLIENT_ID']!, process.env['AZURE_CLIENT_SECRET']!);
	const blobServiceClient = new BlobServiceClient(`https://vscode.blob.core.windows.net`, credential, storagePipelineOptions);
	const containerClient = blobServiceClient.getContainerClient(quality);
	const blobClient = containerClient.getBlockBlobClient(blobName);

	const blobOptions: BlockBlobParallelUploadOptions = {
		blobHTTPHeaders: {
			blobContentType: mime.lookup(filePath),
			blobContentDisposition: `attachment; filename="${fileName}"`,
			blobCacheControl: 'max-age=31536000, public'
		}
	};

	const uploadPromises: Promise<void>[] = [];
	if (await blobClient.exists()) {
		console.log(`Blob ${quality}, ${blobName} already exists, not publishing again.`);
	} else {
		uploadPromises.push(retry(async () => {
			await blobClient.uploadFile(filePath, blobOptions);
			console.log('Blob successfully uploaded to Azure storage.');
		}));
	}

	const shouldUploadToMooncake = /true/i.test(process.env['VSCODE_PUBLISH_TO_MOONCAKE'] ?? 'true');

	if (shouldUploadToMooncake) {
		const mooncakeCredential = new ClientSecretCredential(process.env['AZURE_MOONCAKE_TENANT_ID']!, process.env['AZURE_MOONCAKE_CLIENT_ID']!, process.env['AZURE_MOONCAKE_CLIENT_SECRET']!);
		const mooncakeBlobServiceClient = new BlobServiceClient(`https://vscode.blob.core.chinacloudapi.cn`, mooncakeCredential, storagePipelineOptions);
		const mooncakeContainerClient = mooncakeBlobServiceClient.getContainerClient(quality);
		const mooncakeBlobClient = mooncakeContainerClient.getBlockBlobClient(blobName);

		if (await mooncakeBlobClient.exists()) {
			console.log(`Mooncake Blob ${quality}, ${blobName} already exists, not publishing again.`);
		} else {
			uploadPromises.push(retry(async () => {
				await mooncakeBlobClient.uploadFile(filePath, blobOptions);
				console.log('Blob successfully uploaded to Mooncake Azure storage.');
			}));
		}

		if (uploadPromises.length) {
			console.log('Uploading blobs to Azure storage and Mooncake Azure storage...');
		}
	} else {
		if (uploadPromises.length) {
			console.log('Uploading blobs to Azure storage...');
		}
	}

	await Promise.all(uploadPromises);

	console.log(uploadPromises.length ? 'All blobs successfully uploaded.' : 'No blobs to upload.');

	const assetUrl = `${process.env['AZURE_CDN_URL']}/${quality}/${blobName}`;
	const blobPath = new URL(assetUrl).pathname;
	const mooncakeUrl = `${process.env['MOONCAKE_CDN_URL']}${blobPath}`;

	const asset: Asset = {
		platform,
		type,
		url: assetUrl,
		hash: sha1hash,
		mooncakeUrl,
		sha256hash,
		size
	};

	// Remove this if we ever need to rollback fast updates for windows
	if (/win32/.test(platform)) {
		asset.supportsFastUpdate = true;
	}

	console.log('Asset:', JSON.stringify(asset, null, '  '));

	const client = new CosmosClient({ endpoint: process.env['AZURE_DOCUMENTDB_ENDPOINT']!, aadCredentials: credential });
	const scripts = client.database('builds').container(quality).scripts;
	await retry(() => scripts.storedProcedure('createAsset').execute('', [commit, asset, true]));

	console.log(`  Done ✔️`);
}