misskey/packages/frontend/.storybook/changes.ts

81 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-04-06 14:38:42 +00:00
import fs from 'node:fs/promises';
import path from 'node:path';
import micromatch from 'micromatch';
import main from './main';
2023-04-06 13:25:40 +00:00
2023-04-06 15:33:31 +00:00
interface Stats {
readonly modules: readonly {
readonly id: string;
readonly name: string;
readonly reasons: readonly {
readonly moduleName: string;
}[];
}[];
}
2023-04-06 13:58:59 +00:00
fs.readFile(
2023-04-06 14:38:42 +00:00
path.resolve(__dirname, '../storybook-static/preview-stats.json')
2023-04-06 13:58:59 +00:00
).then((buffer) => {
2023-04-06 15:33:31 +00:00
const stats: Stats = JSON.parse(buffer.toString());
const keys = new Set(stats.modules.map((stat) => stat.id));
const map = new Map(
Array.from(keys, (key) => [
key,
new Set(
stats.modules
.filter((stat) => stat.id === key)
.flatMap((stat) => stat.reasons)
.map((reason) => reason.moduleName)
),
])
);
2023-04-06 13:58:59 +00:00
const modules = new Set(
2023-04-06 14:38:42 +00:00
process.argv
.slice(2)
.map((arg) =>
path.relative(
path.resolve(__dirname, '..'),
path.resolve(__dirname, '../../..', arg)
)
)
2023-04-06 15:33:31 +00:00
.map((path) => (path.startsWith('.') ? path : `./${path}`))
2023-04-06 13:58:59 +00:00
);
if (
micromatch(Array.from(modules), [
2023-04-06 14:38:42 +00:00
'../../assets/**',
'../../fluent-emojis/**',
'../../locales/**',
'../../misskey-assets/**',
'assets/**',
'public/**',
'../../pnpm-lock.yaml',
2023-04-06 13:58:59 +00:00
]).length
) {
return;
}
for (;;) {
const oldSize = modules.size;
for (const module of Array.from(modules)) {
2023-04-06 15:33:31 +00:00
if (map.has(module)) {
for (const dependency of Array.from(map.get(module)!)) {
modules.add(dependency);
2023-04-06 13:25:40 +00:00
}
}
}
2023-04-06 13:58:59 +00:00
if (modules.size === oldSize) {
break;
2023-04-06 13:25:40 +00:00
}
2023-04-06 13:58:59 +00:00
}
2023-04-06 14:38:42 +00:00
const stories = micromatch(
Array.from(modules),
2023-04-06 15:33:31 +00:00
main.stories.map((story) => `./${path.relative('..', story)}`)
2023-04-06 14:38:42 +00:00
);
2023-04-06 14:47:42 +00:00
if (stories.length) {
for (const story of stories) {
process.stdout.write(` --only-story-files ${story}`);
}
} else {
2023-04-06 15:33:31 +00:00
process.stdout.write(` --skip`);
2023-04-06 13:58:59 +00:00
}
});