Skip to content

Commit

Permalink
refactor: use dprint (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Nov 20, 2023
1 parent 45012a2 commit 83b3e1d
Show file tree
Hide file tree
Showing 102 changed files with 1,393 additions and 1,278 deletions.
13 changes: 1 addition & 12 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@
// This enables the use of a lint rule to reject use of @deprecated functions.
// https://github.com/gund/eslint-plugin-deprecation
"deprecation",
// Import sorting integrated into ESLint.
// https://github.com/lydell/eslint-plugin-simple-import-sort
"simple-import-sort",
// https://github.com/microsoft/tsdoc/tree/master/eslint-plugin
"tsdoc"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
"plugin:@typescript-eslint/recommended-requiring-type-checking"
],
"overrides": [],
"rules": {
Expand All @@ -48,13 +44,6 @@
"quotes": "off",
"@typescript-eslint/quotes": ["warn", "backtick"],
"tsdoc/syntax": "warn",
"simple-import-sort/imports": [
"warn",
{
"groups": []
}
],
"simple-import-sort/exports": "warn",
"deprecation/deprecation": "warn",
"prefer-arrow/prefer-arrow-functions": "warn",
// TypeScript makes these safe & effective
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- uses: ./.github/actions/setup
- run: pnpm --workspace-root add dprint
- run: pnpm format:check
test:
runs-on: ubuntu-latest
Expand Down
6 changes: 0 additions & 6 deletions .prettierrc.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint"]
"recommendations": ["dprint.dprint", "dbaeumer.vscode-eslint"]
}
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
"source.fixAll.eslint": true,
// Disable this because it will conflict with ESLint based import organizing.
"source.organizeImports": false
},
"vitest.commandLine": ""
}
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

A set of packages related to building CLIs. Each package has its own docs.

| 📛 | Package | Description | Use Case | Alternatives |
| --- | ----------------------------------------------------------------------- | -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| 🌲 | `molt`</br>[packages/molt](./packages/molt/) | Batteries included CLI framework. Builds on top of the `@molt/*` packages. | Building a CLI with multiple commands, sub-commands, etc. | [OClif](https://oclif.io) [Commander](https://github.com/tj/commander.js/) [Yargs](https://github.com/yargs/yargs) |
| 🌱 | `@molt/command`</br>[packages/@molt/command](./packages/@molt/command/) | Type-safe CLI command definition and execution. | Just want to setup a quick and dirty script, build a small one-command CLI, etc. | [Arg](https://github.com/vercel/arg) |
|| `@molt/types`</br>[packages/@molt/types](./packages/@molt/types/) | Advanced Types for parsing flags & more. | Building your own CLI runtime, but looking for some TypeScript utility types for greater type safety. | Nothing |
| 📛 | Package | Description | Use Case | Alternatives |
| -- | ------------------------------------------------------------------------ | -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
| 🌲 | `molt`</br> [packages/molt](./packages/molt/) | Batteries included CLI framework. Builds on top of the `@molt/*` packages. | Building a CLI with multiple commands, sub-commands, etc. | [OClif](https://oclif.io) [Commander](https://github.com/tj/commander.js/) [Yargs](https://github.com/yargs/yargs) |
| 🌱 | `@molt/command`</br> [packages/@molt/command](./packages/@molt/command/) | Type-safe CLI command definition and execution. | Just want to setup a quick and dirty script, build a small one-command CLI, etc. | [Arg](https://github.com/vercel/arg) |
|| `@molt/types`</br> [packages/@molt/types](./packages/@molt/types/) | Advanced Types for parsing flags & more. | Building your own CLI runtime, but looking for some TypeScript utility types for greater type safety. | Nothing |
16 changes: 16 additions & 0 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"typescript": {
"quoteStyle": "preferSingle",
"semiColons": "asi"
},
"json": {},
"markdown": {},
"toml": {},
"excludes": ["**/node_modules", "**/*-lock.json"],
"plugins": [
"https://plugins.dprint.dev/typescript-0.88.3.wasm",
"https://plugins.dprint.dev/json-0.19.0.wasm",
"https://plugins.dprint.dev/markdown-0.16.2.wasm",
"https://plugins.dprint.dev/toml-0.5.4.wasm"
]
}
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"license": "MIT",
"type": "module",
"scripts": {
"format": "prettier --write .",
"format:check": "prettier --check .",
"format": "dprint fmt",
"format:check": "dprint check",
"lint": "eslint . --ext .ts,.tsx --fix",
"check:lint": "eslint . --ext .ts,.tsx --max-warnings 0",
"check:types": "tsc --noEmit",
Expand All @@ -18,7 +18,6 @@
},
"devDependencies": {
"@octokit/core": "^5.0.1",
"@prisma-labs/prettier-config": "0.1.0",
"@swc/core": "1.3.94",
"@tsconfig/node14": "14.1.0",
"@tsconfig/node16": "16.1.1",
Expand All @@ -28,18 +27,15 @@
"@typescript-eslint/eslint-plugin": "6.7.5",
"@typescript-eslint/parser": "6.7.5",
"eslint": "8.52.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-deprecation": "2.0.0",
"eslint-plugin-only-warn": "1.1.0",
"eslint-plugin-prefer-arrow": "1.2.3",
"eslint-plugin-simple-import-sort": "10.0.0",
"eslint-plugin-tsdoc": "0.2.17",
"execa": "8.0.1",
"fast-glob": "3.3.1",
"fs-jetpack": "5.1.0",
"markdown-toc": "^1.2.0",
"nodemon": "^3.0.1",
"prettier": "3.0.3",
"semver": "^7.5.4",
"semver-regex": "4.0.5",
"tsx": "^4.0.0",
Expand Down
99 changes: 65 additions & 34 deletions packages/@molt/command/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,30 @@ import { z } from 'zod'
const args = Command.create()
.use(Zod)
.parameter(`filePath`, z.string().describe(`Path to the file to convert.`))
.parameter(`to`, z.enum([`json`, `yaml`, `toml`]).describe(`Format to convert to.`))
.parameter(
`to`,
z.enum([`json`, `yaml`, `toml`]).describe(`Format to convert to.`),
)
.parameter(
`from`,
z
.enum([`json`, `yaml`, `toml`])
.optional()
.describe(`Format to convert from. By default inferred from the file extension.`),
.describe(
`Format to convert from. By default inferred from the file extension.`,
),
)
.parameter(
`verbose v`,
z.boolean().default(false).describe(`Log detailed progress as conversion executes.`),
z.boolean().default(false).describe(
`Log detailed progress as conversion executes.`,
),
)
.parameter(
`move m`,
z.boolean().default(false).describe(`Delete the original file after it has been converted.`),
z.boolean().default(false).describe(
`Delete the original file after it has been converted.`,
),
)
.parse()
```
Expand Down Expand Up @@ -139,7 +148,10 @@ $ mybin --help
- Default values:

```ts
const args = Command.create().parameter('--path', z.string().default('./a/b/c')).parse()
const args = Command.create().parameter(
'--path',
z.string().default('./a/b/c'),
).parse()
args.path === './a/b/c/' // $ mybin
args.path === '/over/ride' // $ mybin --path /over/ride
```
Expand Down Expand Up @@ -223,7 +235,6 @@ const args = command.parameter('charlie', z.number())
You can define parameters using dash prefixes (flag syntax).

```ts
// prettier-ignore
const args = Command.create()
.parameter('--foo -f', z.string())
.parameter('qux q', z.string())
Expand Down Expand Up @@ -348,7 +359,8 @@ This section covers the different kinds of built-in types and how they affect ar
Examples:

```ts
const args = Command.create().parameter('f force forcefully', z.boolean()).parse()
const args = Command.create().parameter('f force forcefully', z.boolean())
.parse()
// $ CLI_PARAM_NO_F='true' mybin
// $ CLI_PARAM_NO_FORCE='true' mybin
// $ CLI_PARAM_NO_FORCEFULLY='true' mybin
Expand Down Expand Up @@ -418,7 +430,7 @@ args.force === true

- `min` - The minimum allowed number.
- `max` - the maximum allowed number.
- `multipleOf` - The multiple that the given number must be of. For example `20, 15, 10,5 ` would all be allowed if `multipleOf` was `5` since all those numbers are divisible by `5`.
- `multipleOf` - The multiple that the given number must be of. For example `20, 15, 10,5` would all be allowed if `multipleOf` was `5` since all those numbers are divisible by `5`.
- `int`

#### Enum
Expand Down Expand Up @@ -454,7 +466,10 @@ args.force === true
- By default help rendering will render something like so:

```ts
Command.create().parameter('xee', z.union([z.string(), z.number()]).description('Blah blah blah.'))
Command.create().parameter(
'xee',
z.union([z.string(), z.number()]).description('Blah blah blah.'),
)
```

```
Expand Down Expand Up @@ -558,7 +573,6 @@ $ mybin --filePath ./a/b/c.yaml
```
1/3 level
❯ high | medium | low

```

- string
Expand Down Expand Up @@ -684,7 +698,10 @@ const args = await Command.create()
.parameter(`to`, {
schema: z.enum([`json`, `yaml`, `toml`]),
prompt: {
when: [Command.EventPatterns.rejectedMissingOrInvalid, Command.EventPatterns.omittedWithoutDefault],
when: [
Command.EventPatterns.rejectedMissingOrInvalid,
Command.EventPatterns.omittedWithoutDefault,
],
},
})
.parse()
Expand Down Expand Up @@ -921,7 +938,12 @@ const args = Command.create()
.parameter('--foo', z.string().default('not_from_env'))
.parameter('--bar', z.string().default('not_from_env'))
.parameter('--qux', z.string().default('not_from_env'))
.settings({ environment: { $default: { enabled: true, prefix: 'MOO' }, bar: { prefix: true } } })
.settings({
environment: {
$default: { enabled: true, prefix: 'MOO' },
bar: { prefix: true },
},
})
.parse()

// $ MOO_FOO='foo' CLI_PARAM_BAR='bar' MOO_QUX='qux' mybin
Expand Down Expand Up @@ -968,7 +990,12 @@ const args = Command.create()
.parameter('--foo', z.string().default('not_from_env'))
.parameter('--bar', z.string().default('not_from_env'))
.parameter('--qux', z.string().default('not_from_env'))
.settings({ environment: { $default: { enabled: true, prefix: false }, bar: { prefix: true } } })
.settings({
environment: {
$default: { enabled: true, prefix: false },
bar: { prefix: true },
},
})
.parse()

// $ FOO='foo' CLI_PARAM_BAR='bar' QUX='qux' mybin
Expand Down Expand Up @@ -1018,11 +1045,12 @@ With the chaining API you can declaratively state that two or more parameters ar
Here is an example where you might want this feature. You are building a CLI for publishing software packages that allows the user to specify the version to publish either by [semver](https://semver.org) level to bump by OR an exact version.
```ts
// prettier-ignore
const args = Command.create()
.parametersExclusive(`method`, (_) =>
_.parameter(`v version`, z.string().regex(semverRegex()))
.parameter(`b bump`, z.enum([`major`, `minor`, `patch`]))
.parametersExclusive(
`method`,
(_) =>
_.parameter(`v version`, z.string().regex(semverRegex()))
.parameter(`b bump`, z.enum([`major`, `minor`, `patch`])),
)
```
Expand All @@ -1037,10 +1065,9 @@ There are three key benefits to this method:
In the above example `args` will end up with a `method` property whose type is:
```ts
// prettier-ignore
type Method =
| { _tag: 'version', value: string }
| { _tag: 'bump', value: 'major' | 'minor' | 'patch' }
| { _tag: 'version'; value: string }
| { _tag: 'bump'; value: 'major' | 'minor' | 'patch' }
```
You automatically get a proper TypeScript-ready discriminant property based on the canonical names of your parameters. This helps you to write type-safe code. Also, it pairs well with [Alge 🌱](https://github.com/jasonkuhrt/alge) :). In the following example `Semver.inc` expects a strongly typed semver bump level of `'major'|'minor'|'patch'`:
Expand All @@ -1057,12 +1084,13 @@ const newVersion = Alge.match(args.method)
By default, input for a group of mutually exclusive parameters is required. You can mark the group as being optional:
```ts
// prettier-ignore
const args = Command.create()
.parametersExclusive(`method`, (_) =>
_.parameter(`v version`, z.string().regex(semverRegex()))
.parameter(`b bump`, z.enum([`major`, `minor`, `patch`]))
.optional()
.parametersExclusive(
`method`,
(_) =>
_.parameter(`v version`, z.string().regex(semverRegex()))
.parameter(`b bump`, z.enum([`major`, `minor`, `patch`]))
.optional(),
)
```
Expand All @@ -1071,13 +1099,14 @@ const args = Command.create()
By default, input for a group of mutually exclusive parameters is required. You can mark the group as being optional for users via a default so that internally there is always a value:
```ts
// prettier-ignore
const args = Command.create()
.parametersExclusive(`method`, (_) =>
_.parameter(`v version`, z.string().regex(semverRegex()))
.parameter(`b bump`, z.enum([`major`, `minor`, `patch`]))
.optional()
.default('bump', 'patch')
.parametersExclusive(
`method`,
(_) =>
_.parameter(`v version`, z.string().regex(semverRegex()))
.parameter(`b bump`, z.enum([`major`, `minor`, `patch`]))
.optional()
.default('bump', 'patch'),
)
```
Expand All @@ -1096,7 +1125,7 @@ const args = Command.create()
.description(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.',
)
.parameter(/* ... */)
.parameter() /* ... */
```
Descriptions will show up in the auto generated help.
Expand Down Expand Up @@ -1169,7 +1198,10 @@ mybin --xee z <-- enable xee using z
You could achieve this with the following parameter definition:
```ts
const args = Command.create().parameter('xee', z.union([z.boolean(), z.enum(['x', 'y', 'z'])]).default(false))
const args = Command.create().parameter(
'xee',
z.union([z.boolean(), z.enum(['x', 'y', 'z'])]).default(false),
)

args.xee // type: false | true | 'x' | 'y' | 'z'
```
Expand All @@ -1192,5 +1224,4 @@ Molt Command is composed from multiple distinct layers that execute in a flow:
1. Prompt/Up Front Arguments Merger (prompt overrides up front)
```

```
2 changes: 1 addition & 1 deletion packages/@molt/command/examples/intro.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod'
import { Command } from '../src/_entrypoints/default.js'
import { Zod } from '../src/_entrypoints/extensions.js'
import { z } from 'zod'

const args = await Command.create()
.use(Zod)
Expand Down
5 changes: 2 additions & 3 deletions packages/@molt/command/examples/kitchen-sink.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod'
import { Command } from '../src/_entrypoints/default.js'
import { Zod } from '../src/_entrypoints/extensions.js'
import { z } from 'zod'

const args = Command.create()
.use(Zod)
Expand Down Expand Up @@ -71,8 +71,7 @@ const args = Command.create()
z
.enum([`apple`, `banana`, `orange`])
.describe(`A sustainable snack for everyday happiness and delight!`),
),
)
))
.settings({
parameters: {
environment: {
Expand Down
2 changes: 1 addition & 1 deletion packages/@molt/command/examples/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z } from 'zod'
import { Command } from '../src/_entrypoints/default.js'
import { Zod } from '../src/_entrypoints/extensions.js'
import { z } from 'zod'

const args = await Command.create()
.use(Zod)
Expand Down
Loading

0 comments on commit 83b3e1d

Please sign in to comment.