Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed and tested cli output for comparing tokens from a release and branch #366

Merged
96 changes: 47 additions & 49 deletions tools/diff-generator/src/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ program
.option(
"-otv, --old-token-version <oldVersion>",
"indicates which npm package version/github tag to pull old tokens from",
"latest",
)
.option(
"-ntv, --new-token-version <newVersion>",
"indicates which npm package version/github tag to pull new tokens from",
"latest",
)
.option(
"-otb, --old-token-branch <oldBranch>",
Expand Down Expand Up @@ -105,10 +103,10 @@ function indent(text, amount) {
* @param {object} token - the current token
* @param {object} log - the console.log object being used
*/
const printStyleRenamed = (result, token, log) => {
const printStyleRenamed = (result, token, log, i) => {
const str =
white(`"${result[token]["old-name"]}" -> `) + yellow(`"${token}"`);
log(indent(str, 1));
log(indent(str, i));
};

/**
Expand All @@ -117,13 +115,13 @@ const printStyleRenamed = (result, token, log) => {
* @param {object} token - the current token
* @param {object} log - the console.log object being used
*/
const printStyleDeprecated = (result, token, log) => {
const printStyleDeprecated = (result, token, log, i) => {
log(
indent(
yellow(`"${token}"`) +
white(": ") +
yellow(`"${result[token]["deprecated_comment"]}"`),
1,
i,
),
);
};
Expand All @@ -144,8 +142,8 @@ const printStyleColored = (token, color, log) => {
* @param {object} token - the current token
* @param {object} log - the console.log object being used
*/
const printStyleUpdated = (result, token, log) => {
log(indent(yellow(`"${token}"`), 2));
const printStyleUpdated = (result, token, log, i) => {
log(indent(yellow(`"${token}"`), i));
printNestedChanges(result[token], log);
};

Expand All @@ -165,7 +163,7 @@ async function cliCheck(originalFile, result, options) {
);
if (Object.keys(result.reverted).length > 0 && !options.y) {
printSection(
"\nalarm_clock",
"alarm_clock",
'Newly "Un-deprecated"',
Object.keys(result.reverted).length,
result.reverted,
Expand All @@ -191,7 +189,7 @@ async function cliCheck(originalFile, result, options) {
.then((response) => {
if (response.confirmation) {
console.clear();
return printReport(originalFile, result, log);
return printReport(originalFile, result, log, options);
} else {
log(
yellow(
Expand Down Expand Up @@ -225,25 +223,23 @@ function printReport(original, result, log, options) {
Object.keys(result.deleted).length +
Object.keys(result.updated.added).length +
Object.keys(result.updated.deleted).length +
Object.keys(result.updated.updated).length;
Object.keys(result.updated.updated).length +
Object.keys(result.updated.renamed).length;
log(white("\n**Tokens Changed (" + totalTokens + ")**"));
if (
options.oldTokenBranch !== undefined &&
options.newTokenBranch !== undefined
) {
log(
white(`\n${options.oldTokenBranch} | `) +
yellow(`${options.newTokenBranch}`),
);
let originalSchema = "";
let updatedSchema = "";
if (options.oldTokenBranch !== undefined) {
originalSchema = white(`\n${options.oldTokenBranch} | `);
} else if (options.oldTokenVersion !== undefined) {
originalSchema = white(`\n${options.oldTokenVersion} | `);
}
if (
options.oldTokenVersion !== undefined &&
options.newTokenVersion !== undefined
) {
log(
white(`\n${options.oldTokenVersion} | `) +
yellow(`${options.newTokenVersion}`),
);
if (options.newTokenBranch !== undefined) {
updatedSchema = yellow(`${options.newTokenBranch}`);
} else if (options.newTokenVersion !== undefined) {
updatedSchema = yellow(`${options.newTokenVersion}`);
}
if (originalSchema !== "" && updatedSchema !== "") {
log(`${originalSchema}${updatedSchema}`);
}
log(
white(
Expand All @@ -258,6 +254,7 @@ function printReport(original, result, log, options) {
result.renamed,
log,
printStyleRenamed,
1,
);
}
if (Object.keys(result.deprecated).length > 0) {
Expand All @@ -268,6 +265,7 @@ function printReport(original, result, log, options) {
result.deprecated,
log,
printStyleDeprecated,
1,
);
}
if (Object.keys(result.reverted).length > 0) {
Expand Down Expand Up @@ -306,9 +304,21 @@ function printReport(original, result, log, options) {
const totalUpdatedTokens =
Object.keys(result.updated.added).length +
Object.keys(result.updated.deleted).length +
Object.keys(result.updated.updated).length;
Object.keys(result.updated.updated).length +
Object.keys(result.updated.renamed).length;
if (totalUpdatedTokens > 0) {
printTitle("new", "Updated", totalUpdatedTokens, log);
if (Object.keys(result.updated.renamed).length > 0) {
printSection(
"new",
"Renamed Properties",
Object.keys(result.updated.renamed).length,
result.updated.renamed,
log,
printStyleRenamed,
2,
);
}
if (Object.keys(result.updated.added).length > 0) {
printSection(
"new",
Expand All @@ -317,9 +327,7 @@ function printReport(original, result, log, options) {
result.updated.added,
log,
printStyleUpdated,
white,
result.renamed,
original,
2,
);
}
if (Object.keys(result.updated.deleted).length > 0) {
Expand All @@ -330,9 +338,7 @@ function printReport(original, result, log, options) {
result.updated.deleted,
log,
printStyleUpdated,
white,
result.renamed,
original,
2,
);
}
if (Object.keys(result.updated.updated).length > 0) {
Expand All @@ -343,9 +349,7 @@ function printReport(original, result, log, options) {
result.updated.updated,
log,
printStyleUpdated,
white,
result.renamed,
original,
2,
);
}
}
Expand Down Expand Up @@ -387,8 +391,6 @@ function printTitle(emojiName, title, numTokens, log, amount) {
* @param {object} log - the console.log object being used
* @param {object} func - the styling function that will be used
* @param {object} color - the intended text color
* @param {object} renamed - the renamed tokens
* @param {object} original - the original token (json object)
*/
function printSection(
emojiName,
Expand All @@ -397,27 +399,23 @@ function printSection(
result,
log,
func,
color,
renamed,
original,
colorOrIndent,
) {
const textColor = color || white;
if (
title === "Added Properties" ||
title === "Deleted Properties" ||
title === "Updated Properties"
title === "Updated Properties" ||
title === "Renamed Properties"
) {
printTitle(emojiName, title, numTokens, log, 1);
} else {
printTitle(emojiName, title, numTokens, log, 0);
}
Object.keys(result).forEach((token) => {
if (textColor != white) {
func(token, textColor, log);
} else if (original !== undefined && renamed !== undefined) {
func(result, token, log);
if (typeof colorOrIndent !== "number") {
func(token, colorOrIndent, log);
} else {
func(result, token, log);
func(result, token, log, colorOrIndent);
}
});
log("\n");
Expand Down
4 changes: 2 additions & 2 deletions tools/diff-generator/src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/
import { detailedDiff } from "deep-object-diff";
import checkIfRenamed from "./renamed-token-detection.js";
import detectRenamedTokens from "./renamed-token-detection.js";
import detectNewTokens from "./added-token-detection.js";
import detectDeletedTokens from "./deleted-token-detection.js";
import detectDeprecatedTokens from "./deprecated-token-detection.js";
Expand All @@ -24,7 +24,7 @@ import detectUpdatedTokens from "./updated-token-detection.js";
*/
export default function tokenDiff(original, updated) {
const changes = detailedDiff(original, updated);
const renamedTokens = checkIfRenamed(original, changes.added);
const renamedTokens = detectRenamedTokens(original, changes.added);
const deprecatedTokens = detectDeprecatedTokens(renamedTokens, changes);
const newTokens = detectNewTokens(
renamedTokens,
Expand Down
67 changes: 63 additions & 4 deletions tools/diff-generator/src/lib/updated-token-detection.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function detectUpdatedTokens(
deprecatedTokens,
) {
const updatedTokens = {
renamed: {},
added: {},
deleted: {},
updated: { ...changes.updated },
Expand All @@ -45,7 +46,14 @@ export default function detectUpdatedTokens(
deprecatedTokens.deprecated[token] === undefined
) {
updatedTokens.added[token] = changes.added[token];
formatJSON(updatedTokens.added[token], token, original, renamed, false);
formatJSON(
updatedTokens,
updatedTokens.added[token],
token,
original,
renamed,
false,
);
}
});
Object.keys(changes.deleted).forEach((token) => {
Expand All @@ -56,11 +64,25 @@ export default function detectUpdatedTokens(
original[token],
).updated;
updatedTokens.deleted[token] = tokenDiff;
formatJSON(updatedTokens.deleted[token], token, original, renamed, false);
formatJSON(
updatedTokens,
updatedTokens.deleted[token],
token,
original,
renamed,
false,
);
}
});
Object.keys(updatedTokens.updated).forEach((token) => {
formatJSON(updatedTokens.updated[token], token, original, renamed, true);
formatJSON(
updatedTokens,
updatedTokens.updated[token],
token,
original,
renamed,
true,
);
});
return updatedTokens;
}
Expand All @@ -73,9 +95,17 @@ export default function detectUpdatedTokens(
* @param {object} renamed - a JSON object containing the renamed tokens
* @param {boolean} update - a boolean indicating whether token property is added, deleted, or updated
*/
function formatJSON(tokens, properties, original, renamed, update) {
function formatJSON(
updatedTokens,
tokens,
properties,
original,
renamed,
update,
) {
if (renamed[properties] !== undefined) {
includeOldProperties(
updatedTokens,
tokens,
tokens,
properties,
Expand All @@ -86,6 +116,7 @@ function formatJSON(tokens, properties, original, renamed, update) {
);
} else {
includeOldProperties(
updatedTokens,
tokens,
tokens,
properties,
Expand All @@ -107,6 +138,7 @@ function formatJSON(tokens, properties, original, renamed, update) {
* @param {object} renamed - the renamed tokens
*/
function includeOldProperties(
updatedTokens,
token,
curTokenLevel,
properties,
Expand Down Expand Up @@ -158,7 +190,34 @@ function includeOldProperties(
curTokenLevel =
curTokenLevel[key] === undefined ? token : curTokenLevel[key];
});
if (!update) {
Object.keys(curOriginalLevel).forEach((originalProp) => {
Object.keys(curTokenLevel).forEach((curProp) => {
if (
curTokenLevel[curProp] !== undefined &&
typeof curOriginalLevel[originalProp] !== "string" &&
typeof curTokenLevel[curProp] !== "string" &&
curOriginalLevel[originalProp].uuid !== undefined &&
curTokenLevel[curProp].uuid !== undefined &&
curOriginalLevel[originalProp].uuid ===
curTokenLevel[curProp].uuid &&
originalProp !== curProp
) {
updatedTokens.renamed[curProp] = {
"old-name": originalProp,
};
delete curTokenLevel[curProp];
}
Object.keys(updatedTokens["renamed"]).forEach((prop) => {
if (updatedTokens["renamed"][prop]["old-name"] === curProp) {
delete curTokenLevel[curProp];
}
});
});
});
}
includeOldProperties(
updatedTokens,
token,
curTokenLevel,
nextProperties,
Expand Down
27 changes: 27 additions & 0 deletions tools/diff-generator/test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,30 @@ test("check cli output for deleted non-schema property", async (t) => {
}
});
});

test("check cli output for renamed property", async (t) => {
t.plan(1);
return new Promise((resolve, reject) => {
try {
nixt()
.expect(async () => {
try {
const expectedFileName = `${path}${outputPath}expected-renamed-property-token.txt`;
await access(expectedFileName);
const expected = await readFile(expectedFileName, {
encoding: "utf8",
});
t.snapshot(expected.trim());
} catch (error) {
reject(error);
}
})
.run(
`pnpm tdiff report -t ${path}${schemaPath}basic-set-token-property.json ${path}${schemaPath}renamed-added-deleted-property-set-token.json`,
)
.end(resolve);
} catch (error) {
reject(error);
}
});
});
Loading