-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new snippets for last modified
- Loading branch information
Showing
3 changed files
with
110 additions
and
2 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,58 @@ | ||
# Last Modified Schema | ||
|
||
We provide a last modified timestamp schema based on file stat and git timestamp. | ||
|
||
## Based on file stat | ||
|
||
create a timestamp schema based on file stat. | ||
|
||
```ts | ||
const timestamp = () => | ||
s.custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta, addIssue }) => { | ||
if (value != null) { | ||
addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the file modified timestamp' }) | ||
} | ||
|
||
const stats = await stat(meta.path) | ||
return stats.mtime.toISOString() | ||
}) | ||
``` | ||
|
||
use it in your schema | ||
|
||
```ts | ||
const posts = defineCollection({ | ||
// ... | ||
schema: { | ||
// ... | ||
lastModified: timestamp() | ||
} | ||
}) | ||
``` | ||
|
||
## Based on git timestamp | ||
|
||
```ts | ||
const execAsync = promisify(exec) | ||
|
||
const timestamp = () => | ||
s.custom<string | undefined>(i => i === undefined || typeof i === 'string').transform<string>(async (value, { meta, addIssue }) => { | ||
if (value != null) { | ||
addIssue({ fatal: false, code: 'custom', message: '`s.timestamp()` schema will resolve the value from `git log -1 --format=%cd`' }) | ||
} | ||
const { stdout } = await execAsync(`git log -1 --format=%cd ${meta.path}`) | ||
return new Date(stdout).toISOString() | ||
}) | ||
``` | ||
|
||
use it in your schema | ||
|
||
```ts | ||
const posts = defineCollection({ | ||
// ... | ||
schema: { | ||
// ... | ||
lastModified: timestamp() | ||
} | ||
}) | ||
``` |
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