Skip to content

Instantly share code, notes, and snippets.

@rotimi-best
Last active October 31, 2023 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rotimi-best/7bd7e4ebda09a68ff0a1dc8ae6fa0009 to your computer and use it in GitHub Desktop.
Save rotimi-best/7bd7e4ebda09a68ff0a1dc8ae6fa0009 to your computer and use it in GitHub Desktop.
Script that generates a cache.ts file for any service
#!/bin/bash
# Service name for the TypeScript file.
service_name="$1"
file_name="cache.ts"
# Code content to write to the file.
code="import { revalidateTag } from \"next/cache\";
interface RevalidateProps {
id?: string;
}
export const ${service_name}Cache = {
tag: {
byId(id: string) {
return \`${service_name}-\${id}\`;
},
},
revalidate({ id }: RevalidateProps): void {
if (id) {
revalidateTag(this.tag.byId(id));
}
},
};"
# Create the file in the current directory.
file_path="./packages/lib/$service_name/$file_name"
# Check if the file already exists.
if [ -e "$file_path" ]; then
echo "Error: File '$file_path' already exists."
exit 1
fi
# Create the file with the specified code.
echo "$code" > "$file_path"
echo "Created $file_path with the specified code."
@rotimi-best
Copy link
Author

Here is how to call the service.

/path/to/script nameOfService

Note that nameOfService must already exist in the packages/lib folder for it to add a cache.ts file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment