Skip to content

Commit

Permalink
feat: version utils
Browse files Browse the repository at this point in the history
  • Loading branch information
coltenkrauter committed Sep 30, 2024
1 parent 3197d61 commit ca2762a
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 153 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
. ./scripts/pre-commit/index.sh
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Typescript Core
A TypeScript Repository that stands as a starting point for all other TypeScript repositories.
# Utils
A versatile TypeScript utility package packed with reusable, type-safe functions, scripts useful for all kinds of TypeScript projects, and precommit scripts to streamline your development workflow.

## Husky

Expand All @@ -11,6 +11,27 @@ Pre-commit hooks run scripts before a commit is finalized to catch issues or enf

This project uses a custom pre-commit hook to run `npm run bundle`. This ensures that our bundled assets are always up to date before any commit (which is especially important for TypeScript GitHub Actions). Husky automates this, so no commits will go through without a fresh bundle, keeping everything streamlined.

### Using Utils as Pre-Commit Hooks

```sh
./husky/pre-commit
#!/bin/sh

RED='\033[1;31m'
GREEN='\033[1;32m'
NC='\033[0m'

PREFIX="${GREEN}[HUSKY]${NC} "

if ! OUTPUT=$(npx ts-node ./node_modules/@krauters/utils/src/version.ts 2>&1); then
echo -e "${RED}${PREFIX}${OUTPUT}${NC}"
echo -e "${RED}${PREFIX}Aborting commit.${NC}"
echo -e "${RED}${PREFIX}Run commit with '-n' to skip pre-commit hooks.${NC}"
exit 1
fi

```

## Contributing

The goal of this project is to continually evolve and improve its core features, making it more efficient and easier to use. Development happens openly here on GitHub, and we’re thankful to the community for contributing bug fixes, enhancements, and fresh ideas. Whether you're fixing a small bug or suggesting a major improvement, your input is invaluable.
Expand All @@ -23,6 +44,6 @@ This project is licensed under the ISC License. Please see the [LICENSE](./LICEN

Thanks for spending time on this project.

<a href="https://github.com/krauters/typescript-core/graphs/contributors">
<img src="https://contrib.rocks/image?repo=krauters/typescript-core" />
<a href="https://github.com/krauters/utils/graphs/contributors">
<img src="https://contrib.rocks/image?repo=krauters/utils" />
</a>
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@krauters/typescript-core",
"description": "A TypeScript Repository that stands as a starting point for all other TypeScript repositories.",
"version": "1.0.0",
"main": "app.ts",
"name": "@krauters/utils",
"description": "A versatile TypeScript utility package packed with reusable, type-safe functions, scripts useful for all kinds of TypeScript projects, and precommit scripts to streamline your development workflow.",
"version": "0.4.0",
"main": "index.ts",
"type": "commonjs",
"scripts": {
"build": "ts-node ./src/app.ts",
"build": "ts-node ./src/index.ts",
"example-1": "ts-node ./example/1.ts",
"fix": "npm run lint -- --fix",
"lint": "npx eslint src/**",
Expand Down
17 changes: 17 additions & 0 deletions scripts/pre-commit/index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

# Check if MAIN_DIR is set; if not, use the default directory calculation
if [ -z "$MAIN_DIR" ]; then
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)/../scripts/pre-commit"
else
SCRIPT_DIR="$MAIN_DIR"
fi

. "$SCRIPT_DIR/log.sh"
log "Running pre-commit scripts from directory [${SCRIPT_DIR}]"


. "$SCRIPT_DIR/version.sh"

log 'All pre-commit checks passed.'
exit 0
20 changes: 20 additions & 0 deletions scripts/pre-commit/log.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# This script provides reusable logging functions for other scripts.

# Color codes
RED='\033[1;31m'
GREEN='\033[1;32m'
NC='\033[0m' # No Color

# Prefix for log messages
PREFIX='[CHECKS]'

# Log functions
log() {
echo "${GREEN}${PREFIX}${NC} $1"
}

error() {
echo "${RED}${PREFIX}${NC} $1"
}
14 changes: 14 additions & 0 deletions scripts/pre-commit/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

log 'Running version check...'

# Execute the version check from @krauters/utils
if ! OUTPUT=$(npx ts-node "$SCRIPT_DIR/../../src/scripts/pre-commit.ts" 2>&1); then
error "Version check failed:\n\n${OUTPUT}\n"
error 'Aborting commit.'
error "Run commit with '-n' to skip pre-commit hooks."
exit 1
fi

log "Version check output...\n\n${OUTPUT}\n"
log 'Version check passed.'
120 changes: 0 additions & 120 deletions scripts/readme.js

This file was deleted.

1 change: 0 additions & 1 deletion src/app.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './version'
25 changes: 25 additions & 0 deletions src/scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Scripts

This folder contains reusable TypeScript scripts intended for pre-commit checks and other automation tasks, designed to be used across multiple repositories.

## Purpose

- **Centralized Automation:** Provides a consistent way to enforce checks, such as version validation, before committing changes.
- **Ease of Integration:** Ensures all consuming repositories maintain uniform pre-commit behavior.

## Key Script

- **`pre-commit.ts`**: This script handles pre-commit checks (like version validation). It’s meant to be executed via a bash script.

## Usage

1. **Install Dependencies**:
```
npm install --save-dev @krauters/utils ts-node
```
2. **Set Up Your Pre-Commit Hook**:
- Use a bash script to call `/scripts/pre-commit/index.sh` (or other specific scripst) in your repo.
```sh
. ./node_modules/@krauters/utils/scripts/pre-commit/index.sh
```
1 change: 1 addition & 0 deletions src/scripts/pre-commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './version'
3 changes: 3 additions & 0 deletions src/scripts/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { compareVersions } = require('../version')

compareVersions()
Loading

0 comments on commit ca2762a

Please sign in to comment.