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

Fix last modified date issue #281

Merged
merged 8 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: true
fetch-depth: 0

- name: Set up Node.js
uses: actions/setup-node@v2
Expand Down
2 changes: 1 addition & 1 deletion _external/data
Submodule data updated 122 files
52 changes: 52 additions & 0 deletions scripts/pre-build/library/getExampleLastModifiedDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const path = require("path");
const { exec } = require("child_process");
const { format } = require("date-fns");
const { rewriteRelativePath } = require("./rewritePath");

const getExampleLastModifiedDate = async ({ html, sourcePath }) => {
const getFileLastModifiedDate = async (sourcePath) => {
const output = await new Promise((resolve) => {
exec(
`git log -1 --pretty="format:%cI" ${path.basename(sourcePath)}`,
{ cwd: path.dirname(sourcePath) },
(error, stdout, stderr) => {
resolve(stdout);
}
);
});
let date;
try {
date = new Date(output);
} catch (error) {
console.error(
`Failed to extract a last-modified date for the file "${sourcePath}"`
);
throw error;
}
return date;
};

const dependencyFilePaths = html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever!

.querySelectorAll("#css_js_files a")
.map(
(link) =>
rewriteRelativePath(link.getAttribute("href"), {
onSourcePath: sourcePath,
}).sourcePath
)
.concat(sourcePath);

const dates = await Promise.all(
dependencyFilePaths.map((filePath) => getFileLastModifiedDate(filePath))
);

dates.sort((a, b) => b - a);

const mostRecent = dates[0];

const dateFormatted = format(mostRecent, "d MMMM y");

return dateFormatted;
};

module.exports = getExampleLastModifiedDate;
2 changes: 1 addition & 1 deletion scripts/pre-build/library/rewritePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const rewriteRelativePath = (
const siteRootPath = `${siteRootPathPreHash}${hashFormatted}`;
const siteRelativePath = `${siteRelativePathPreHash}${queryStringFormatted}${hashFormatted}`;

return { siteRelativePath, siteRootPath, buildPath };
return { siteRelativePath, siteRootPath, sourcePath, buildPath };
};

module.exports = {
Expand Down
30 changes: 5 additions & 25 deletions scripts/pre-build/library/transformExample.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const fs = require("fs/promises");
const path = require("path");
const { exec } = require("child_process");
const { parse: parseHtml } = require("node-html-parser");
const { format } = require("date-fns");
const formatForJekyll = require("./formatForJekyll");
const { rewriteSourcePath, sourceRoot } = require("./rewritePath");
const rewriteElementPaths = require("./rewriteElementPaths");
const removeDuplicateMainTag = require("./removeDuplicateMainTag");
const wrapTablesWithResponsiveDiv = require("./wrapTablesWithResponsiveDiv");
const removeConflictingCss = require("./removeConflictingCss");
const getExampleLastModifiedDate = require("./getExampleLastModifiedDate");

const loadNotice = async () => {
const relativePath = "content/shared/templates/example-usage-warning.html";
Expand All @@ -31,28 +30,6 @@ const loadNotice = async () => {

const loadedNotice = loadNotice();

const getLastModifiedDate = async (exampleFilePath) => {
const output = await new Promise((resolve) => {
exec(
`git log -1 --pretty="format:%cI" ${path.basename(exampleFilePath)}`,
{ cwd: path.dirname(exampleFilePath) },
(error, stdout, stderr) => {
resolve(stdout);
}
);
});
let dateFormatted;
try {
dateFormatted = format(new Date(output), "d MMMM y");
} catch (error) {
console.error(
`Failed to extract a last-modified date for the file "${exampleFilePath}"`
);
throw error;
}
return dateFormatted;
};

const transformExample = async (sourcePath, sourceContents) => {
const { sitePath, githubPath } = rewriteSourcePath(sourcePath);
const html = parseHtml(sourceContents);
Expand All @@ -62,7 +39,10 @@ const transformExample = async (sourcePath, sourceContents) => {

removeConflictingCss(html);

const lastModifiedDateFormatted = await getLastModifiedDate(sourcePath);
const lastModifiedDateFormatted = await getExampleLastModifiedDate({
html,
sourcePath,
});

await rewriteElementPaths(html, { onSourcePath: sourcePath });

Expand Down