Skip to content

Commit

Permalink
Add test utils, logger improvements, general polish (#12)
Browse files Browse the repository at this point in the history
* Avoid resolving relative paths

* Cleanup

* Add a type param to `Plugin` for the `meta` field

* Cleanup terminology in Hooks

* Make HooksEmitter a generic

* Add default type to  param

* Change type param name

* Improve logger plugin

* Switch context function props to methods (immutability should be handled elsewhere)

* Improve comment

* Remove 'node:' prefix from imports

* Add client getter to state

* Bump help plugin version

* Improve comment

* Update command-menu plugin info

* Rename test folder

* Revert making context functions methods

* Improve hooks comments

* Move format-file-name to utils

* Rename data option in state and ensure it's used

* Add missing export for formatFileName

* Update tsconfigs

* Improve and export test-utils

* Replace `skip` option with `enabled` on command-menu, improve testing

* Update regex link

* Switch to biome

* Update biome settings

* Fix optionValues type in State.fork

* Wrap custom logger hooks in util fns

* Add changesets

* Quote command in workflow

* Add yarn.lock

* Fix workflow

* Remove unused deep freeze util

* Don't lint PRs for now
  • Loading branch information
ryangoree committed Jun 26, 2024
1 parent 5942944 commit 2eb1489
Show file tree
Hide file tree
Showing 68 changed files with 1,082 additions and 1,090 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-walls-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"clide-plugin-command-menu": minor
---

Renamed `skip` option to `enabled`.
13 changes: 13 additions & 0 deletions .changeset/late-ears-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"clide-js": minor
---

- Polished up internal ✨**test utils**✨ and added them to the exports so you can easily test you're CLIs and plugins!
- Made the `logger` plugin more full featured with `prefix`, `logFile`, and `enabled` options, and util functions for enabling/disabling the logger during runtime.
- Added `formatFileName` util function to exports.
- Added getters to `State` for `client`, ... so they can be accessed without needing to go through through `Context`.
- Made the `Hooks` type a generic which takes a `HooksObject` type param.
- Made the `PluginInfo` type a generic which takes a `PluginMeta` type param.
- Renamed `StateOptions.data` to `StateOptions.initialData`.
- Refactored command resolution to ignore relative paths.
- Fixed the `optionValues` type on `State.fork` to work with command types that have an optional `options` field.
4 changes: 0 additions & 4 deletions .eslintrc

This file was deleted.

3 changes: 2 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
tasks: [lint, build, typecheck, test:ci]
# tasks: [lint, build, typecheck, test]
tasks: [build, typecheck, test]

steps:
- name: Check out code
Expand Down
2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

9 changes: 0 additions & 9 deletions .prettierrc

This file was deleted.

34 changes: 26 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
{
"cSpell.words": [
"cfonts",
"cliui",
"divs",
"signale",
"unmock"
]
}
"cSpell.words": ["cfonts", "cliui", "divs", "signale", "unmock"],
"editor.defaultFormatter": "biomejs.biome",
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"quickfix.biome": "explicit",
"source.organizeImports.biome": "explicit"
},
"[json]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[jsonc]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[javascript]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[yaml]": {
"editor.defaultFormatter": "biomejs.biome"
},
"[markdown]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
44 changes: 44 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"$schema": "https://biomejs.dev/schemas/1.7.2/schema.json",
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 80,
"attributePosition": "auto",
"ignore": ["**/node_modules/**", "**/dist/**"]
},
"organizeImports": { "enabled": true },
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"correctness": {
"noUnusedImports": "error"
},
"style": {
"useNodejsImportProtocol": "warn"
},
"suspicious": {
"noExplicitAny": "warn",
"noDoubleEquals": "warn"
}
},
"ignore": ["./**/dist/**", "./**/node_modules/**"]
},
"javascript": {
"formatter": {
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"trailingComma": "all",
"semicolons": "always",
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto"
}
}
}
5 changes: 3 additions & 2 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"dist"
],
"scripts": {
"dev": "NODE_ENV=development ts-node --esm src/cli.ts",
"prod": "ts-node --esm src/cli.ts",
"dev": "NODE_ENV=development tesm-node src/cli.ts",
"prod": "node dist/cli.js",
"build": "rimraf dist && tsc"
},
"dependencies": {
Expand All @@ -21,6 +21,7 @@
"devDependencies": {
"@tsconfig/node-lts": "^18.12.5",
"rimraf": "^5.0.5",
"tesm-node": "^1.3.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
Expand Down
1 change: 1 addition & 0 deletions examples/typescript/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
import { help, run } from 'clide-js';
import { commandMenu } from 'clide-plugin-command-menu';

Expand Down
5 changes: 2 additions & 3 deletions notes/source-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ notes/ # misc notes
packages/ # package directories
├─ clide-js/ # core package
├─ clide-plugin-command-menu/ # a plugin to prompt users for missing subcommands
├─ esling-config/ # shared eslint config
├─ extras/ # utilities from CLIs I've written in the past that might get used later.
├─ tsconfig/ # shared tsconfig
```

The 2 published packages are [clide-js](https://www.npmjs.com/package/clide-js) and [clide-plugin-command-menu](https://www.npmjs.com/package/clide-plugin-command-menu)
The 2 published packages are [clide-js](https://www.npmjs.com/package/clide-js) and [clide-plugin-command-menu](https://www.npmjs.com/package/clide-plugin-command-menu)

## Clide-JS Source Code

Expand Down Expand Up @@ -94,7 +93,7 @@ plugin](https://github.com/ryangoree/clide-js/blob/main/packages/clide-js/src/pl

The
[run](https://github.com/ryangoree/clide-js/blob/main/packages/clide-js/src/core/run.ts)
module contains the `run` function which is the primary entry to framework,
module contains the `run` function which is the primary entry to framework,
used in the bin file of CLIs. It offers a simple unified API for running a CLI with
the Clide-JS framework. It manages registering hooks, creating a
`Context` instance, adding options, preparing and executing the context, and catching
Expand Down
15 changes: 5 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
{
"private": true,
"type": "module",
"scripts": {
"build:clide": "turbo run build --filter=./packages/clide*",
"build": "turbo run build",
"format": "prettier --write \"**/*.{ts,md}\"",
"lint": "turbo run lint",
"format": "biome format --write ./packages/",
"lint": "biome lint ./packages/",
"release": "turbo run build && changeset publish",
"test": "turbo run test:ci",
"typecheck": "turbo run typecheck"
},
"devDependencies": {
"@biomejs/biome": "1.7.2",
"@changesets/cli": "^2.27.1",
"eslint": "^9.1.0",
"prettier": "^3.2.5",
"prettier-plugin-organize-imports": "^3.2.4",
"tsconfig": "*",
"turbo": "latest"
},
"name": "clide",
"packageManager": "yarn@1.22.19",
"workspaces": [
"packages/*",
"examples/*",
".github/actions/*"
]
"workspaces": ["packages/*", "examples/*", ".github/actions/*"]
}
14 changes: 9 additions & 5 deletions packages/clide-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@
"yargs-parser": "^21.1.1"
},
"devDependencies": {
"@types/cli-table": "^0.3.4",
"@types/node": "^20.12.7",
"@types/prompts": "^2.4.9",
"@types/signale": "^1.4.7",
"@types/yargs-parser": "^21.0.3",
"@vitest/coverage-v8": "^1.5.0",
"dotenv": "^16.4.5",
"prettier-plugin-organize-imports": "^3.2.4",
"tesm-node": "^1.3.1",
"ts-node": "^10.9.2",
"tsconfig": "*",
Expand All @@ -50,11 +48,17 @@
},
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./test-utils": {
"types": {
"import": "./dist/test-utils.d.ts",
"require": "./dist/test-utils.d.cts"
},
"import": "./dist/test-utils.js",
"require": "./dist/test-utils.cjs"
}
},
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
]
"files": ["dist"]
}
26 changes: 0 additions & 26 deletions packages/clide-js/src/cli/commands/bar.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/clide-js/src/cli/commands/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default command({
name: {
type: 'string',
description: 'The name to greet',
default: '0xff',
},
},
handler: async ({ next, options }) => {
Expand Down
14 changes: 14 additions & 0 deletions packages/clide-js/src/cli/commands/foo/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { command } from 'src/core/command';

export default command({
description: 'Get a greeting',
options: {
name: {
type: 'string',
description: 'The name to greet',
},
},
handler: async ({ options }) => {
console.log(options);
},
});
10 changes: 8 additions & 2 deletions packages/clide-js/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#!/usr/bin/env node
import { run } from 'src/core/run';
import { help } from 'src/plugins/help';
import { logger } from 'src/plugins/logger';

const result = await run({
plugins: [logger(), help()],
plugins: [
logger({
enabled: process.env.NODE_ENV === 'development',
}),
help(),
],
}).catch((e) => {
console.error(e);
process.exit(1);
});

console.log('result', result);
console.log('result:', result);
Loading

0 comments on commit 2eb1489

Please sign in to comment.