MCPcopy Index your code
hub / github.com/ionic-team/capacitor / updateFile

Function updateFile

cli/src/tasks/migrate.ts:532–580  ·  view source on GitHub ↗
(
  config: Config,
  filename: string,
  textStart: string,
  textEnd: string,
  replacement?: string,
  skipIfNotFound?: boolean,
)

Source from the content-addressed store, hash-verified

530}
531
532async function updateFile(
533 config: Config,
534 filename: string,
535 textStart: string,
536 textEnd: string,
537 replacement?: string,
538 skipIfNotFound?: boolean,
539): Promise<boolean> {
540 if (config === null) {
541 return false;
542 }
543 const path = filename;
544 let txt = readFile(path);
545 if (!txt) {
546 return false;
547 }
548 if (txt.includes(textStart)) {
549 if (replacement) {
550 txt = setAllStringIn(txt, textStart, textEnd, replacement);
551 writeFileSync(path, txt, { encoding: 'utf-8' });
552 } else {
553 // Replacing in code so we need to count the number of brackets to find the end of the function in swift
554 const lines = txt.split('\n');
555 let replaced = '';
556 let keep = true;
557 let brackets = 0;
558 for (const line of lines) {
559 if (line.includes(textStart)) {
560 keep = false;
561 }
562 if (!keep) {
563 brackets += (line.match(/{/g) || []).length;
564 brackets -= (line.match(/}/g) || []).length;
565 if (brackets == 0) {
566 keep = true;
567 }
568 } else {
569 replaced += line + '\n';
570 }
571 }
572 writeFileSync(path, replaced, { encoding: 'utf-8' });
573 }
574 return true;
575 } else if (!skipIfNotFound) {
576 logger.error(`Unable to find "${textStart}" in ${filename}. Try updating it manually`);
577 }
578
579 return false;
580}
581
582export function setAllStringIn(data: string, start: string, end: string, replacement: string): string {
583 let position = 0;

Callers 1

migrateCommandFunction · 0.85

Calls 4

readFileFunction · 0.85
setAllStringInFunction · 0.85
matchMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected