2023-03-20 08:13:07 +01:00
|
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
|
|
import { writeFile } from 'node:fs/promises';
|
2023-03-20 06:58:24 +01:00
|
|
|
import { basename, dirname } from 'node:path/posix';
|
2023-03-19 14:22:14 +01:00
|
|
|
import { promisify } from 'node:util';
|
2023-03-20 15:07:13 +01:00
|
|
|
import { GENERATOR, type State, generate } from 'astring';
|
2023-03-19 14:22:14 +01:00
|
|
|
import type * as estree from 'estree';
|
2023-03-21 05:01:40 +01:00
|
|
|
import glob from 'glob';
|
2023-03-19 14:22:14 +01:00
|
|
|
import { format } from 'prettier';
|
|
|
|
|
2023-03-20 15:07:13 +01:00
|
|
|
interface SatisfiesExpression extends estree.BaseExpression {
|
|
|
|
type: 'SatisfiesExpression';
|
|
|
|
expression: estree.Expression;
|
|
|
|
reference: estree.Identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
const generator = {
|
|
|
|
...GENERATOR,
|
|
|
|
SatisfiesExpression(node: SatisfiesExpression, state: State) {
|
2023-03-21 05:01:40 +01:00
|
|
|
switch (node.expression.type) {
|
|
|
|
case 'ArrowFunctionExpression': {
|
|
|
|
state.write('(');
|
|
|
|
this[node.expression.type](node.expression, state);
|
|
|
|
state.write(')');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// @ts-ignore
|
|
|
|
this[node.expression.type](node.expression, state);
|
|
|
|
break;
|
|
|
|
}
|
2023-03-21 03:58:58 +01:00
|
|
|
}
|
2023-03-20 15:07:13 +01:00
|
|
|
state.write(' satisfies ');
|
|
|
|
this[node.reference.type](node.reference, state);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:01:40 +01:00
|
|
|
type SplitCamel<T extends string, YC extends string = '', YN extends readonly string[] = []> = T extends `${infer XH}${infer XR}`
|
|
|
|
? XR extends ''
|
|
|
|
? [...YN, Uncapitalize<`${YC}${XH}`>]
|
|
|
|
: XH extends Uppercase<XH>
|
|
|
|
? SplitCamel<XR, Lowercase<XH>, [...YN, YC]>
|
|
|
|
: SplitCamel<XR, `${YC}${XH}`, YN>
|
|
|
|
: YN;
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
type SplitKebab<T extends string> = T extends `${infer XH}-${infer XR}`
|
|
|
|
? [XH, ...SplitKebab<XR>]
|
|
|
|
: [T];
|
|
|
|
|
|
|
|
type ToKebab<T extends readonly string[]> = T extends readonly [infer XO extends string]
|
|
|
|
? XO
|
|
|
|
: T extends readonly [infer XH extends string, ...infer XR extends readonly string[]]
|
|
|
|
? `${XH}${XR extends readonly string[] ? `-${ToKebab<XR>}` : ''}`
|
|
|
|
: '';
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
type ToPascal<T extends readonly string[]> = T extends readonly [infer XH extends string, ...infer XR extends readonly string[]]
|
|
|
|
? `${Capitalize<XH>}${ToPascal<XR>}`
|
|
|
|
: '';
|
|
|
|
|
2023-03-19 14:22:14 +01:00
|
|
|
function h<T extends estree.Node>(component: T['type'], props: Omit<T, 'type'>): T {
|
|
|
|
const type = component.replace(/(?:^|-)([a-z])/g, (_, c) => c.toUpperCase());
|
|
|
|
return Object.assign(props, { type }) as T;
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:01:40 +01:00
|
|
|
declare global {
|
|
|
|
namespace JSX {
|
|
|
|
type Element = never;
|
|
|
|
type ElementClass = never;
|
|
|
|
type ElementAttributesProperty = never;
|
|
|
|
type ElementChildrenAttribute = never;
|
|
|
|
type IntrinsicAttributes = never;
|
|
|
|
type IntrinsicClassAttributes<T> = never;
|
|
|
|
type IntrinsicElements = {
|
|
|
|
[T in keyof typeof generator as ToKebab<SplitCamel<Uncapitalize<T>>>]: {
|
|
|
|
[K in keyof Omit<Parameters<typeof generator[T]>[0], 'type'>]?: Parameters<typeof generator[T]>[0][K];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:56:34 +01:00
|
|
|
function toStories(component: string): string {
|
2023-03-20 08:13:07 +01:00
|
|
|
const msw = `${component.slice(0, -'.vue'.length)}.msw`;
|
|
|
|
const implStories = `${component.slice(0, -'.vue'.length)}.stories.impl`;
|
|
|
|
const hasMsw = existsSync(`${msw}.ts`);
|
|
|
|
const hasImplStories = existsSync(`${implStories}.ts`);
|
2023-03-20 06:56:34 +01:00
|
|
|
const base = basename(component);
|
|
|
|
const dir = dirname(component);
|
2023-03-19 14:22:14 +01:00
|
|
|
const literal = (
|
2023-03-20 06:56:34 +01:00
|
|
|
<literal value={component.slice('src/'.length, -'.vue'.length)} />
|
2023-03-19 14:22:14 +01:00
|
|
|
) as unknown as estree.Literal;
|
|
|
|
const identifier = (
|
2023-03-21 05:12:30 +01:00
|
|
|
<identifier name={base.slice(0, -'.vue'.length).replace(/[-.]|^(?=\d)/g, '_').replace(/(?<=^[^A-Z_]*$)/, '_')} />
|
2023-03-19 14:22:14 +01:00
|
|
|
) as unknown as estree.Identifier;
|
2023-03-20 06:56:34 +01:00
|
|
|
const parameters = (
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='layout' />}
|
2023-03-20 08:15:03 +01:00
|
|
|
value={<literal value={`${dir}/`.startsWith('src/pages/') ? 'fullscreen' : 'centered'} />}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 08:13:07 +01:00
|
|
|
/>,
|
|
|
|
...hasMsw
|
|
|
|
? [
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='msw' />}
|
|
|
|
value={<identifier name='msw' />}
|
|
|
|
kind={'init' as const}
|
2023-03-20 08:13:07 +01:00
|
|
|
shorthand
|
|
|
|
/>,
|
|
|
|
]
|
|
|
|
: [],
|
2023-03-20 06:56:34 +01:00
|
|
|
]}
|
|
|
|
/>
|
|
|
|
);
|
2023-03-19 14:22:14 +01:00
|
|
|
const program = (
|
|
|
|
<program
|
|
|
|
body={[
|
|
|
|
<import-declaration
|
2023-03-21 05:05:40 +01:00
|
|
|
source={<literal value='@storybook/vue3' />}
|
2023-03-19 14:22:14 +01:00
|
|
|
specifiers={[
|
|
|
|
<import-specifier
|
2023-03-21 05:05:40 +01:00
|
|
|
local={<identifier name='Meta' />}
|
|
|
|
imported={<identifier name='Meta' />}
|
2023-03-19 14:22:14 +01:00
|
|
|
/>,
|
2023-03-20 15:07:13 +01:00
|
|
|
...hasImplStories
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
<import-specifier
|
2023-03-21 05:05:40 +01:00
|
|
|
local={<identifier name='StoryObj' />}
|
|
|
|
imported={<identifier name='StoryObj' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
],
|
2023-03-19 14:22:14 +01:00
|
|
|
]}
|
|
|
|
/>,
|
2023-03-20 08:13:07 +01:00
|
|
|
...hasMsw
|
|
|
|
? [
|
|
|
|
<import-declaration
|
|
|
|
source={<literal value={`./${basename(msw)}`} />}
|
|
|
|
specifiers={[
|
|
|
|
<import-namespace-specifier
|
2023-03-21 05:05:40 +01:00
|
|
|
local={<identifier name='msw' />}
|
2023-03-20 08:13:07 +01:00
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
]
|
|
|
|
: [],
|
2023-03-20 08:27:40 +01:00
|
|
|
...hasImplStories
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
<import-declaration
|
|
|
|
source={<literal value={`./${base}`} />}
|
|
|
|
specifiers={[
|
|
|
|
<import-default-specifier
|
|
|
|
local={identifier}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
],
|
2023-03-19 14:22:14 +01:00
|
|
|
<variable-declaration
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'const' as const}
|
2023-03-19 14:22:14 +01:00
|
|
|
declarations={[
|
|
|
|
<variable-declarator
|
2023-03-21 05:05:40 +01:00
|
|
|
id={<identifier name='meta' />}
|
2023-03-19 14:22:14 +01:00
|
|
|
init={
|
2023-03-20 15:07:13 +01:00
|
|
|
<satisfies-expression
|
|
|
|
expression={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='title' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
value={literal}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='component' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
value={identifier}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
reference={<identifier name={`Meta<typeof ${identifier.name}>`} />}
|
2023-03-19 14:22:14 +01:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
2023-03-20 08:13:07 +01:00
|
|
|
...hasImplStories
|
|
|
|
? [
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
<export-named-declaration
|
|
|
|
declaration={
|
|
|
|
<variable-declaration
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'const' as const}
|
2023-03-20 08:13:07 +01:00
|
|
|
declarations={[
|
|
|
|
<variable-declarator
|
2023-03-21 05:05:40 +01:00
|
|
|
id={<identifier name='Default' />}
|
2023-03-20 08:13:07 +01:00
|
|
|
init={
|
2023-03-20 15:07:13 +01:00
|
|
|
<satisfies-expression
|
|
|
|
expression={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='render' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
value={
|
|
|
|
<function-expression
|
2023-03-21 05:05:40 +01:00
|
|
|
id={<identifier name='render' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
params={[
|
2023-03-21 05:05:40 +01:00
|
|
|
<identifier name='args' />,
|
2023-03-20 15:07:13 +01:00
|
|
|
<object-pattern
|
|
|
|
properties={[
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='argTypes' />}
|
|
|
|
value={<identifier name='argTypes' />}
|
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
shorthand
|
|
|
|
/>,
|
|
|
|
]}
|
2023-03-20 08:13:07 +01:00
|
|
|
/>,
|
|
|
|
]}
|
2023-03-20 15:07:13 +01:00
|
|
|
body={
|
|
|
|
<block-statement
|
|
|
|
body={[
|
|
|
|
<return-statement
|
|
|
|
argument={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='components' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
value={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
|
|
|
key={identifier}
|
|
|
|
value={identifier}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
shorthand
|
|
|
|
/>,
|
|
|
|
]}
|
2023-03-20 08:13:07 +01:00
|
|
|
/>
|
|
|
|
}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='props' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
value={
|
|
|
|
<call-expression
|
|
|
|
callee={
|
|
|
|
<member-expression
|
2023-03-21 05:05:40 +01:00
|
|
|
object={<identifier name='Object' />}
|
|
|
|
property={<identifier name='keys' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
arguments={[
|
2023-03-21 05:05:40 +01:00
|
|
|
<identifier name='argTypes' />,
|
2023-03-20 15:07:13 +01:00
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='template' />}
|
2023-03-21 05:12:30 +01:00
|
|
|
value={<literal value={`<${identifier.name} v-bind="$props" />`} />}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
2023-03-20 08:13:07 +01:00
|
|
|
/>
|
|
|
|
}
|
2023-03-20 15:07:13 +01:00
|
|
|
method
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
<property
|
2023-03-21 05:05:40 +01:00
|
|
|
key={<identifier name='parameters' />}
|
2023-03-20 15:07:13 +01:00
|
|
|
value={parameters}
|
2023-03-21 05:05:40 +01:00
|
|
|
kind={'init' as const}
|
2023-03-20 15:07:13 +01:00
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
reference={<identifier name={`StoryObj<typeof ${identifier.name}>`} />}
|
2023-03-20 08:13:07 +01:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
],
|
2023-03-19 14:22:14 +01:00
|
|
|
<export-default-declaration
|
2023-03-21 05:05:40 +01:00
|
|
|
declaration={<identifier name='meta' />}
|
2023-03-19 14:22:14 +01:00
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
) as unknown as estree.Program;
|
|
|
|
return format(
|
2023-03-20 15:07:13 +01:00
|
|
|
generate(program, { generator }) + (hasImplStories ? readFileSync(`${implStories}.ts`, 'utf-8') : ''),
|
2023-03-19 14:22:14 +01:00
|
|
|
{
|
|
|
|
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');
|
2023-03-20 08:13:07 +01:00
|
|
|
return writeFile(stories, toStories(component));
|
2023-03-19 14:22:14 +01:00
|
|
|
})
|
|
|
|
));
|