Skip to content

Instantly share code, notes, and snippets.

View astahmer's full-sized avatar

Alexandre Stahmer astahmer

View GitHub Profile
@astahmer
astahmer / hono-dts.ts
Last active February 1, 2025 15:03
generating a `rpc.d.ts` file from hono router typings (cause the rpc typings are horribly slow to infer)
import { exec } from 'node:child_process';
import fs from 'node:fs/promises';
import path from 'node:path';
import { promisify } from 'node:util';
import { Project } from 'ts-morph';
const execPromise = promisify(exec);
async function generateHonoDtsFile() {
@astahmer
astahmer / create-persisted-query.ts
Created December 24, 2024 10:42
Effect.ts createQuery + createPersistedQuery utils, similar to Effect.fn but with optional Schema integration in input+output
import type { SqlError } from "@effect/sql";
import { Cause, Effect, Either } from "effect";
import type { ParseError } from "effect/ParseResult";
import { QueryError } from "./create-query.ts";
type PossibleError = SqlError.SqlError | ParseError;
interface PersistQueryConfig<Input, Output, FE, LE, PA, PE> {
name: string;
fetch: (input: Input) => Effect.Effect<Output, PossibleError | FE>;
@astahmer
astahmer / rewrite-imports.ts
Created November 5, 2024 18:02
Rewrite imports to use explicit `.ts` extensions, with https://www.typescriptlang.org/tsconfig/#allowImportingTsExtensions
import path from 'node:path';
import { Project } from 'ts-morph';
const __dirname = path.dirname(new URL(import.meta.url).pathname);
async function main() {
console.log('Rewritings imports start');
const onDone = '✅ Rewriting imports';
console.time(onDone);
@astahmer
astahmer / remove-unused-css-vars.ts
Created February 23, 2024 17:34
Remove unused CSS vars using LightningCSS plugin with visitors
// port of https://github.com/chakra-ui/panda/blob/f58f6df22d7cfc0163296bc86a811da64e74977a/sandbox/vite-ts/remove-unused-css-vars.ts
// with lightningcss
// doesn't seem much faster, probably because of the `Stylesheet` visitor
// `transform` is lightningcss fn
/*
// using this lightningcss plugin
❯ pnpm panda cssgen --lightningcss -o oui.css
traverse: 3.357ms
transform: 18.327ms
@astahmer
astahmer / chakra-reset-css.ts
Created January 17, 2024 00:07
Generate Chakra-UI reset.css inside a @layer with an optional scope selector
/**
https://github.com/chakra-ui/chakra-ui/blob/768aea0f0eb55af9d2e13d7568d92b0b995a0699/packages/components/src/css-reset/css-reset.tsx
*/
const css = String.raw;
const vhPolyfill = css`
:root,
:host {
--chakra-vh: 100vh;
@astahmer
astahmer / panda-vite.ts
Created December 2, 2023 14:46
Panda CSS vite plugin to inline `css` result as class strings
import { PandaContext, loadConfigAndCreateContext } from '@pandacss/node'
import { createCss } from '@pandacss/shared'
import { isAbsolute, resolve } from 'path'
import { PluginOption, ResolvedConfig, createFilter } from 'vite'
import MagicString from 'magic-string'
const ensureAbsolute = (path: string, root: string) => (path ? (isAbsolute(path) ? path : resolve(root, path)) : root)
const pandaVite = (): PluginOption => {
let config: ResolvedConfig
let root: string
@astahmer
astahmer / doc.md
Last active April 24, 2023 08:09
pnpm-where.ts command to locate a binary
@astahmer
astahmer / Box.tsx
Created July 17, 2022 13:58
vanilla-extract + dessert-box Box props
import { NonUndefined } from 'pastable';
import { defineProperties } from '@vanilla-extract/sprinkles';
import tb from 'ts-toolbelt';
import { vtmnPreset } from './vtmn-preset';
const colors = mapTailwindColorsToTheme(vtmnPreset.theme.colors);
export const colorStyles = defineProperties({
conditions: {
lightMode: {},
@astahmer
astahmer / imports-search.js
Created December 8, 2020 14:21
VSCode camel/Pascal case file imports regex search
const PascalCase = /from "(\.\/)*([A-Z])+([A-Za-z0-9]*)+";/;
/*
Find these, starting at "from" ending at ";"
export * from "./Reader";
export * from "./AliasHandler";
export * from "./EntityGroupsMetadata";
export * from "./SuchNameShouldNotExist";
*/
const wait = (duration, callback) => new Promise((resolve) => setTimeout(() => resolve(callback?.()), duration))