Skip to content

Commit

Permalink
Merge pull request #26764 from storybookjs/shilman/portable-stories-s…
Browse files Browse the repository at this point in the history
…tats

Telemetry: Add portable stories
  • Loading branch information
shilman authored Aug 7, 2024
2 parents f77eff2 + fee2848 commit dd05523
Show file tree
Hide file tree
Showing 8 changed files with 730 additions and 317 deletions.
11 changes: 11 additions & 0 deletions code/core/src/telemetry/get-portable-stories-usage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, it, expect } from 'vitest';

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

describe('getPortableStoriesFileCountUncached', () => {
it('should ignores node_modules, non-source files', async () => {
const usage = await getPortableStoriesFileCountUncached();
// you can verify with: `git grep -m1 -c composeStor | wc -l`
expect(usage).toMatchInlineSnapshot(`14`);
});
});
34 changes: 34 additions & 0 deletions code/core/src/telemetry/get-portable-stories-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { execaCommand } from 'execa';

import { createFileSystemCache, resolvePathInStorybookCache } from '../common';

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

export const getPortableStoriesFileCountUncached = async () => {
const { stdout } = await execaCommand(`git grep -m1 -c composeStor`, {
cwd: process.cwd(),
shell: true,
});
return stdout.split('\n').filter(Boolean).length;
};

const CACHE_KEY = 'portableStories';
export const getPortableStoriesFileCount = async () => {
let cached = await cache.get(CACHE_KEY);
if (!cached) {
try {
const count = await getPortableStoriesFileCountUncached();
cached = { count };
await cache.set(CACHE_KEY, cached);
} catch (err: any) {
// exit code 1 if no matches are found
const count = err.exitCode === 1 ? 0 : null;
cached = { count };
}
}
return cached.count;
};
3 changes: 3 additions & 0 deletions code/core/src/telemetry/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 @@ -205,10 +206,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/core/src/telemetry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type StorybookMetadata = {
preview?: {
usesGlobals?: boolean;
};
portableStoriesFileCount?: number;
};

export interface Payload {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React from 'react';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React from 'react';
export const Button = ({
isDisabled = false,
Expand Down
Loading

0 comments on commit dd05523

Please sign in to comment.