Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
idleberg committed Apr 24, 2024
0 parents commit 93b7461
Show file tree
Hide file tree
Showing 12 changed files with 5,189 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig is awesome: https://EditorConfig.org
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

[*.{ls,yml,yaml}]
indent_style = space

[*.{cjs,js,mjs,ts}]
block_comment_start = /*
block_comment = *
block_comment_end = */

[*.{markdown,md}]
trim_trailing_whitespace = false
31 changes: 31 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-disable */
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended'
],
rules: {
'@typescript-eslint/ban-ts-comment': 0
},
ignorePatterns: [
'test/**/*'
],
overrides: [
{
files: [
'tsconfig.json'
],
rules: {
'json/*': [
'error',
'allowComments'
]
}
}
]
};
22 changes: 22 additions & 0 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: 'Stale issues and PR'
on:
schedule:
- cron: '30 1 * * *'

jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v4
with:
stale-issue-label: stale
stale-pr-label: stale
stale-issue-message: 'This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.'
stale-pr-message: 'This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.'
close-issue-message: 'This issue was closed because it has been stalled for 30 days with no activity.'
close-pr-message: 'This PR was closed because it has been stalled for 30 days with no activity.'
days-before-issue-stale: 60
days-before-pr-stale: 60
days-before-issue-close: 30
days-before-pr-close: 30
exempt-issue-labels: 'bug,enhancement,stale-exempt'
67 changes: 67 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: CI

on:
push:
paths:
- '.github/**'
- 'src/**'
- 'tests/**'
- 'package.json'
- 'pnpm-lock.yaml'
pull_request:
paths:
- '.github/**'
- 'src/**'
- 'tests/**'
- 'package.json'
- 'pnpm-lock.yaml'
workflow_dispatch:

jobs:
tests:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: ['lts/*', '*']
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 1

- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- uses: pnpm/[email protected]
name: Install pnpm
id: pnpm-install
with:
version: 8
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
run: |
echo "::set-output name=pnpm_cache_dir::$(pnpm store path)"
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile --strict-peer-dependencies

- name: Lint Source
run: pnpm run --if-present lint

- name: Build Source
run: pnpm run --if-present build

- name: Run Tests
run: pnpm run --if-present test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/

dist/
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Jan T. Sott

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# vite-plugin-valibot-env

> A Vite plugin to validate environment variables against a Valibot schema
[![License](https://img.shields.io/github/license/idleberg/vite-plugin-valibot-env?color=blue&style=for-the-badge)](https://github.com/idleberg/vite-plugin-valibot-env/blob/main/LICENSE)
[![Version](https://img.shields.io/npm/v/vite-plugin-valibot-env?style=for-the-badge)](https://www.npmjs.org/package/vite-plugin-valibot-env)
[![Build](https://img.shields.io/github/actions/workflow/status/idleberg/vite-plugin-valibot-env/tests.yml?style=for-the-badge)](https://github.com/idleberg/vite-plugin-valibot-env/actions)

## Installation

`npm install -D vite-plugin-valibot-env`

## Usage

```js
import { defineConfig } from 'vite';
import v from 'valibot';
import valibot from 'vite-plugin-valibot-env';

const envSchema = v.object({
NODE_ENV: v.union([
v.literal('development'),
v.literal('staging'),
v.literal('production'),
])
});

export default defineConfig({
plugins: [
valibot(envSchema)
]
});
```

## License

This work is licensed under [The MIT License](LICENSE)
32 changes: 32 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Plugin, loadEnv } from 'vite';
import { safeParse, type ObjectSchema, type SchemaIssue } from 'valibot';
import { yellow } from 'kleur';
import logSymbols from 'log-symbols';

export default function ValibotEnvPlugin<T extends ObjectSchema<any, any> = ObjectSchema<any, any>>(schema: T): Plugin {
return {
name: 'valibot-env',
config(userConfig, { mode }) {
const env = loadEnv(mode, process.cwd(), '')
const { issues, success } = safeParse(schema, env);

if (!success) {
for (const issue of issues) {
logIssue(issue)
}

process.exit(1);
}

return userConfig;
}
}
}

function logIssue(issue: SchemaIssue | undefined) {
if (!issue?.path) {
return
};

console.error(logSymbols.error, yellow(String(issue.path[0]?.key || '<undefined>')), issue.message);
}
64 changes: 64 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "vite-plugin-valibot-env",
"version": "0.0.0",
"description": "A Vite plugin to validate environment variables against a Valibot schema",
"license": "MIT",
"author": "Jan T. Sott",
"repository": {
"type": "git",
"url": "https://github.com/idleberg/vite-plugin-valibot-env"
},
"exports": {
".": {
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
}
},
"types": "./dist/index.d.ts",
"engines": {
"node": ">=18.0.0"
},
"files": [
"dist/",
"LICENSE",
"README.md"
],
"scripts": {
"build": "npm-run-all --parallel build:*",
"build:esm": "tsup index.ts --format esm --platform node --dts",
"lint": "eslint index.ts --ignore-path .gitignore",
"prepack": "npm run build",
"prepare": "husky install",
"release": "np --no-yarn --any-branch",
"test": "exit 0"
},
"keywords": [
"vite-plugin",
"valibot"
],
"dependencies": {
"kleur": "^4.1.5",
"log-symbols": "^4.1.0"
},
"devDependencies": {
"@types/node": "^14.18.0",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"eslint": "^8.50.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"np": "^10.0.5",
"npm-run-all2": "^6.1.2",
"tsup": "^8.0.2",
"typescript": "^5.4.5",
"valibot": "^0.30.0",
"vite": "^5.2.10"
},
"peerDependencies": {
"valibot": "^0.30.0",
"vite": "^4.0.0"
},
"lint-staged": {
"*.ts": "eslint --cache --fix"
}
}
Loading

0 comments on commit 93b7461

Please sign in to comment.