Skip to content

Commit

Permalink
Merge pull request #281 from w3c/last-modified-date-issue
Browse files Browse the repository at this point in the history
Fix last modified date issue
  • Loading branch information
alflennik authored Dec 20, 2023
2 parents ba0a291 + de939dd commit 9fe8e43
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
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
.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

0 comments on commit 9fe8e43

Please sign in to comment.