import * as fs from 'node:fs/promises'; import { basename, dirname } from 'node:path/posix'; import { promisify } from 'node:util'; import { generate } from 'astring'; import type * as estree from 'estree'; import * as glob from 'glob'; import { format } from 'prettier'; function h(component: T['type'], props: Omit): T { const type = component.replace(/(?:^|-)([a-z])/g, (_, c) => c.toUpperCase()); return Object.assign(props, { type }) as T; } function toStories(component: string): string { const base = basename(component); const dir = dirname(component); const literal = ( ) as unknown as estree.Literal; const identifier = ( ) as unknown as estree.Identifier; const parameters = ( } value={} kind="init" /> ]} /> ); const program = ( } specifiers={[ } imported={} />, } imported={} />, ]} />, } specifiers={[ , ]} />, } init={ } value={literal} kind="init" />, } value={identifier} kind="init" />, ]} /> } />, ]} />, } init={ } value={ , ]} /> } kind="init" />, } value={`} />} kind="init" />, } value={parameters} kind="init" />, ]} /> } />, ]} /> } />, } />, ]} /> ) as unknown as estree.Program; return format( generate(program), { parser: 'babel-ts', singleQuote: true, useTabs: true, } ); } promisify(glob)('src/{components,pages,ui,widgets}/**/*.vue').then((components) => Promise.all( components.map((component) => { const stories = component.replace(/\.vue$/, '.stories.ts'); fs.stat(stories).then( () => {}, () => { fs.writeFile(stories, toStories(component)); } ); }) ));