Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telemetry: Add portable stories #26764

Merged
merged 8 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code/lib/telemetry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"detect-package-manager": "^2.0.1",
"fetch-retry": "^5.0.2",
"fs-extra": "^11.1.0",
"glob": "^10.0.0",
"read-pkg-up": "^7.0.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/.storybook/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// composeStory;
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// composeStory;
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// composeStories;
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
composeStory
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/foo/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// composeStory;
19 changes: 19 additions & 0 deletions code/lib/telemetry/src/get-portable-stories-usage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, it, expect } from 'vitest';
import { join } from 'path';

import { getPortableStoriesFiles } from './get-portable-stories-usage';

describe('getPortableStoriesFiles', () => {
it('should ignores node_modules, non-source files', async () => {
const base = join(__dirname, '__fixtures__');
const usage = (await getPortableStoriesFiles(base)).map((f) => f.replace(base, ''));
expect(usage).toMatchInlineSnapshot(`
[
"/b.js",
"/a.js",
"/foo/a.js",
"/.storybook/a.js",
]
`);
});
});
51 changes: 51 additions & 0 deletions code/lib/telemetry/src/get-portable-stories-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { readFile } from 'fs/promises';
import { join } from 'path';
import { glob } from 'glob';

import { createFileSystemCache, resolvePathInStorybookCache } from '@storybook/core-common';

const cache = createFileSystemCache({
basePath: resolvePathInStorybookCache('portable-stories'),
ns: 'storybook',
ttl: 24 * 60 * 60 * 1000,
});

export const containsPortableStories = async (filename: string) => {
if (/sb\-preview\/runtime.m?js$/i.test(filename)) return null;

const fileContent = await readFile(filename, 'utf-8');
const contains = /composeStor[y|ies]/g.test(fileContent);
return contains ? filename : null;
};

export const getPortableStoriesFiles = async (base: string) => {
const files = await glob('**/*.{js,mjs,cjs,jsx,ts,mts,cts,tsx}', {
ignore: ['**/node_modules/**', '**/storybook-static/**', '**/dist/**'],
shilman marked this conversation as resolved.
Show resolved Hide resolved
dot: true,
cwd: base,
});

const hits = [];
const chunkSize = 10;
for (let i = 0; i < files.length; i += chunkSize) {
const chunk = files.slice(i, i + chunkSize);
const results = (
await Promise.all(chunk.map((f: string) => containsPortableStories(join(base, f))))
).filter(Boolean);
if (results.length > 0) {
hits.push(...results);
}
}
return hits as string[];
};

const CACHE_KEY = 'portableStories';
export const getPortableStoriesFileCount = async () => {
shilman marked this conversation as resolved.
Show resolved Hide resolved
let cached = await cache.get(CACHE_KEY);
if (!cached) {
const files = await getPortableStoriesFiles(process.cwd());
cached = { usage: files.length };
await cache.set(CACHE_KEY, cached);
}
return cached.usage;
};
3 changes: 3 additions & 0 deletions code/lib/telemetry/src/storybook-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getMonorepoType } from './get-monorepo-type';
import { cleanPaths } from './sanitize';
import { getFrameworkInfo } from './get-framework-info';
import { getChromaticVersionSpecifier } from './get-chromatic-version';
import { getPortableStoriesFileCount } from './get-portable-stories-usage';

export const metaFrameworks = {
next: 'Next',
Expand Down Expand Up @@ -177,10 +178,12 @@ export const computeStorybookMetadata = async ({
}

const storybookVersion = storybookPackages[storybookInfo.frameworkPackage]?.version;
const portableStoriesFileCount = await getPortableStoriesFileCount();

return {
...metadata,
...frameworkInfo,
portableStoriesFileCount,
storybookVersion,
storybookVersionSpecifier: storybookInfo.version,
language,
Expand Down
1 change: 1 addition & 0 deletions code/lib/telemetry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type StorybookMetadata = {
preview?: {
usesGlobals?: boolean;
};
portableStoriesFileCount?: number;
};

export interface Payload {
Expand Down
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6646,6 +6646,7 @@ __metadata:
detect-package-manager: "npm:^2.0.1"
fetch-retry: "npm:^5.0.2"
fs-extra: "npm:^11.1.0"
glob: "npm:^10.0.0"
nanoid: "npm:^4.0.2"
node-fetch: "npm:^3.3.1"
read-pkg-up: "npm:^7.0.1"
Expand Down
Loading