generated from w3c/wai-resource-template
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from w3c/last-modified-date-issue
Fix last modified date issue
- Loading branch information
Showing
5 changed files
with
60 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters