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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portable stories: Add telemetry #26764

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions code/lib/telemetry/package.json
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
@@ -0,0 +1 @@
// composeStory;
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/a.js
@@ -0,0 +1 @@
// composeStory;
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/b.js
@@ -0,0 +1 @@
// composeStories;
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/b.txt
@@ -0,0 +1 @@
composeStory
1 change: 1 addition & 0 deletions code/lib/telemetry/src/__fixtures__/foo/a.js
@@ -0,0 +1 @@
// composeStory;
19 changes: 19 additions & 0 deletions code/lib/telemetry/src/get-portable-stories-usage.test.ts
@@ -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
@@ -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/**'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to get values from user's gitignore, but I don't know how complex it is as projects might have multiple gitignore files

Suggested change
ignore: ['**/node_modules/**', '**/storybook-static/**', '**/dist/**'],
ignore: ['**/node_modules/**', '**/storybook-static/**', '**/dist/**', '**/build/**'],

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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to keep in mind that people might have helper functions that wrap composeStory/ies and therefore the count won't be accurate

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
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
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
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