2023-03-20 00:13:07 -07:00
|
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
|
|
import { writeFile } from 'node:fs/promises';
|
2023-03-19 22:58:24 -07:00
|
|
|
import { basename, dirname } from 'node:path/posix';
|
2023-03-19 06:22:14 -07:00
|
|
|
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<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-19 22:56:34 -07:00
|
|
|
function toStories(component: string): string {
|
2023-03-20 00:13:07 -07: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-19 22:56:34 -07:00
|
|
|
const base = basename(component);
|
|
|
|
const dir = dirname(component);
|
2023-03-19 06:22:14 -07:00
|
|
|
const literal = (
|
2023-03-19 22:56:34 -07:00
|
|
|
<literal value={component.slice('src/'.length, -'.vue'.length)} />
|
2023-03-19 06:22:14 -07:00
|
|
|
) as unknown as estree.Literal;
|
|
|
|
const identifier = (
|
2023-03-19 22:56:34 -07:00
|
|
|
<identifier name={base.slice(0, -'.vue'.length).replace(/[-.]|^(?=\d)/g, '_')} />
|
2023-03-19 06:22:14 -07:00
|
|
|
) as unknown as estree.Identifier;
|
2023-03-19 22:56:34 -07:00
|
|
|
const parameters = (
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
|
|
|
key={<identifier name="layout" />}
|
2023-03-20 00:15:03 -07:00
|
|
|
value={<literal value={`${dir}/`.startsWith('src/pages/') ? 'fullscreen' : 'centered'} />}
|
2023-03-19 22:56:34 -07:00
|
|
|
kind="init"
|
2023-03-20 00:13:07 -07:00
|
|
|
/>,
|
|
|
|
...hasMsw
|
|
|
|
? [
|
|
|
|
<property
|
|
|
|
key={<identifier name="msw" />}
|
|
|
|
value={<identifier name="msw" />}
|
|
|
|
kind="init"
|
|
|
|
shorthand
|
|
|
|
/>,
|
|
|
|
]
|
|
|
|
: [],
|
2023-03-19 22:56:34 -07:00
|
|
|
]}
|
|
|
|
/>
|
|
|
|
);
|
2023-03-19 06:22:14 -07:00
|
|
|
const program = (
|
|
|
|
<program
|
|
|
|
body={[
|
|
|
|
<import-declaration
|
|
|
|
source={<literal value="@storybook/vue3" />}
|
|
|
|
specifiers={[
|
|
|
|
<import-specifier
|
|
|
|
local={<identifier name="Meta" />}
|
|
|
|
imported={<identifier name="Meta" />}
|
|
|
|
/>,
|
|
|
|
<import-specifier
|
|
|
|
local={<identifier name="Story" />}
|
|
|
|
imported={<identifier name="Story" />}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
2023-03-20 00:13:07 -07:00
|
|
|
...hasMsw
|
|
|
|
? [
|
|
|
|
<import-declaration
|
|
|
|
source={<literal value={`./${basename(msw)}`} />}
|
|
|
|
specifiers={[
|
|
|
|
<import-namespace-specifier
|
|
|
|
local={<identifier name="msw" />}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
]
|
|
|
|
: [],
|
2023-03-19 06:22:14 -07:00
|
|
|
<import-declaration
|
2023-03-19 22:56:34 -07:00
|
|
|
source={<literal value={`./${base}`} />}
|
2023-03-19 06:22:14 -07:00
|
|
|
specifiers={[
|
|
|
|
<import-default-specifier
|
|
|
|
local={identifier}
|
|
|
|
imported={identifier}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
<variable-declaration
|
|
|
|
kind="const"
|
|
|
|
declarations={[
|
|
|
|
<variable-declarator
|
|
|
|
id={<identifier name="meta" />}
|
|
|
|
init={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
|
|
|
key={<identifier name="title" />}
|
|
|
|
value={literal}
|
|
|
|
kind="init"
|
|
|
|
/>,
|
|
|
|
<property
|
|
|
|
key={<identifier name="component" />}
|
|
|
|
value={identifier}
|
|
|
|
kind="init"
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
2023-03-20 00:13:07 -07:00
|
|
|
...hasImplStories
|
|
|
|
? [
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
<export-named-declaration
|
|
|
|
declaration={
|
|
|
|
<variable-declaration
|
|
|
|
kind="const"
|
|
|
|
declarations={[
|
|
|
|
<variable-declarator
|
|
|
|
id={<identifier name="Default" />}
|
|
|
|
init={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
|
|
|
key={<identifier name="render" />}
|
|
|
|
value={
|
|
|
|
<function-expression
|
|
|
|
id={<identifier name="render" />}
|
|
|
|
params={[
|
|
|
|
<identifier name="args" />,
|
|
|
|
<object-pattern
|
|
|
|
properties={[
|
|
|
|
<property
|
|
|
|
key={<identifier name="argTypes" />}
|
|
|
|
value={<identifier name="argTypes" />}
|
|
|
|
kind="init"
|
|
|
|
shorthand
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
body={
|
|
|
|
<block-statement
|
|
|
|
body={[
|
|
|
|
<return-statement
|
|
|
|
argument={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
|
|
|
key={<identifier name="components" />}
|
|
|
|
value={
|
|
|
|
<object-expression
|
|
|
|
properties={[
|
|
|
|
<property
|
|
|
|
key={identifier}
|
|
|
|
value={identifier}
|
|
|
|
kind="init"
|
|
|
|
shorthand
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
kind="init"
|
|
|
|
/>,
|
|
|
|
<property
|
|
|
|
key={<identifier name="props" />}
|
|
|
|
value={
|
|
|
|
<call-expression
|
|
|
|
callee={
|
|
|
|
<member-expression
|
|
|
|
object={<identifier name="Object" />}
|
|
|
|
property={<identifier name="keys" />}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
arguments={[
|
|
|
|
<identifier name="argTypes" />,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
kind="init"
|
|
|
|
/>,
|
|
|
|
<property
|
|
|
|
key={<identifier name="template" />}
|
|
|
|
value={<literal value={`<${identifier.name} v-bind="$props" />`} />}
|
|
|
|
kind="init"
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
method
|
|
|
|
kind="init"
|
|
|
|
/>,
|
|
|
|
<property
|
|
|
|
key={<identifier name="parameters" />}
|
|
|
|
value={parameters}
|
|
|
|
kind="init"
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
|
|
|
],
|
2023-03-19 06:22:14 -07:00
|
|
|
<export-default-declaration
|
|
|
|
declaration={<identifier name="meta" />}
|
|
|
|
/>,
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
) as unknown as estree.Program;
|
|
|
|
return format(
|
2023-03-20 00:13:07 -07:00
|
|
|
generate(program) + (hasImplStories ? readFileSync(`${implStories}.ts`, 'utf-8') : ''),
|
2023-03-19 06:22:14 -07: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 00:13:07 -07:00
|
|
|
return writeFile(stories, toStories(component));
|
2023-03-19 06:22:14 -07:00
|
|
|
})
|
|
|
|
));
|