Skip to content

Commit

Permalink
feat: start looking for optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Aug 31, 2023
1 parent 0977b3b commit 3a355ac
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 16 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@prismicio/types-internal": "^2.0.0",
"@slicemachine/manager": "^0.9.0",
"chalk": "^4.1.2",
"diff": "^5.1.0",
"listr": "^0.14.3",
"log-symbols": "^4.1.0",
"meow": "^12.1.0",
Expand All @@ -78,6 +79,7 @@
"devDependencies": {
"@size-limit/preset-small-lib": "^8.2.6",
"@trivago/prettier-plugin-sort-imports": "^4.2.0",
"@types/diff": "^5.0.3",
"@types/listr": "0.14.4",
"@types/prompts": "2.4.4",
"@typescript-eslint/eslint-plugin": "^6.4.0",
Expand Down
7 changes: 4 additions & 3 deletions playground/slicemachine.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"__repositoryName": "inspiring-briouat-syt67g",
"_repositoryName": "2306-meetup-figma",
"repositoryName": "upgrade-no-conflict-full-legacy",
"___repositoryName": "inspiring-briouat-syt67g",
"__repositoryName": "2306-meetup-figma",
"repositoryName": "upgrade-optimize-full-legacy",
"_repositoryName": "upgrade-no-conflict-full-legacy",
"adapter": {
"resolve": "@slicemachine/adapter-next",
"options": {
Expand Down
54 changes: 42 additions & 12 deletions src/UpgradeProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import { CustomType } from "./models/CustomType";
import { SharedSlice } from "./models/SharedSlice";
import {
SliceConflicts,
findSliceConflicts,
} from "./models/findSliceConflicts";
checkSliceConflicts,
} from "./models/checkSliceConflicts";
import { findDuplicatedSlices } from "./models/findDuplicatedSlices";

export type UpgradeProcessOptions = {
cwd?: string;
Expand Down Expand Up @@ -63,7 +64,7 @@ export class UpgradeProcess {
await this.loginAndFetchUserData();
await this.fetchRepository();
await this.searchCompositeSlices();
await this.checkConflicts();
await this.checkSliceConflicts();

assertExists(
this.context.conflicts,
Expand Down Expand Up @@ -112,7 +113,7 @@ export class UpgradeProcess {
await this.loginAndFetchUserData();
await this.fetchRepository();
await this.searchCompositeSlices();
await this.checkConflicts();
await this.checkSliceConflicts();

assertExists(
this.context.conflicts,
Expand All @@ -139,7 +140,7 @@ export class UpgradeProcess {
// We prefer to manually allow console logs despite the app being a CLI to catch wild/unwanted console logs better
// eslint-disable-next-line no-console
console.log(
`\n${chalk.bgBlue(` ${chalk.bold.white("Prismic")} `)} ${chalk.dim(
`\n${chalk.bgCyan(` ${chalk.bold.white("Prismic")} `)} ${chalk.dim(
"→",
)} ${message}\n`,
);
Expand Down Expand Up @@ -330,7 +331,7 @@ export class UpgradeProcess {
]);
}

protected async checkConflicts(): Promise<void> {
protected async checkSliceConflicts(): Promise<void> {
return listrRun([
{
title: "Checking conflicts...",
Expand All @@ -344,7 +345,7 @@ export class UpgradeProcess {
"Repository Composite Slices (legacy) must be available through context to proceed",
);

this.context.conflicts = findSliceConflicts([
this.context.conflicts = checkSliceConflicts([
...this.context.repository.sharedSlices,
...this.context.repository.compositeSlices,
]);
Expand Down Expand Up @@ -373,9 +374,11 @@ export class UpgradeProcess {
output.push(
` ${chalk.dim("(shared slice)")} ${chalk.cyan(
"<slice-type>",
)}\n ${chalk.dim("(composite slice)")} ${chalk.dim(
"<custom-type>::<tab>::<field>::",
)}${chalk.cyan("<slice-type>")}`,
)} ${chalk.dim("(maybe-suffix)")}\n ${chalk.dim(
"(composite slice)",
)} ${chalk.dim("<custom-type>::<tab>::<field>::")}${chalk.cyan(
"<slice-type>",
)} ${chalk.dim("(maybe-suffix)")}`,
);

for (const id in this.context.conflicts) {
Expand All @@ -385,19 +388,46 @@ export class UpgradeProcess {
`\n\n ${logSymbols.error} ${chalk.cyan(id)} is conflicting:\n`,
);

const duplicatedSlices = findDuplicatedSlices(conflict);
const allDuplicated =
duplicatedSlices.length === 1 &&
duplicatedSlices[0].length === conflict.length;

for (const slice of conflict) {
const maybeIndex =
duplicatedSlices.findIndex((slices) => slices.includes(slice)) + 1;

const suffix = maybeIndex
? ` ${chalk.hsl(maybeIndex * 45, 50, 50)(`(d${maybeIndex})`)}`
: "";
if (slice instanceof SharedSlice) {
output.push(
` ${chalk.dim("(shared slice)")} ${chalk.cyan(slice.id)} `,
` ${chalk.dim("(shared slice)")} ${chalk.cyan(
slice.id,
)}${suffix}`,
);
} else {
output.push(
` ${chalk.dim("(composite slice)")} ${chalk.dim(
`${slice.meta.parent.id}::${slice.meta.path.tabID}::${slice.meta.path.sliceZoneID}::`,
)}${chalk.cyan(slice.id)}`,
)}${chalk.cyan(slice.id)}${suffix}`,
);
}
}

if (allDuplicated) {
output.push(`\n ${chalk.dim("All slices are identical!")}`);
} else if (duplicatedSlices.length) {
output.push(
`\n ${chalk.dim(
`(d${chalk.italic(
duplicatedSlices.length === 1
? "1"
: `1-${duplicatedSlices.length}`,
)}) suffixed slices are identical to slices with the same suffix`,
)}`,
);
}
}

return output.join("\n");
Expand Down
46 changes: 46 additions & 0 deletions src/models/CompositeSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CompositeSlice as CompositeSliceDefinition } from "@prismicio/types-internal/lib/customtypes";

import { CustomType } from "./CustomType";
import { SharedSlice } from "./SharedSlice";

export type CompositeSliceMeta = {
parent: CustomType;
Expand All @@ -26,4 +27,49 @@ export class CompositeSlice {
this.definition = compositeSlice;
this.meta = meta;
}

diff(slice: CompositeSlice | SharedSlice): {
primary: string[];
items: string[];
} {
const primary: string[] = [];
const items: string[] = [];

if (slice instanceof SharedSlice) {
// TODO
} else if (
JSON.stringify(this.definition) !== JSON.stringify(slice.definition)
) {
const nonRepeatKeys = Object.keys({
...this.definition["non-repeat"],
...slice.definition["non-repeat"],
});
for (const key of nonRepeatKeys) {
if (
this.definition["non-repeat"]?.[key]?.type !==
slice.definition["non-repeat"]?.[key]?.type
) {
primary.push(key);
}
}

const repeatKeys = Object.keys({
...this.definition.repeat,
...slice.definition.repeat,
});
for (const key of repeatKeys) {
if (
this.definition.repeat?.[key]?.type !==
slice.definition.repeat?.[key]?.type
) {
items.push(key);
}
}
}

return {
primary,
items,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SharedSlice } from "./SharedSlice";

export type SliceConflicts = Record<string, (CompositeSlice | SharedSlice)[]>;

export const findSliceConflicts = (
export const checkSliceConflicts = (
slices: (SharedSlice | CompositeSlice)[],
): SliceConflicts => {
const ids: Record<string, SharedSlice | CompositeSlice> = {};
Expand Down
31 changes: 31 additions & 0 deletions src/models/findDuplicatedSlices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CompositeSlice } from "./CompositeSlice";
import { SharedSlice } from "./SharedSlice";

export const findDuplicatedSlices = (
slices: (CompositeSlice | SharedSlice)[],
): (CompositeSlice | SharedSlice)[][] => {
const duplicatedSlices: (CompositeSlice | SharedSlice)[][] = [];

for (const slice of slices) {
const maybeDuplicatedSlices = duplicatedSlices.find((slices) => {
if (slice instanceof SharedSlice) {
// TODO
} else {
const diff = slice.diff(slices[0]);

// All fields are alike
return !diff.primary.length && !diff.items.length;
}

return false;
});

if (maybeDuplicatedSlices) {
maybeDuplicatedSlices.push(slice);
} else {
duplicatedSlices.push([slice]);
}
}

return duplicatedSlices.filter((slices) => slices.length > 1);
};

0 comments on commit 3a355ac

Please sign in to comment.