Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generating A Git Repo

The sketch repo command allows you to generate a new git repository, starting from a preset stored in one of your configuration files.

A git preset uses (or defines) a preset for its gitignore file and, optionally, for pre-commit, as well as a list of templates that will be generated inside the root of the new repo when the command is triggered.

# Gitignore presets
gitignore_presets:
  # Can be a string
  base:
    content: |
      *.env
      dist
  # Or a list of strings
  ts:
    extends_presets:
      # When being merged, the new entries are placed at the top
      - base
    content:
      - "*.tsBuildInfo"
      - node_modules

# Pre-commit presets
pre_commit_presets:
  # Commonly used repo
  base:
    repos:
      - repo: https://github.com/gitleaks/gitleaks
        rev: v8.28.0
        hooks:
          - id: gitleaks

  # Hooks specific to typescript projects
  typescript:
    # Extending the base preset
    extends_presets:
      - base
    repos:
      - repo: local
        hooks:
          - id: oxlint
            name: oxlint
            entry: oxlint
            language: system
            files: '\.svelte$|\.js$|\.ts$'
            types: [file]

# Git presets
git_presets:
  ts_package:
    workflows:
      - file_name: my_workflow.yaml
        # Preset ID
        id: extended
    # License file to generate
    license: Apache-2.0
    # Selecting a preset
    gitignore: ts
    pre_commit: typescript

Adding Workflows

We can also include some Github workflows by using their presets:

    workflows:
      - file_name: my_workflow.yaml
        # Preset ID
        id: extended

Adding Templates

We can use the with_templates setting to add a group of templates to a git preset. Let's say that we want to automatically generate a basic docker setup whenever we use this preset:

# We define a template in a file or in a config file...
templates:
  dockerfile: |
    FROM node:23-alpine

    COPY . .
    EXPOSE {{ docker_dev_port | default(value=5173) }}
    CMD ["npm", "run", "dev"]

# Templating presets
templating_presets:
  dockerfile:
    context:
      # Group context, lower priority
      docker_dev_port: 9530
    # List of templates
    templates:
      # Single template
      - output: Dockerfile
        template: dockerfile
        # Local context with higher priority
        context:
          docker_dev_port: 5173


# ...and then we add it to a preset

# Git presets
git_presets:
  ts_package:
    workflows:
      - file_name: my_workflow.yaml
        # Preset ID
        id: extended
    # License file to generate
    license: Apache-2.0
    # Selecting a preset
    gitignore: ts
    pre_commit: typescript

    # Templates that will be generated with this preset
    # starting from the new repo's root
    with_templates:
      - id: dockerfile

Hooks

We can define some commands (which can also be templates) to execute before and/or after generating the new repo:

    # Commands to run before generation, from the root of the new project
    hooks_pre:
      - command:
          # Inlining a new template definition here,
          # but as always, we can use stored templates too
          name: pre_hook
          content: "echo '{{ greeting }}' > pre.txt"
        context:
          greeting: hi

    # Commands to run after generation, from the root of the new project
    hooks_post:
      - command:
          name: post_hook
          content: "echo '{{ greeting }}' > post.txt"
        context:
          greeting: hi

Example

Starting from this config, we can run this command:

sketch repo --preset ts_package

To get this tree output:

├── .github
│   └── workflows
│       └── my_workflow.yaml
├── .gitignore
├── .pre-commit-config.yaml
├── Dockerfile
├── LICENSE
├── post.txt
└── pre.txt

ℹ️ With cli flags, we can override the gitignore and pre-commit presets, as well as adding new templates or hooks to run or generate when the preset is being used.

pre-commit-config.yaml output
repos:
- repo: local
  hooks:
  - id: oxlint
    entry: oxlint
    files: \.svelte$|\.js$|\.ts$
    language: system
    name: oxlint
    types:
    - file
- repo: https://github.com/gitleaks/gitleaks
  rev: v8.28.0
  hooks:
  - id: gitleaks
gitignore output
*.tsBuildInfo
node_modules
*.env
dist