MCPcopy Create free account
hub / github.com/cocos/cocos-engine / replaceInFile

Method replaceInFile

scripts/native-pack-tool/source/utils.ts:280–297  ·  view source on GitHub ↗
(patterns: { reg: string | RegExp, text: string }[], filepath: string)

Source from the content-addressed store, hash-verified

278 }
279
280 static async replaceInFile(patterns: { reg: string | RegExp, text: string }[], filepath: string) {
281 filepath = this.replaceEnvVariables(filepath);
282 if (!fs.existsSync(filepath)) {
283 console.log(`While replace template content, file ${filepath}`);
284 return;
285 }
286 // console.log(`replace ${filepath} with ${JSON.stringify(patterns)}`);
287 const lines = (await fs.readFile(filepath)).toString('utf8').split('\n');
288
289 const newContent = lines.map((l) => {
290 patterns.forEach((p) => {
291 l = l.replace(new RegExp(p.reg), this.replaceEnvVariables(p.text));
292 });
293 return l;
294 }).join('\n');
295
296 await fs.writeFile(filepath, newContent);
297 }
298
299
300 static exactValueFromFile(regexp: RegExp, filename: string, idx: number): string | undefined {

Callers 5

executeTemplateTaskFunction · 0.80
createMethod · 0.80
createMethod · 0.80

Calls 11

replaceEnvVariablesMethod · 0.95
existsSyncMethod · 0.80
logMethod · 0.80
readFileMethod · 0.80
joinMethod · 0.80
mapMethod · 0.80
replaceMethod · 0.80
writeFileMethod · 0.80
toStringMethod · 0.65
forEachMethod · 0.65
splitMethod · 0.45

Tested by

no test coverage detected