Skip to content

Commit 8738736

Browse files
committed
extract diff rendering from apply/deploy
1 parent ddbc4dc commit 8738736

File tree

3 files changed

+79
-147
lines changed

3 files changed

+79
-147
lines changed

packages/wrangler/src/cloudchamber/apply.ts

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
success,
1212
updateStatus,
1313
} from "@cloudflare/cli";
14-
import { bold, brandColor, dim, green, red } from "@cloudflare/cli/colors";
14+
import { bold, brandColor, dim, green } from "@cloudflare/cli/colors";
1515
import {
1616
ApiError,
1717
ApplicationsService,
@@ -27,7 +27,6 @@ import { FatalError, UserError } from "../errors";
2727
import { getAccountId } from "../user";
2828
import { cleanForInstanceType, promiseSpinner } from "./common";
2929
import {
30-
createLine,
3130
diffLines,
3231
printLine,
3332
sortObjectRecursive,
@@ -421,76 +420,7 @@ export async function apply(
421420
false
422421
);
423422

424-
let printedLines: string[] = [];
425-
let printedDiff = false;
426-
// prints the lines we accumulated to bring context to the edited line
427-
const printContext = () => {
428-
let index = 0;
429-
for (let i = printedLines.length - 1; i >= 0; i--) {
430-
if (printedLines[i].trim().startsWith("[")) {
431-
log("");
432-
index = i;
433-
break;
434-
}
435-
}
436-
437-
for (let i = index; i < printedLines.length; i++) {
438-
log(printedLines[i]);
439-
if (printedLines.length - i > 2) {
440-
i = printedLines.length - 2;
441-
printLine(dim("..."), " ");
442-
}
443-
}
444-
445-
printedLines = [];
446-
};
447-
448-
// go line by line and print diff results
449-
for (const lines of results) {
450-
const trimmedLines = (lines.value ?? "")
451-
.split("\n")
452-
.map((e) => e.trim())
453-
.filter((e) => e !== "");
454-
455-
for (const l of trimmedLines) {
456-
if (lines.added) {
457-
printContext();
458-
if (l.startsWith("[")) {
459-
printLine("");
460-
}
461-
462-
printedDiff = true;
463-
printLine(l, green("+ "));
464-
} else if (lines.removed) {
465-
printContext();
466-
if (l.startsWith("[")) {
467-
printLine("");
468-
}
469-
470-
printedDiff = true;
471-
printLine(l, red("- "));
472-
} else {
473-
// if we had printed a diff before this line, print a little bit more
474-
// so the user has a bit more context on where the edit happens
475-
if (printedDiff) {
476-
let printDots = false;
477-
if (l.startsWith("[")) {
478-
printLine("");
479-
printDots = true;
480-
}
481-
482-
printedDiff = false;
483-
printLine(l, " ");
484-
if (printDots) {
485-
printLine(dim("..."), " ");
486-
}
487-
continue;
488-
}
489-
490-
printedLines.push(createLine(l, " "));
491-
}
492-
}
493-
}
423+
renderDiff(results);
494424

495425
if (appConfigNoDefaults.rollout_kind !== "none") {
496426
actions.push({

packages/wrangler/src/cloudchamber/helpers/diff.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// It's been simplified so it can basically do line diffing only
33
// and we can avoid the 600kb sized package.
44
import { log } from "@cloudflare/cli";
5-
import { bold, brandColor, red } from "@cloudflare/cli/colors";
5+
import { bold, brandColor, dim, green, red } from "@cloudflare/cli/colors";
66

77
class Diff {
88
diff(oldString: string[], newString: string[], callback: Callback) {
@@ -488,3 +488,76 @@ export function sortObjectRecursive<T = Record<string | number, unknown>>(
488488

489489
return sortObjectKeys(objectCopy) as T;
490490
}
491+
492+
export const renderDiff = (results: Result[]) => {
493+
let printedLines: string[] = [];
494+
let printedDiff = false;
495+
// prints the lines we accumulated to bring context to the edited line
496+
const printContext = () => {
497+
let index = 0;
498+
for (let i = printedLines.length - 1; i >= 0; i--) {
499+
if (printedLines[i].trim().startsWith("[")) {
500+
log("");
501+
index = i;
502+
break;
503+
}
504+
}
505+
506+
for (let i = index; i < printedLines.length; i++) {
507+
log(printedLines[i]);
508+
if (printedLines.length - i > 2) {
509+
i = printedLines.length - 2;
510+
printLine(dim("..."), " ");
511+
}
512+
}
513+
514+
printedLines = [];
515+
};
516+
517+
// go line by line and print diff results
518+
for (const lines of results) {
519+
const trimmedLines = (lines.value ?? "")
520+
.split("\n")
521+
.map((e) => e.trim())
522+
.filter((e) => e !== "");
523+
524+
for (const l of trimmedLines) {
525+
if (lines.added) {
526+
printContext();
527+
if (l.startsWith("[")) {
528+
printLine("");
529+
}
530+
531+
printedDiff = true;
532+
printLine(l, green("+ "));
533+
} else if (lines.removed) {
534+
printContext();
535+
if (l.startsWith("[")) {
536+
printLine("");
537+
}
538+
539+
printedDiff = true;
540+
printLine(l, red("- "));
541+
} else {
542+
// if we had printed a diff before this line, print a little bit more
543+
// so the user has a bit more context on where the edit happens
544+
if (printedDiff) {
545+
let printDots = false;
546+
if (l.startsWith("[")) {
547+
printLine("");
548+
printDots = true;
549+
}
550+
551+
printedDiff = false;
552+
printLine(l, " ");
553+
if (printDots) {
554+
printLine(dim("..."), " ");
555+
}
556+
continue;
557+
}
558+
559+
printedLines.push(createLine(l, " "));
560+
}
561+
}
562+
}
563+
};

packages/wrangler/src/containers/deploy.ts

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
success,
1111
updateStatus,
1212
} from "@cloudflare/cli";
13-
import { bold, brandColor, dim, green, red } from "@cloudflare/cli/colors";
13+
import { bold, brandColor, dim, green } from "@cloudflare/cli/colors";
1414
import {
1515
ApiError,
1616
ApplicationsService,
@@ -23,9 +23,9 @@ import {
2323
} from "@cloudflare/containers-shared";
2424
import { cleanForInstanceType, promiseSpinner } from "../cloudchamber/common";
2525
import {
26-
createLine,
2726
diffLines,
2827
printLine,
28+
renderDiff,
2929
sortObjectRecursive,
3030
stripUndefined,
3131
} from "../cloudchamber/helpers/diff";
@@ -380,78 +380,7 @@ export async function apply(
380380
`${brandColor.underline("EDIT")} ${application.name}`,
381381
false
382382
);
383-
384-
let printedLines: string[] = [];
385-
let printedDiff = false;
386-
// prints the lines we accumulated to bring context to the edited line
387-
const printContext = () => {
388-
let index = 0;
389-
for (let i = printedLines.length - 1; i >= 0; i--) {
390-
if (printedLines[i].trim().startsWith("[")) {
391-
log("");
392-
index = i;
393-
break;
394-
}
395-
}
396-
397-
for (let i = index; i < printedLines.length; i++) {
398-
log(printedLines[i]);
399-
if (printedLines.length - i > 2) {
400-
i = printedLines.length - 2;
401-
printLine(dim("..."), " ");
402-
}
403-
}
404-
405-
printedLines = [];
406-
};
407-
408-
// go line by line and print diff results
409-
for (const lines of results) {
410-
const trimmedLines = (lines.value ?? "")
411-
.split("\n")
412-
.map((e) => e.trim())
413-
.filter((e) => e !== "");
414-
415-
for (const l of trimmedLines) {
416-
if (lines.added) {
417-
printContext();
418-
if (l.startsWith("[")) {
419-
printLine("");
420-
}
421-
422-
printedDiff = true;
423-
printLine(l, green("+ "));
424-
} else if (lines.removed) {
425-
printContext();
426-
if (l.startsWith("[")) {
427-
printLine("");
428-
}
429-
430-
printedDiff = true;
431-
printLine(l, red("- "));
432-
} else {
433-
// if we had printed a diff before this line, print a little bit more
434-
// so the user has a bit more context on where the edit happens
435-
if (printedDiff) {
436-
let printDots = false;
437-
if (l.startsWith("[")) {
438-
printLine("");
439-
printDots = true;
440-
}
441-
442-
printedDiff = false;
443-
printLine(l, " ");
444-
if (printDots) {
445-
printLine(dim("..."), " ");
446-
}
447-
continue;
448-
}
449-
450-
printedLines.push(createLine(l, " "));
451-
}
452-
}
453-
}
454-
383+
renderDiff(results);
455384
if (appConfigNoDefaults.rollout_kind !== "none") {
456385
actions.push({
457386
action: "modify",

0 commit comments

Comments
 (0)