Skip to content

Commit

Permalink
feat: handle all Zod types
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinraja committed Dec 12, 2021
0 parents commit 238f27c
Show file tree
Hide file tree
Showing 19 changed files with 5,228 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
root: true,
env: {
node: true,
},
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
ignorePatterns: ['node_modules', 'dist'],
overrides: [
{
files: '*.ts',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
extraFileExtensions: ['.cjs'],
},
},
],
}
3 changes: 3 additions & 0 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
extends: ['config:js-lib', 'github>sachinraja/renovate-config:js'],
}
28 changes: 28 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test

on: [push]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: pnpm/[email protected]
with:
version: 6

- uses: actions/setup-node@v2
with:
node-version: 14
cache: pnpm

- name: Install dependencies
run: pnpm install

- name: Lint
run: pnpm lint

- name: Test
run: pnpm test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
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"

pnpm lint-staged
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "dprint.dprint"]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.defaultFormatter": "dprint.dprint",
"dprint.path": "node_modules/dprint/dprint"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Sachin Raja

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.
143 changes: 143 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# ts-lib-starter

Boilerplate for your next TypeScript library. Build with speed and efficiency.

## Features

### [pnpm](https://pnpm.io/)

A fast and efficient package manager. Packages are linked from a single, global store.

### [tsup](https://tsup.egoist.sh/)

A quick, easy-to-use, and zero config bundler powered by esbuild. This allows for dual publishing esmodules and commonjs. It also produces only one dts file for each entrypoint.

### [Jest](https://jestjs.io/)

A testing framework. Uses [swc](https://swc.rs/) for transpiling TypeScript, look through the swc docs if you need to add features to the transpiler such as jsx. Since this uses `--experimental-vm-modules` for esm, you cannot mock modules in the normal way. You can currently use [`jest.unstable_mockModule`](https://github.com/facebook/jest/issues/9430#issuecomment-915109139), but it may change later.

### [dprint](https://dprint.dev/)

A pluggable and configurable code formatting platform written in Rust. Faster alternative to Prettier.

### [ESLint](https://eslint.org/) and [TypeScript ESLint](https://typescript-eslint.io/)

Linter that helps you find problems in your code.

### [npm-run-all](https://github.com/mysticatea/npm-run-all)

Run dprint, TypeScript, and ESLint checks in parallel.

### [Github Actions](https://github.com/features/actions)

Run all your checks on each commit.

### pre-commit hook

Uses [husky](https://typicode.github.io/husky/#/) to register a pre-commit hook and [lint-staged](https://github.com/okonet/lint-staged) to run commands only on changed files.

Ensure all files are formatted before they are committed and run linters on changed files.

### [Renovate](https://www.whitesourcesoftware.com/free-developer-tools/renovate/)

Automatically opens PRs to update dependencies. Automerges patch and minor updates, but not major updates or any `typescript` updates. Also pins all `devDependencies`) to use exact versions (**no** `^` before version signifying that the latest patch version can be matched, only the version specified can be used).

## Usage

This is esm-first, meaning you write esm and it is transpiled to both esm and cjs. For example, use:

```ts
import path from 'path'
import { fileURLToPath } from 'url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
```

instead of `__dirname`.

### Setup

1. [Install pnpm](https://pnpm.io/installation)

2. [Grant Renovate access to your GitHub repos](https://github.com/marketplace/renovate)

3. Copy the repo, replace `mypackage` with your repository name:

```
pnpx degit sachinraja/ts-lib-starter mypackage && cd mypackage
```

4. Search and replace all instances of `ts-lib-starter` with your package name. Remove `LICENSE` or replace it with your own.

5. Install dependencies:

```
pnpm i
```

6. Lint package:

```
pnpm lint
```

7. Test package:

```
pnpm t
```

Note that there is a workflow in `.github/workflows/test.yml` that will run on each commit if you push it to GitHub.

### Publishing

Run `pnpm publish` to publish the package. Make sure the version is what you want to publish before publishing. Building the package (in a `prepublishOnly` script) and setting the relevant `package.json` attributes are already done. Note that [`sideEffects`](https://webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free) is set to `false`, so bundlers like Webpack can tree shake the package:

> A "side effect" is defined as code that performs a special behavior when imported, other than exposing one or more exports. An example of this are polyfills, which affect the global scope and usually do not provide an export.
### Entry Points

An entry point is a path that modules from your package can be imported from. The default entry point for this starter is `.`, which simply means that `src/index.ts` can be imported as `ts-lib-starter` (your package name).

If you want to add an entrypoint, you must do the following:

1. Specify the path you want to users to import your module from. For this example, I will use the file `src/constants.ts` and expose the entry point as `ts-lib-starter/constants`. Add the following in `package.json` exports:

```jsonc
"exports": {
".": {
// ...
},
"./constants": {
"import": "./dist/constants.js",
"default": "./dist/constants.cjs"
}
}
```

This exposes the module to users in multiple formats. `import` is used when a user uses an esm import for the entry point. `default` is used in any other case (i.e. a cjs `require`).

2. Add the file to the `tsup` build in the `package.json` config:

```diff
{
"tsup": {
"entryPoints": [
"src/index.ts",
+ "src/constants.ts"
]
"format": [
"esm",
"cjs"
],
"dts": {
"resolve": true
},
"splitting": true
}
}
```

Note the options here. `format` specifies for the package to be bundled in both esm and cjs, which allows for a dual publish. `dts.resolve` is used to bundle types for `devDependencies`. For example, if you use a TypeScript utilities package, such as [`ts-essentials`](https://github.com/krzkaczor/ts-essentials), the types will be bundled (in the `.d.ts` files) to avoid a dependency on `ts-essentials`. `splitting` enables an experimental feature that allows for creating chunks with cjs. This helps to avoid duplicating code with a package with multiple entry points.

The `entryPoints` (`src/index.ts` and `src/constants.ts`), specify the files that are our entry points, so when you add an entry point, it must also be added to the `build` config like before.
23 changes: 23 additions & 0 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"incremental": true,
"indentWidth": 2,
"typescript": {
"quoteStyle": "preferSingle",
"semiColons": "asi"
},
"prettier": {
"singleQuote": true,
"associations": [
"**/*.yaml",
"**/*.json5"
]
},
"includes": ["**/*.{ts,tsx,js,jsx,cjs,mjs,json,json5,md,yaml}"],
"excludes": ["**/node_modules", "pnpm-lock.yaml", "dist"],
"plugins": [
"https://plugins.dprint.dev/typescript-0.60.0.wasm",
"https://plugins.dprint.dev/json-0.13.2.wasm",
"https://plugins.dprint.dev/markdown-0.11.3.wasm",
"https://plugins.dprint.dev/prettier-0.3.0.exe-plugin@c6c227493e655717b5f3d9c811ba576c82f2b060317bb233c6ec014958fbee19"
]
}
18 changes: 18 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const jestConfig = {
transformIgnorePatterns: ['node_modules', 'dist'],
transform: {
'^.+\\.(j|t)sx?$': [
'@swc/jest',
{
jsc: {
parser: {
syntax: 'typescript',
},
},
},
],
},
extensionsToTreatAsEsm: ['.ts'],
}

export default jestConfig
75 changes: 75 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "zod-to-ts",
"version": "0.0.0",
"type": "module",
"description": "generate TypeScript types from your Zod schema",
"main": "dist/index.cjs",
"exports": {
"./package.json": "./package.json",
".": {
"import": "./dist/index.js",
"default": "./dist/index.cjs"
}
},
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "pnpm clean && tsup",
"watch": "tsup --watch --no-dts",
"clean": "rimraf dist",
"format": "dprint fmt",
"lint": "run-p lint:*",
"lint:format": "dprint check",
"lint:types": "tsc",
"lint:js": "eslint .",
"prepare": "husky install",
"prepublishOnly": "pnpm build",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
},
"peerDependencies": {
"typescript": "^4",
"zod": "*"
},
"dependencies": {
"ts-morph": "13.0.2"
},
"devDependencies": {
"@swc/core": "1.2.119",
"@swc/jest": "0.2.12",
"@types/jest": "27.0.3",
"@types/node": "16.11.12",
"@typescript-eslint/eslint-plugin": "5.6.0",
"@typescript-eslint/parser": "5.6.0",
"dprint": "0.19.1",
"eslint": "8.4.1",
"eslint-config-prettier": "8.3.0",
"husky": "7.0.4",
"jest": "27.4.4",
"lint-staged": "12.1.2",
"npm-run-all": "4.1.5",
"regenerator-runtime": "0.13.9",
"rimraf": "3.0.2",
"tsup": "5.11.1",
"typescript": "4.5.3",
"zod": "3.11.6"
},
"sideEffects": false,
"lint-staged": {
"*.{js,ts,md,json,yaml}": "dprint fmt"
},
"tsup": {
"entryPoints": [
"src/index.ts"
],
"format": [
"esm",
"cjs"
],
"dts": {
"resolve": true
},
"splitting": true
}
}
Loading

0 comments on commit 238f27c

Please sign in to comment.