Skip to content

Commit

Permalink
Add example for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Jun 21, 2024
1 parent 4cbce82 commit fb2cb0a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
9 changes: 9 additions & 0 deletions examples/content-injection/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export default defineIntegration({
seedTemplateDirectory: './src/integration',
});
},
'@it-astro:content:gitTrackedListResolved': ({ trackedFiles }) => {
console.log('Content utils tracking files:', trackedFiles);
},
'@it-astro:content:gitCommitResolved': ({ file, age, resolvedDate }) => {
console.log(
`Content utils resolved the ${age} commit date for file ${file} as:`,
resolvedDate
);
},
},
};
},
Expand Down
14 changes: 6 additions & 8 deletions packages/content-utils/src/runtime/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ export function setContentPath(path: string) {
contentPath = path;
}

const commitResolvedHook: HookTrigger<'@it-astro:content:gitCommitResolved'> = (globalThis as any)[
Symbol.for('@inox-tools/content-utils:triggers/gitCommitResolved')
];
const getCommitResolvedHook = (): HookTrigger<'@it-astro:content:gitCommitResolved'> =>
(globalThis as any)[Symbol.for('@inox-tools/content-utils:triggers/gitCommitResolved')];

/**
* @internal
Expand Down Expand Up @@ -47,7 +46,7 @@ export async function getCommitDate(file: string, age: 'oldest' | 'latest'): Pro

let resolvedDate = new Date(Number(match.groups.timestamp) * 1000);

await commitResolvedHook({
await getCommitResolvedHook()({
resolvedDate,
age,
file,
Expand All @@ -59,9 +58,8 @@ export async function getCommitDate(file: string, age: 'oldest' | 'latest'): Pro
return resolvedDate;
}

const trackedListResolvedHook: HookTrigger<'@it-astro:content:gitTrackedListResolved'> = (
globalThis as any
)[Symbol.for('@inox-tools/content-utils:triggers/gitTrackedListResolved')];
const getTrackedListResolvedHook = (): HookTrigger<'@it-astro:content:gitTrackedListResolved'> =>
(globalThis as any)[Symbol.for('@inox-tools/content-utils:triggers/gitTrackedListResolved')];

/**
* @internal
Expand All @@ -80,7 +78,7 @@ export async function listGitTrackedFiles(): Promise<string[]> {
const output = result.stdout.trim();
let files = output.split('\n');

await trackedListResolvedHook({
await getTrackedListResolvedHook()({
trackedFiles: Array.from(files),
ignoreFiles: (ignore) => {
for (const file of ignore) {
Expand Down
4 changes: 2 additions & 2 deletions packages/content-utils/virtual.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ declare namespace AstroIntegrationKit {
file: string;
resolvedDate: Date;
overrideDate: (newDate: Date) => void;
}) => Promise<void>;
}) => Promise<void> | void;
'@it-astro:content:gitTrackedListResolved'?: (params: {
trackedFiles: string[];
ignoreFiles: (files: string[]) => void;
}) => Promise<void>;
}) => Promise<void> | void;
}
}

0 comments on commit fb2cb0a

Please sign in to comment.