Skip to content

Instantly share code, notes, and snippets.

@rotimi-best
Last active October 31, 2023 07:45
Show Gist options
  • 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.

Revisions

  1. rotimi-best renamed this gist Oct 31, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. rotimi-best created this gist Oct 31, 2023.
    39 changes: 39 additions & 0 deletions bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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."