Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/inlineMeta.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import es from 'event-stream';
7
import { basename } from 'path';
8
import File from 'vinyl';
9
10
export interface IInlineMetaContext {
11
readonly targetPaths: string[];
12
readonly packageJsonFn: () => string;
13
readonly productJsonFn: () => string;
14
}
15
16
const packageJsonMarkerId = 'BUILD_INSERT_PACKAGE_CONFIGURATION';
17
18
// TODO in order to inline `product.json`, more work is
19
// needed to ensure that we cover all cases where modifications
20
// are done to the product configuration during build. There are
21
// at least 2 more changes that kick in very late:
22
// - a `darwinUniversalAssetId` is added in`create-universal-app.ts`
23
// - a `target` is added in `gulpfile.vscode.win32.js`
24
// const productJsonMarkerId = 'BUILD_INSERT_PRODUCT_CONFIGURATION';
25
26
export function inlineMeta(result: NodeJS.ReadWriteStream, ctx: IInlineMetaContext): NodeJS.ReadWriteStream {
27
return result.pipe(es.through(function (file: File) {
28
if (matchesFile(file, ctx)) {
29
let content = file.contents!.toString();
30
let markerFound = false;
31
32
const packageMarker = `${packageJsonMarkerId}:"${packageJsonMarkerId}"`; // this needs to be the format after esbuild has processed the file (e.g. double quotes)
33
if (content.includes(packageMarker)) {
34
content = content.replace(packageMarker, JSON.stringify(JSON.parse(ctx.packageJsonFn())).slice(1, -1) /* trim braces */);
35
markerFound = true;
36
}
37
38
// const productMarker = `${productJsonMarkerId}:"${productJsonMarkerId}"`; // this needs to be the format after esbuild has processed the file (e.g. double quotes)
39
// if (content.includes(productMarker)) {
40
// content = content.replace(productMarker, JSON.stringify(JSON.parse(ctx.productJsonFn())).slice(1, -1) /* trim braces */);
41
// markerFound = true;
42
// }
43
44
if (markerFound) {
45
file.contents = Buffer.from(content);
46
}
47
}
48
49
this.emit('data', file);
50
}));
51
}
52
53
function matchesFile(file: File, ctx: IInlineMetaContext): boolean {
54
for (const targetPath of ctx.targetPaths) {
55
if (file.basename === basename(targetPath)) { // TODO would be nicer to figure out root relative path to not match on false positives
56
return true;
57
}
58
}
59
return false;
60
}
61
62