/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import es from 'event-stream';6import { basename } from 'path';7import File from 'vinyl';89export interface IInlineMetaContext {10readonly targetPaths: string[];11readonly packageJsonFn: () => string;12readonly productJsonFn: () => string;13}1415const packageJsonMarkerId = 'BUILD_INSERT_PACKAGE_CONFIGURATION';1617// TODO in order to inline `product.json`, more work is18// needed to ensure that we cover all cases where modifications19// are done to the product configuration during build. There are20// at least 2 more changes that kick in very late:21// - a `darwinUniversalAssetId` is added in`create-universal-app.ts`22// - a `target` is added in `gulpfile.vscode.win32.js`23// const productJsonMarkerId = 'BUILD_INSERT_PRODUCT_CONFIGURATION';2425export function inlineMeta(result: NodeJS.ReadWriteStream, ctx: IInlineMetaContext): NodeJS.ReadWriteStream {26return result.pipe(es.through(function (file: File) {27if (matchesFile(file, ctx)) {28let content = file.contents!.toString();29let markerFound = false;3031const packageMarker = `${packageJsonMarkerId}:"${packageJsonMarkerId}"`; // this needs to be the format after esbuild has processed the file (e.g. double quotes)32if (content.includes(packageMarker)) {33content = content.replace(packageMarker, JSON.stringify(JSON.parse(ctx.packageJsonFn())).slice(1, -1) /* trim braces */);34markerFound = true;35}3637// const productMarker = `${productJsonMarkerId}:"${productJsonMarkerId}"`; // this needs to be the format after esbuild has processed the file (e.g. double quotes)38// if (content.includes(productMarker)) {39// content = content.replace(productMarker, JSON.stringify(JSON.parse(ctx.productJsonFn())).slice(1, -1) /* trim braces */);40// markerFound = true;41// }4243if (markerFound) {44file.contents = Buffer.from(content);45}46}4748this.emit('data', file);49}));50}5152function matchesFile(file: File, ctx: IInlineMetaContext): boolean {53for (const targetPath of ctx.targetPaths) {54if (file.basename === basename(targetPath)) { // TODO would be nicer to figure out root relative path to not match on false positives55return true;56}57}58return false;59}606162