-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extract git log metadata about content markdown files (#14)
* feat: Extract git log metadata about content markdown files * chore: remove unnecessary .gitignore
- Loading branch information
Showing
5 changed files
with
180 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ logs | |
.env | ||
.env.* | ||
!.env.example | ||
|
||
# generated by generate-content-metadata.ts | ||
content-metadata.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,57 @@ | ||
import path from 'node:path' | ||
import fs from 'node:fs' | ||
|
||
import { simpleGit } from 'simple-git' | ||
import { glob } from 'glob' | ||
|
||
const __dirname = import.meta.dirname | ||
const clientPath = path.resolve(__dirname, '..') | ||
const contentPath = path.resolve(clientPath, 'content') | ||
|
||
const contentMarkdownPaths = await glob(path.join(contentPath, '**/*.md')) | ||
|
||
const git = simpleGit() | ||
|
||
export type ContentMetadata = Record< | ||
string, // path within content directory | ||
{ | ||
timestamp: string // timestamp ISO 8601 | ||
} | ||
> | ||
|
||
const markdownMetadataArray = await Promise.all( | ||
contentMarkdownPaths.map( | ||
(contentMarkdownPath) => | ||
new Promise<ContentMetadata>((resolve, reject) => { | ||
git | ||
.log({ | ||
file: contentMarkdownPath, | ||
maxCount: 1, | ||
strictDate: true | ||
}) | ||
.then((gitLog) => { | ||
if (gitLog.latest?.date) { | ||
const relativePath = contentMarkdownPath.substring(contentPath.length).replace(/\.md$/, '') | ||
resolve({ | ||
[relativePath]: { timestamp: gitLog.latest?.date } | ||
}) | ||
} else { | ||
reject( | ||
`Unable to extract latest Git log time from path ${contentMarkdownPath}` | ||
) | ||
} | ||
}) | ||
}) | ||
) | ||
) | ||
|
||
const contentMetadata: ContentMetadata = Object.assign({}, ...markdownMetadataArray) | ||
|
||
const contentMetadataPath = path.join(clientPath, 'content-metadata.json') | ||
|
||
fs.writeFileSync(contentMetadataPath, JSON.stringify(contentMetadata, null, 2)) | ||
|
||
console.log( | ||
'Success: timestamps extract from git and written to', | ||
contentMetadataPath | ||
) |