Skip to content

Commit d870091

Browse files
authored
feat: set up turbo gen hook (#4)
1 parent 6c7fafa commit d870091

File tree

11 files changed

+1472
-20
lines changed

11 files changed

+1472
-20
lines changed

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 2
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ coverage
2626
out/
2727
build
2828
dist
29+
generated
2930

3031

3132
# Debug

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
{
22
"name": "workspace",
33
"private": true,
4+
"module": "true",
5+
"type": "module",
46
"scripts": {
57
"build": "turbo run build",
68
"dev": "turbo run dev",
79
"test": "turbo run test",
810
"lint": "turbo run lint",
911
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
10-
"check-types": "turbo run check-types"
12+
"gen-hook": "turbo gen hook --config \"turbo/generators/config.cts\" && pnpm format"
1113
},
1214
"devDependencies": {
15+
"@turbo/gen": "^2.5.0",
16+
"date-fns": "^4.1.0",
1317
"prettier": "^3.5.3",
1418
"turbo": "^2.5.0",
1519
"typescript": "5.8.2"
1620
},
1721
"packageManager": "pnpm@9.0.0",
1822
"engines": {
19-
"node": ">=18"
20-
}
23+
"node": ">=18.17"
24+
},
25+
"license": "MIT"
2126
}

pnpm-lock.yaml

Lines changed: 1346 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbo/generators/config.cts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { PlopTypes } from '@turbo/gen'
2+
import { format } from 'date-fns'
3+
import path from 'path'
4+
5+
export default function generator(plop: PlopTypes.NodePlopAPI): void {
6+
const usehooksSrcPath = path.resolve('packages/usehooks-ts/src')
7+
plop.setGenerator('hook', {
8+
description: 'Create a post',
9+
prompts: [
10+
{
11+
type: 'input',
12+
name: 'name',
13+
message: 'post name please (eg: "use test")',
14+
},
15+
],
16+
actions: [
17+
{
18+
type: 'add',
19+
path: usehooksSrcPath + '/{{camelCase name}}/{{camelCase name}}.ts',
20+
templateFile: 'templates/hook/hook.ts.hbs',
21+
},
22+
{
23+
type: 'add',
24+
path: usehooksSrcPath + '/{{camelCase name}}/{{camelCase name}}.test.ts',
25+
templateFile: 'templates/hook/hook.test.ts.hbs',
26+
},
27+
{
28+
data: {
29+
date: format(new Date(), 'yyyy-MM-dd'),
30+
},
31+
type: 'add',
32+
path: usehooksSrcPath + '/{{camelCase name}}/{{camelCase name}}.md',
33+
templateFile: 'templates/hook/hook.mdx.hbs',
34+
},
35+
{
36+
type: 'add',
37+
path: usehooksSrcPath + '/{{camelCase name}}/{{camelCase name}}.demo.tsx',
38+
templateFile: 'templates/hook/hook.demo.tsx.hbs',
39+
},
40+
{
41+
type: 'add',
42+
path: usehooksSrcPath + '/{{camelCase name}}/index.ts',
43+
templateFile: 'templates/hook/index.ts.hbs',
44+
},
45+
{
46+
type: 'append',
47+
path: usehooksSrcPath + '/index.ts',
48+
templateFile: 'templates/index.ts.hbs',
49+
},
50+
],
51+
})
52+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { {{camelCase name}} } from './{{camelCase name}}'
2+
3+
export default function Component() {
4+
const [two] = {{camelCase name}}()
5+
6+
return <div>Hello {two}</div>
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This hook description markdown text.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { renderHook } from '@testing-library/react'
2+
3+
import { {{camelCase name}} } from './{{camelCase name}}'
4+
5+
describe('{{camelCase name}}()', () => {
6+
it('should {{name}} be ok', () => {
7+
const { result } = renderHook(() => {{camelCase name}}())
8+
const [value, setNumber] = result.current
9+
10+
expect(value).toBe(2)
11+
expect(typeof setNumber).toBe('function')
12+
})
13+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useState } from 'react'
2+
3+
import type { Dispatch, SetStateAction } from 'react'
4+
5+
/** Hook return type */
6+
type {{pascalCase name}}ReturnType = {
7+
/** The value of ... */
8+
value: number
9+
/** A method to update the value of ... */
10+
setValue: Dispatch<SetStateAction<number>>
11+
}
12+
13+
/**
14+
* Custom hook that ...
15+
* @param {boolean} [defaultValue] - The initial value for ... (default is `0`).
16+
* @returns {[number, Dispatch<SetStateAction<number>>]} A tuple containing ...
17+
* @see [Documentation](https://usehooks-ts.com/react-hook/{{kebabCase name}})
18+
* @public
19+
* @example
20+
* ```tsx
21+
* const { value, setValue } = {{camelCase name}}(2);
22+
*
23+
* console.log(value); // 2
24+
* ```
25+
*/
26+
export function {{camelCase name}}(
27+
defaultValue?: number,
28+
): {{pascalCase name}}ReturnType {
29+
const [value, setValue] = useState(defaultValue || 0)
30+
31+
return { value, setValue }
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './{{camelCase name}}'

0 commit comments

Comments
 (0)