Skip to content

Commit

Permalink
Support resolving webpack stats in subdirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Jun 15, 2021
1 parent 4263c19 commit c03175e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
6 changes: 6 additions & 0 deletions bin/git/git.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import execa from 'execa';
import gql from 'fake-tag';
import { EOL } from 'os';
Expand Down Expand Up @@ -446,3 +447,8 @@ export async function checkoutPrevious() {
export async function discardChanges() {
return execGitCommand(`git reset --hard`);
}

export async function getWorkingDir() {
const rootDir = await execGitCommand(`git rev-parse --show-toplevel`);
return path.relative(rootDir, '.');
}
10 changes: 8 additions & 2 deletions bin/lib/getDependentStoryFiles.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getWorkingDir } from '../git/git';
import bailFile from '../ui/messages/warnings/bailFile';

// Bail whenever one of these was changed
Expand All @@ -9,8 +10,10 @@ const EXTERNALS = [/\/node_modules\//, /\/webpack\/runtime\//, /^\(webpack\)/];
const isGlobal = (name) => GLOBALS.some((re) => re.test(name));
const isUserCode = ({ name, moduleName }) => !EXTERNALS.some((re) => re.test(name || moduleName));

export function getDependentStoryFiles(ctx, stats, changedFiles) {
export async function getDependentStoryFiles(ctx, stats, changedFiles) {
const { configDir = './.storybook', staticDir = [] } = ctx.storybook || {};
const workingDir = await getWorkingDir();
const workingDirRegExp = workingDir && new RegExp(`^./${workingDir}`);

// TODO deal with Windows path separator
const storybookDir = configDir.startsWith('./') ? configDir : `./${configDir}`;
Expand Down Expand Up @@ -68,7 +71,10 @@ export function getDependentStoryFiles(ctx, stats, changedFiles) {
}
}

changedFiles.forEach(traceName);
changedFiles.forEach((gitFilePath) => {
const webpackFilePath = workingDir ? gitFilePath.replace(workingDirRegExp, '.') : gitFilePath;
traceName(webpackFilePath);
});
while (toCheck.length > 0) {
const id = toCheck.pop();
checkedIds[id] = true;
Expand Down
82 changes: 80 additions & 2 deletions bin/lib/getDependentStoryFiles.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
import path from 'path';

import { getDependentStoryFiles } from './getDependentStoryFiles';
import { getWorkingDir } from '../git/git';

jest.mock('../git/git');

const CSF_GLOB = './src sync ^\\.\\/(?:(?!\\.)(?=.)[^/]*?\\.stories\\.js)$';

const ctx = {
log: { warn: jest.fn() },
};

describe('getDependentStoryFiles', () => {
it('traverses the reasons to find affected CSF files', () => {
// getDependentStoryFiles([], { idsByName: {}, reasonsById: {}, isCsfGlob: {} });
it('detects direct changes to CSF files', async () => {
const changedFiles = ['./src/foo.stories.js'];
const modules = [
{
id: 1,
name: './src/foo.stories.js',
reasons: [{ moduleName: CSF_GLOB }],
},
{
id: 999,
name: CSF_GLOB,
reasons: [{ moduleName: './.storybook/generated-stories-entry.js' }],
},
];
const res = await getDependentStoryFiles(ctx, { modules }, changedFiles);
expect(res).toEqual({
1: './src/foo.stories.js',
});
});

it('detects indirect changes to CSF files', async () => {
const changedFiles = ['./src/foo.js'];
const modules = [
{
id: 1,
name: './src/foo.js',
reasons: [{ moduleName: './src/foo.stories.js' }],
},
{
id: 2,
name: './src/foo.stories.js',
reasons: [{ moduleName: CSF_GLOB }],
},
{
id: 999,
name: CSF_GLOB,
reasons: [{ moduleName: './.storybook/generated-stories-entry.js' }],
},
];
const res = await getDependentStoryFiles(ctx, { modules }, changedFiles);
expect(res).toEqual({
2: './src/foo.stories.js',
});
});

it('supports webpack root in git subdirectory', async () => {
getWorkingDir.mockResolvedValueOnce('frontend');
const changedFiles = ['./frontend/src/foo.js'];
const modules = [
{
id: 1,
name: './src/foo.js',
reasons: [{ moduleName: './src/foo.stories.js' }],
},
{
id: 2,
name: './src/foo.stories.js',
reasons: [{ moduleName: CSF_GLOB }],
},
{
id: 999,
name: CSF_GLOB,
reasons: [{ moduleName: './.storybook/generated-stories-entry.js' }],
},
];
const res = await getDependentStoryFiles(ctx, { modules }, changedFiles);
expect(res).toEqual({
2: './src/foo.stories.js',
});
});
});
2 changes: 1 addition & 1 deletion bin/tasks/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const traceChangedFiles = async (ctx, task) => {
const { changedFiles } = ctx.git;
try {
const stats = await fs.readJson(statsPath);
ctx.onlyStoryFiles = getDependentStoryFiles(ctx, stats, changedFiles);
ctx.onlyStoryFiles = await getDependentStoryFiles(ctx, stats, changedFiles);
ctx.log.debug(
`Found affected story files:\n${Object.entries(ctx.onlyStoryFiles)
.map(([id, f]) => ` ${f} [${id}]`)
Expand Down
2 changes: 1 addition & 1 deletion scripts/stats-to-story-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const main = async () => {
},
};
// eslint-disable-next-line no-console
console.log(getDependentStoryFiles(ctx, stats, changedFiles));
console.log(await getDependentStoryFiles(ctx, stats, changedFiles));
};

main();

0 comments on commit c03175e

Please sign in to comment.