Last active
October 31, 2023 07:45
-
-
Save rotimi-best/7bd7e4ebda09a68ff0a1dc8ae6fa0009 to your computer and use it in GitHub Desktop.
Script that generates a cache.ts file for any service
This file contains hidden or 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
#!/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." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is how to call the service.
Note that
nameOfService
must already exist in thepackages/lib
folder for it to add acache.ts
file