Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ling1726 committed May 30, 2022
1 parent 9b04842 commit c3fa622
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
5 changes: 4 additions & 1 deletion storybook-addon-export-to-codesandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export const parameters = {
react: 'latest',
'react-dom': 'latest', // for React
'react-scripts': 'latest', // necessary when using typescript in CodeSandbox
'@fluentui/react-components': '^9.0.0-beta', // necessary for FluentProvider
},
// Dependencies that should be included in the story if it exists in the code
optionalDependencies: {
'@fluentui/react-icons': '^9.0.0-beta',
},
// Content of index.tsx in CodeSandbox
indexTsx: dedent`
Expand Down
13 changes: 9 additions & 4 deletions storybook-addon-export-to-codesandbox/src/getDepdencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ export type PackageDependencies = { [dependencyName: string]: string };
/**
*
* @param fileContent - code
* @param requiredDependencies - whose versions will override those found in the code
* @param requiredDependencies - dependencies that will always be incldued in package.json
* @param optionalDependencies - whose versions will override those found in the code
* @returns - Map of dependencies and their versions to include in package.json
*/
export const getDependencies = (fileContent: string, requiredDependencies: PackageDependencies) => {
export const getDependencies = (
fileContent: string,
requiredDependencies: PackageDependencies,
optionalDependencies: PackageDependencies,
) => {
const matches = fileContent.matchAll(/import .* from ['"](.*?)['"];/g);

const dependenciesInCode = Array.from(matches).reduce((dependencies, match) => {
if (!match[1].startsWith('react/')) {
const dependency = parsePackageName(match[1]).name;

if (!dependencies.hasOwnProperty(dependency)) {
dependencies[dependency] = requiredDependencies[dependency] ?? 'latest';
dependencies[dependency] = optionalDependencies[dependency] ?? 'latest';
}
}

return dependencies;
}, {} as PackageDependencies);

return dependenciesInCode;
return { ...dependenciesInCode, ...requiredDependencies };
};

// Parsed a scoped package name into name, version, and path.
Expand Down
29 changes: 26 additions & 3 deletions storybook-addon-export-to-codesandbox/src/getDependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('getDependencies', () => {
import * as allStuff from 'dependency1';
import { moreStuff } from '@dependency/dependency';
`;
const deps = getDependencies(code, {});
const deps = getDependencies(code, {}, {});

expect(deps).toEqual({
'@dependency/dependency': 'latest',
Expand All @@ -23,22 +23,45 @@ describe('getDependencies', () => {
import { moreStuff2 } from '@dependency/unstable';
import { moreStuff3 } from '@dependency/unstable/component';
`;
const deps = getDependencies(code, {});
const deps = getDependencies(code, {}, {});

expect(deps).toEqual({
dependency: 'latest',
'@dependency/unstable': 'latest',
});
});

it('versions in optionalDependencies should win ', () => {
const code = `
import { stuff } from 'dependency';
`;
const deps = getDependencies(code, {}, { dependency: '1.0.0' });

expect(deps).toEqual({
dependency: '1.0.0',
});
});

it('versions in requiredDependencies should win ', () => {
const code = `
import { stuff } from 'dependency';
`;
const deps = getDependencies(code, { dependency: '1.0.0' });
const deps = getDependencies(code, { dependency: '1.0.0' }, {});

expect(deps).toEqual({
dependency: '1.0.0',
});
});

it('versions in requiredDependencies should win ', () => {
const code = `
import { stuff } from 'dependency';
`;
const deps = getDependencies(code, { required: '1.0.0' }, {});

expect(deps).toEqual({
dependency: 'latest',
required: '1.0.0',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@ const displayToolState = (selector: string, context: StoryContext) => {
return false;
}

const requiredDependencies: PackageDependencies = context.parameters?.exportToCodeSandbox?.requiredDependencies;
const requiredDependencies: PackageDependencies = context.parameters?.exportToCodeSandbox?.requiredDependencies ?? {};
const optionalDependencies: PackageDependencies = context.parameters?.exportToCodeSandbox?.optionalDependencies ?? {};

if (requiredDependencies == null) {
console.error(`Export to CodeSandbox: Please set parameters.exportToCodeSandbox.requiredDependencies.`);
return false;
}

const dependencies = getDependencies(storyFile, requiredDependencies);
const dependencies = getDependencies(storyFile, requiredDependencies, optionalDependencies);

const indexTsx = context.parameters?.exportToCodeSandbox?.indexTsx;
if (indexTsx == null) {
Expand Down

0 comments on commit c3fa622

Please sign in to comment.