Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release #1

Merged
merged 18 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: moontaiworks/package-template
slug: moontai0724/yargs-typebox
files: ./coverage/cobertura-coverage.xml
verbose: true

Expand Down
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) 2024 moontai0724

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.
181 changes: 114 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,142 @@
# @moontaiworks/package-template
# yargs-typebox

[![NPM Version](https://img.shields.io/npm/v/@moontaiworks/package-template)](https://www.npmjs.com/package/@moontaiworks/package-template)
[![NPM Downloads](https://img.shields.io/npm/d18m/@moontaiworks/package-template)](https://www.npmjs.com/package/@moontaiworks/package-template)
[![Codecov](https://codecov.io/gh/moontaiworks/package-template/graph/badge.svg)](https://codecov.io/gh/moontaiworks/package-template)
[![NPM Version](https://img.shields.io/npm/v/yargs-typebox)](https://www.npmjs.com/package/yargs-typebox)
[![NPM Downloads](https://img.shields.io/npm/d18m/yargs-typebox)](https://www.npmjs.com/package/yargs-typebox)
[![Codecov](https://codecov.io/gh/moontaiworks/yargs-typebox/graph/badge.svg)](https://codecov.io/gh/moontaiworks/yargs-typebox)

## Features

- [x] TypeScript
- [x] [Vitest](https://github.com/vitest-dev/vitest): Tests and [Coverage](https://github.com/vitest-dev/vitest/tree/main/packages/coverage-v8)
- [x] [ESLint](https://eslint.org) + [Prettier](https://prettier.io): Coding Style & Formatter
- [Husky](https://github.com/typicode/husky) + [Lint-staged](https://github.com/okonet/lint-staged): Pre-commit hooks
- ~~[Airbnb Coding Style](https://github.com/airbnb/javascript)~~
- Auto [organize imports](https://github.com/un-js/eslint-plugin-import-x).
- Auto sort [imports](https://github.com/lydell/eslint-plugin-simple-import-sort).
- [x] [Commitlint](https://github.com/conventional-changelog/commitlint): Commit message linting to follow [Conventional Commits](https://www.conventionalcommits.org)
- [x] Auto versioning, Changelog and Release to GitHub release and NPM by using [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version)
- Version is bumped by parsing conventional commit messages
- Changelog is generated based on the commit messages
- [x] Show code coverage by uploading to [CodeCov](https://codecov.io) when running deployment workflow
- [x] [Typedoc](https://github.com/TypeStrong/typedoc): API documentation

## Getting Started

### 1. Use this template to create a new repository.

Click the [`Use this template`](https://github.com/new?template_name=package-template&template_owner=moontaiworks) button on the top right corner of the repository page.

### 2. Clone the repository, and install the dependencies.

Just clone your repo and install the dependencies with any package manager you like. This template does not strong bind to any package manager, but the used package manager in GitHub Actions workflow is `pnpm`. You may need to modify the workflow file if you use other package managers.

### 3. Modify the package content to fit your project.

There are few places you need to modify to fit your project, like `package.json`, `README.md`. You can use the following command to replace most of the content:

```bash
YOUR_GITHUB_USER="your-user-name"
YOUR_REPO_NAME="your-awesome-package-name"
sed -i "s/moontaiworks/${YOUR_GITHUB_USER}/g" package.json README.md .github/workflows/*
sed -i "s/package-template/${YOUR_REPO_NAME}/g" package.json README.md .github/workflows/*
```

### 4. Start coding!

## Publish

This template uses [CodeCov](https://docs.codecov.com/docs/quick-start) to check the code coverage. You can remove the CodeCov badge and the related scripts in `package.json` if you don't need it.
If you want to use CodeCov, you need to set the `CODECOV_TOKEN` in the [repository secrets](https://github.com/moontaiworks/package-template/settings/secrets/actions).

Once you done, you can push your codes to the `main` branch.

The actions in this template will auto perform following steps when you push the code to the `main` branch:

- Test: Run tests and generate coverage report
- Build: Generate bundled and minified esm, cjs version (for browser), and unminified esm version (for node).
- Release: Bump Version & Generate Changelog
- [Publish the package to npm](https://www.npmjs.com/package/@moontaiworks/package-template/)
- [Publish docs to GitHub Pages](https://moontaiworks.github.io/package-template/): You may need to setup the GitHub Pages in the repository settings.

You can modify the workflow file to fit your needs.
Use [TypeBox](https://github.com/sinclairzx81/typebox) to define your [yargs](https://www.npmjs.com/package/yargs) arguments.

## Install

### NPM

```bash
npm install @moontaiworks/package-template
npm install yargs-typebox
```

### Yarn

```bash
yarn add @moontaiworks/package-template
yarn add yargs-typebox
```

### PNPM

```bash
pnpm add @moontaiworks/package-template
pnpm add yargs-typebox
```

## Usage

### getOptions(TObject)

Transform a whole TypeBox object into yargs options object.

```typescript
import { add, divide, max } from "@moontaiworks/package-template";
import { type Static, Type } from "@sinclair/typebox";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { getOptions } from "yargs-typebox";

const schema = Type.Object({
page: Type.Number({ description: "page number" }),
size: Type.Number({ description: "page size", default: 10 }),
query: Type.Optional(Type.String()),
sort: Type.Optional(
Type.Array(Type.Union([Type.Literal("id"), Type.Literal("createdAt")])),
),
order: Type.Union([Type.Literal("asc"), Type.Literal("desc")], {
default: "asc",
implies: ["sort"],
}),
pretty: Type.Boolean({ description: "pretty print" }),
count: Type.Any({ description: "count", count: true }),
});

const options = getOptions(schema);
// {
// page: {
// type: "number",
// demandOption: true,
// describe: "page number",
// },
// size: {
// type: "number",
// default: 10,
// demandOption: false,
// describe: "page size",
// },
// query: {
// type: "string",
// demandOption: false,
// },
// sort: {
// type: "array",
// demandOption: false,
// choices: ["id", "createdAt"],
// },
// order: {
// type: "string",
// implies: ["sort"],
// default: "asc",
// demandOption: false,
// choices: ["asc", "desc"],
// },
// pretty: {
// type: "boolean",
// demandOption: true,
// describe: "pretty print",
// },
// count: {
// count: true,
// demandOption: true,
// describe: "count",
// },
// }

const argv = yargs(hideBin(process.argv))
.options(options)
.help()
.parse() as Static<typeof schema>;

console.log(argv);
```

add(1, 2); // 3
divide(1, 2); // 0.5
max(1, 2); // 2
### getOption(TSchema)

Transform a single TypeBox schema into yargs option.

```typescript
import { Type } from "@sinclair/typebox";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { getOption } from "yargs-typebox";

const prettyArgument = Type.Boolean({ description: "pretty print" });
const countArgument = Type.Any({ description: "count", count: true });

const prettyOption = getOption(prettyArgument);
// {
// type: "boolean",
// demandOption: true,
// describe: "pretty print",
// }
const countOption = getOption(countArgument);
// {
// count: true,
// demandOption: true,
// describe: "count",
// }

const argv = yargs(hideBin(process.argv))
.option("pretty", prettyOption)
.option("count", countOption)
.help()
.parse();

console.log(argv);
```

## API Document

See the [API documentation](https://moontaiworks.github.io/package-template/).
For more informations, see the [API documentation](https://moontaiworks.github.io/yargs-typebox/).
6 changes: 5 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import globals from "globals";
import { config as typedConfig, configs as tsConfigs } from "typescript-eslint";

export default typedConfig(
{ ignores: ["node_modules", "dist", "build", "coverage"] },
{ ignores: ["node_modules", "dist", "build", "docs", "coverage"] },
{
files: ["src/**/*.ts"],
languageOptions: {
Expand Down Expand Up @@ -39,6 +39,10 @@ export default typedConfig(
...tsConfigs.strictTypeChecked,
...tsConfigs.stylisticTypeChecked,
],
rules: {
// causing error when passing empty object as initial value
"@typescript-eslint/prefer-reduce-type-parameter": "off",
},
},
{
plugins: {
Expand Down
29 changes: 19 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
{
"name": "@moontaiworks/package-template",
"name": "yargs-typebox",
"type": "module",
"private": false,
"publishConfig": {
"access": "public"
},
"version": "2.0.0-pre",
"description": "An npm package template.",
"author": "moontaiworks",
"license": "UNLICENSED",
"homepage": "https://moontaiworks.github.io/package-template",
"version": "0.0.0",
"description": "Use typebox to define your yargs arguments.",
"author": "moontai0724",
"license": "MIT",
"homepage": "https://moontaiworks.github.io/yargs-typebox",
"bugs": {
"url": "https://github.com/moontaiworks/package-template/issues"
"url": "https://github.com/moontaiworks/yargs-typebox/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/moontaiworks/package-template.git"
"url": "git+https://github.com/moontaiworks/yargs-typebox.git"
},
"keywords": [
"template"
"yargs",
"typebox",
"type"
],
"files": [
"build",
Expand Down Expand Up @@ -49,8 +51,10 @@
"@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0",
"@eslint/js": "^9.11.1",
"@sinclair/typebox": "^0.32.35",
"@types/eslint__js": "^8.42.3",
"@types/node": "^20.11.0",
"@types/yargs": "^17.0.33",
"@typescript-eslint/parser": "^8.8.0",
"@vitest/coverage-v8": "^2.1.1",
"eslint": "^9.11.1",
Expand All @@ -68,6 +72,11 @@
"typescript": "~5.5",
"typescript-eslint": "^8.7.0",
"vite": "^5.4.8",
"vitest": "^2.1.1"
"vitest": "^2.1.1",
"yargs": "^17.0.0"
},
"peerDependencies": {
"@sinclair/typebox": "^0.32.35",
"yargs": "^17.0.0"
}
}
Loading