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

ci: improve theme preview action #2572

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions scripts/preview-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,30 @@ const parseJSON = (json) => {
);
}
} catch (error) {
let parsedJson = json
// Remove trailing commas (if any).
let parsedJson = json.replace(/(,\s*})/g, "}");

// Remove JS comments (if any).
parsedJson = parsedJson.replace(/\/\/[A-z\s]*\s/g, "");

// Fix incorrect open bracket (if any).
const splitJson = parsedJson
.split(/([\s\r\s]*}[\s\r\s]*,[\s\r\s]*)(?=[\w"-]+:)/)
.filter((x) => typeof x !== "string" || !!x.trim());
if (parsedJson[0].replace(/\s+/g, "") === "},") {
parsedJson[0] = "},";
if (!/\s*}\s*,?\s*$/.test(parsedJson[1])) {
parsedJson.push(parsedJson.shift());
.filter((x) => typeof x !== "string" || !!x.trim()); // Split json into array of strings and objects.
if (splitJson[0].replace(/\s+/g, "") === "},") {
splitJson[0] = "},";
if (!/\s*}\s*,?\s*$/.test(splitJson[1])) {
splitJson.push(splitJson.shift());
} else {
parsedJson.shift();
splitJson.shift();
}
return Hjson.parse(parsedJson.join(""));
} else {
parsedJson = splitJson.join("");
}

// Try to parse the fixed json.
try {
return Hjson.parse(parsedJson);
} catch (error) {
throw new IncorrectJsonFormatError(
`Theme JSON file could not be parsed: ${error.message}`,
);
Expand Down Expand Up @@ -387,10 +399,17 @@ export const run = async () => {
// Retrieve theme changes from the PR diff.
debug("Retrieve themes...");
const diff = parse(res.data);

// Retrieve all theme changes from the PR diff and convert to JSON.
debug("Retrieve theme changes...");
const content = diff
.find((file) => file.to === "themes/index.js")
.chunks[0].changes.filter((c) => c.type === "add")
.map((c) => c.content.replace("+", ""))
.chunks.map((chunk) =>
chunk.changes
.filter((c) => c.type === "add")
.map((c) => c.content.replace("+", ""))
.join(""),
)
.join("");
const themeObject = parseJSON(content);
if (
Expand Down