Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Dec 31, 2024
1 parent 3ca3662 commit 394cc44
Show file tree
Hide file tree
Showing 30 changed files with 189 additions and 159 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-snakes-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"porto": patch
---

Modified `porto/wagmi` entrypoint to export `Actions`, `Hooks`, and `Query` modules.
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,3 @@ out
# vercel
.vercel

# proxy packages
src/Chains
src/Porto
src/wagmi
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ export const wagmiConfig = createConfig({
This means you can now use Wagmi-compatible Hooks like `useConnect`. For more info, check out the [Wagmi Reference](#wagmi-reference).

```tsx
import { W } from 'porto/wagmi'
import { Hooks } from 'porto/wagmi'
import { useConnectors } from 'wagmi'

function Connect() {
const connect = W.useConnect()
const connect = Hooks.useConnect()
const connectors = useConnectors()

return connectors?.map((connector) => (
Expand Down Expand Up @@ -485,8 +485,8 @@ Import via named export or `A` namespace (better autocomplete DX and does not im
- `sessions`

```ts
import { A } from 'porto/wagmi/actions' // A.connect()
import { connect } from 'porto/wagmi/actions'
import { Actions } from 'porto/wagmi' // Actions.connect()
import { connect } from 'porto/wagmi/Actions'
```

### React Hooks
Expand All @@ -501,8 +501,8 @@ Import via named export or `W` namespace (better autocomplete DX and does not im
- `useSessions`

```ts
import { W } from 'porto/wagmi' // W.useConnect()
import { useConnect } from 'porto/wagmi'
import { Hooks } from 'porto/wagmi' // Hooks.useConnect()
import { useConnect } from 'porto/wagmi/Hooks'
```

## Development
Expand Down
14 changes: 1 addition & 13 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": {
"ignore": [
"contracts/lib",
"src/generated.ts",
"src/core/internal/generated.ts",
"tsconfig.json",
"tsconfig.*.json"
]
Expand Down Expand Up @@ -38,18 +38,6 @@
}
}
},
"overrides": [
{
"include": ["./scripts/**/*.ts"],
"linter": {
"rules": {
"suspicious": {
"noConsoleLog": "off"
}
}
}
}
],
"javascript": {
"formatter": {
"quoteStyle": "single",
Expand Down
14 changes: 7 additions & 7 deletions examples/wagmi/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { W } from 'porto/wagmi'
import { Hooks } from 'porto/wagmi'
import { type Hex, formatEther, parseEther } from 'viem'
import {
type BaseError,
Expand Down Expand Up @@ -39,8 +39,8 @@ export function App() {

function Account() {
const account = useAccount()
const { data: sessions } = W.useSessions()
const disconnect = W.useDisconnect()
const { data: sessions } = Hooks.useSessions()
const disconnect = Hooks.useDisconnect()

return (
<div>
Expand Down Expand Up @@ -69,7 +69,7 @@ function Connect() {
const [grantSession, setGrantSession] = useState<boolean>(true)

const connectors = useConnectors()
const connect = W.useConnect()
const connect = Hooks.useConnect()

return (
<div>
Expand Down Expand Up @@ -118,7 +118,7 @@ function ImportAccount() {
const [privateKey, setPrivateKey] = useState<string>('')

const connectors = useConnectors()
const importAccount = W.useImportAccount()
const importAccount = Hooks.useImportAccount()

return (
<div>
Expand Down Expand Up @@ -202,8 +202,8 @@ function Balance() {
}

function GrantSession() {
const sessions = W.useSessions()
const grantSession = W.useGrantSession()
const sessions = Hooks.useSessions()
const grantSession = Hooks.useGrantSession()

if (sessions.data?.length !== 0) return null
return (
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"check:repo": "sherif",
"check:size": "size-limit",
"check:types": "tsc --noEmit",
"clean": "rm -rf src/_dist src/Chains src/Porto src/wagmi ./tsconfig.build.tsbuildinfo src/tsconfig.build.tsbuildinfo",
"clean": "rm -rf src/_dist ./tsconfig.build.tsbuildinfo src/tsconfig.build.tsbuildinfo",
"deps": "pnpx taze -r",
"dev": "pnpm preconstruct && pnpm --filter playground dev",
"dev:wagmi": "pnpm preconstruct && pnpm --filter wagmi-example dev",
Expand Down Expand Up @@ -52,9 +52,9 @@
"pre-commit": "pnpm check"
},
"knip": {
"entry": ["src/{index,actions,query,wagmi}.ts!"],
"entry": ["src/{index,wagmi/index}.ts!"],
"project": ["src/**"],
"ignore": ["src/internal/**", "src/generated.ts"],
"ignore": ["**/internal/**", "src/core/internal/generated.ts"],
"ignoreDependencies": ["@tanstack/react-query", "react", "wagmi"]
},
"size-limit": [
Expand Down
62 changes: 25 additions & 37 deletions scripts/preconstruct.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,55 @@
import { mkdir, readFile, readdir, rm, symlink } from 'node:fs/promises'
import { basename, dirname, join, resolve } from 'node:path'

// Symlinks package sources to dist for local development
import * as fs from 'node:fs'
import { basename, dirname, resolve } from 'node:path'
import { getExports } from './utils/exports.js'

// biome-ignore lint/suspicious/noConsoleLog:
console.log('Setting up packages for development.')

const packagePath = resolve(import.meta.dirname, '../src/package.json')
type Package = {
bin?: Record<string, string> | undefined
exports: Record<string, { types: string; default: string } | string>
name?: string | undefined
private?: boolean | undefined
}
const packageJson = JSON.parse(await readFile(packagePath, 'utf8')) as Package
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf-8'))

// biome-ignore lint/suspicious/noConsoleLog:
console.log(`${packageJson.name}${dirname(packagePath)}`)

const dir = resolve(dirname(packagePath))

// Empty dist directories
const distDirName = '_dist'
const dist = resolve(dir, distDirName)
let files: string[] = []
try {
files = await readdir(dist)
} catch {
await mkdir(dist)
}
fs.rmSync(resolve(dir, '_dist'), { recursive: true, force: true })

const promises = []
for (const file of files) {
promises.push(rm(join(dist, file), { recursive: true, force: true }))
}
await Promise.all(promises)
const exports = getExports()

// Link exports to dist locations
for (const [key, exports] of Object.entries(packageJson.exports)) {
for (const [key, distExports] of Object.entries(exports.dist ?? {})) {
// Skip `package.json` exports
if (/package\.json$/.test(key)) continue
if (typeof exports === 'string') continue

let entries: any
if (typeof distExports === 'string')
entries = [
['default', distExports],
['types', distExports.replace('.js', '.d.ts')],
]
else entries = Object.entries(distExports as {})

// Link exports to dist locations
for (const [_, value] of Object.entries(exports) as [
for (const [, value] of entries as [
type: 'types' | 'default',
value: string,
][]) {
const srcDir = resolve(dir, dirname(value).replace(distDirName, ''))
let srcFileName: string
if (key === '.') srcFileName = 'index.ts'
else {
srcFileName = basename(`${key}.ts`)
}
const srcFilePath = resolve(srcDir, srcFileName)
const srcFilePath = resolve(dir, exports.src[key]!)

const distDir = resolve(dir, dirname(value))
const distFileName = basename(value)
const distFilePath = resolve(distDir, distFileName)

await mkdir(distDir, { recursive: true })
fs.mkdirSync(distDir, { recursive: true })

// Symlink src to dist file
await symlink(srcFilePath, distFilePath, 'file')
try {
fs.symlinkSync(srcFilePath, distFilePath, 'file')
} catch {}
}
}

console.log('Done. Set up packages.')
// biome-ignore lint/suspicious/noConsoleLog:
console.log('Done.')
79 changes: 41 additions & 38 deletions scripts/prepublish.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import { dirname, relative, resolve } from 'node:path'

// Generates proxy packages for package.json#exports.

console.log('Generating proxy packages.')

const packagePath = resolve(import.meta.dirname, '../src/package.json')
type Package = Record<string, unknown> & {
name?: string | undefined
private?: boolean | undefined
exports: Record<string, { types: string; default: string } | string>
}
const packageJson = JSON.parse(await readFile(packagePath, 'utf8')) as Package

console.log(`${packageJson.name}${dirname(packagePath)}`)

const dir = resolve(dirname(packagePath))

for (const [key, exports] of Object.entries(packageJson.exports)) {
// Skip `package.json` export
if (/package\.json$/.test(key)) continue
if (key === '.') continue
if (typeof exports === 'string') continue
if (!exports.default) continue

const proxyDir = resolve(dir, key)
await mkdir(proxyDir, { recursive: true })

const types = relative(key, exports.types)
const main = relative(key, exports.default)
await writeFile(
`${proxyDir}/package.json`,
`${JSON.stringify({ type: 'module', types, main }, undefined, 2)}\n`,
)
}

console.log('Done. Generated proxy packages.')
import * as fs from 'node:fs'
import { join, relative, resolve } from 'node:path'
import { getExports } from './utils/exports.js'

const packageJsonPath = join(import.meta.dirname, '../src/package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))

const exports = getExports({
onEntry: ({ entryName, name, parentEntryName }) => {
const modulePath = (dist?: string) => {
let path = './'
if (dist) path += `${dist}/`
if (dist || (parentEntryName && parentEntryName !== 'core'))
path += `${parentEntryName}/`
if (dist || name !== 'index') path += name
return path
}

try {
fs.mkdirSync(resolve(import.meta.dirname, '../src', entryName))
} catch {}
fs.writeFileSync(
resolve(import.meta.dirname, '../src', entryName, 'package.json'),
JSON.stringify(
{
type: 'module',
types: relative(modulePath(), modulePath('_dist')) + '.d.ts',
main: relative(modulePath(), modulePath('_dist')) + '.js',
},
null,
2,
),
)
},
})

packageJson.exports = exports.dist

delete packageJson.type

fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
Loading

0 comments on commit 394cc44

Please sign in to comment.