-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathpost-build.ts
More file actions
183 lines (161 loc) · 5.39 KB
/
post-build.ts
File metadata and controls
183 lines (161 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import tsConfig from '../tsconfig.json' with {type: 'json'};
import {sed} from './sed.ts';
const BUILD_DIR = path.join(process.cwd(), 'build');
/**
* Writes content to a file.
* @param filePath The path to the file.
* @param content The content to write.
*/
function writeFile(filePath: string, content: string): void {
fs.writeFileSync(filePath, content, 'utf-8');
}
/**
* Ensures that licenses for third party files we use gets copied into the build/ dir.
*/
function copyThirdPartyLicenseFiles() {
const thirdPartyDirectories = tsConfig.include.filter(location => {
return location.includes(
'node_modules/chrome-devtools-frontend/front_end/third_party',
);
});
for (const thirdPartyDir of thirdPartyDirectories) {
const fullPath = path.join(process.cwd(), thirdPartyDir);
const licenseFile = path.join(fullPath, 'LICENSE');
if (!fs.existsSync(licenseFile)) {
console.error('No LICENSE for', path.basename(thirdPartyDir));
}
const destinationDir = path.join(BUILD_DIR, thirdPartyDir);
const destinationFile = path.join(destinationDir, 'LICENSE');
fs.copyFileSync(licenseFile, destinationFile);
}
}
function main(): void {
const devtoolsThirdPartyPath =
'node_modules/chrome-devtools-frontend/front_end/third_party';
const devtoolsFrontEndCorePath =
'node_modules/chrome-devtools-frontend/front_end/core';
// Create i18n mock
const i18nDir = path.join(BUILD_DIR, devtoolsFrontEndCorePath, 'i18n');
fs.mkdirSync(i18nDir, {recursive: true});
const i18nFile = path.join(i18nDir, 'i18n.js');
const i18nContent = `
export const i18n = {
registerUIStrings: () => {},
getLocalizedString: (_, str) => {
// So that the string passed in gets output verbatim.
return str;
},
lockedLazyString: () => {},
getLazilyComputedLocalizedString: () => ()=>{},
};
// TODO(jacktfranklin): once the DocumentLatency insight does not depend on
// this method, we can remove this stub.
export const TimeUtilities = {
millisToString(x) {
const separator = '\xA0';
const formatter = new Intl.NumberFormat('en-US', {
style: 'unit',
unitDisplay: 'narrow',
minimumFractionDigits: 0,
maximumFractionDigits: 1,
unit: 'millisecond',
});
const parts = formatter.formatToParts(x);
for (const part of parts) {
if (part.type === 'literal') {
if (part.value === ' ') {
part.value = separator;
}
}
}
return parts.map(part => part.value).join('');
}
};
// TODO(jacktfranklin): once the ImageDelivery insight does not depend on this method, we can remove this stub.
export const ByteUtilities = {
bytesToString(x) {
const separator = '\xA0';
const formatter = new Intl.NumberFormat('en-US', {
style: 'unit',
unit: 'kilobyte',
unitDisplay: 'narrow',
minimumFractionDigits: 1,
maximumFractionDigits: 1,
});
const parts = formatter.formatToParts(x / 1000);
for (const part of parts) {
if (part.type === 'literal') {
if (part.value === ' ') {
part.value = separator;
}
}
}
return parts.map(part => part.value).join('');
}
};`;
writeFile(i18nFile, i18nContent);
// Create codemirror.next mock.
const codeMirrorDir = path.join(
BUILD_DIR,
devtoolsThirdPartyPath,
'codemirror.next',
);
fs.mkdirSync(codeMirrorDir, {recursive: true});
const codeMirrorFile = path.join(codeMirrorDir, 'codemirror.next.js');
const codeMirrorContent = `export default {}`;
writeFile(codeMirrorFile, codeMirrorContent);
// Create root mock
const rootDir = path.join(BUILD_DIR, devtoolsFrontEndCorePath, 'root');
fs.mkdirSync(rootDir, {recursive: true});
const runtimeFile = path.join(rootDir, 'Runtime.js');
const runtimeContent = `
export function getChromeVersion() { return ''; };
export const hostConfig = {};
`;
writeFile(runtimeFile, runtimeContent);
// Update protocol_client to remove:
// 1. self.Protocol assignment
// 2. Call to register backend commands.
const protocolClientDir = path.join(
BUILD_DIR,
devtoolsFrontEndCorePath,
'protocol_client',
);
const clientFile = path.join(protocolClientDir, 'protocol_client.js');
const globalAssignment = /self\.Protocol = self\.Protocol \|\| \{\};/;
const registerCommands =
/InspectorBackendCommands\.registerCommands\(InspectorBackend\.inspectorBackend\);/;
sed(clientFile, globalAssignment, '');
sed(clientFile, registerCommands, '');
const devtoolsLicensePath = path.join(
'node_modules',
'chrome-devtools-frontend',
'LICENSE',
);
const devtoolsLicenseFileSource = path.join(
process.cwd(),
devtoolsLicensePath,
);
const devtoolsLicenseFileDestination = path.join(
BUILD_DIR,
devtoolsLicensePath,
);
fs.copyFileSync(devtoolsLicenseFileSource, devtoolsLicenseFileDestination);
copyThirdPartyLicenseFiles();
copyDevToolsDescriptionFiles();
}
function copyDevToolsDescriptionFiles() {
const devtoolsIssuesDescriptionPath =
'node_modules/chrome-devtools-frontend/front_end/models/issues_manager/descriptions';
const sourceDir = path.join(process.cwd(), devtoolsIssuesDescriptionPath);
const destDir = path.join(BUILD_DIR, devtoolsIssuesDescriptionPath);
fs.cpSync(sourceDir, destDir, {recursive: true});
}
main();