Skip to content

Commit

Permalink
Merge pull request #12863 from g1eny0ung/refactor/getAutoRefs
Browse files Browse the repository at this point in the history
Composition: Filter out disabled refs in getAutoRefs
  • Loading branch information
shilman committed Dec 1, 2020
2 parents bd835c1 + 79b3f27 commit 17826e3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/cli/src/js-package-manager/JsPackageManager.ts
Expand Up @@ -195,7 +195,7 @@ export abstract class JsPackageManager {
? `build-storybook -s ${options.staticFolder}`
: `build-storybook`;

const preCommand = options.preCommand ? this.getRunCommand(options.preCommand) : undefined;
const preCommand = options?.preCommand ? this.getRunCommand(options.preCommand) : undefined;
this.addScripts({
storybook: [preCommand, storybookCmd].filter(Boolean).join(' && '),
'build-storybook': [preCommand, buildStorybookCmd].filter(Boolean).join(' && '),
Expand Down
53 changes: 37 additions & 16 deletions lib/core/src/server/manager/manager-config.ts
Expand Up @@ -14,14 +14,20 @@ import loadCustomPresets from '../common/custom-presets';
import { typeScriptDefaults } from '../config/defaults';
import { Presets, PresetsOptions, Ref, StorybookConfigOptions } from '../types';

export const getAutoRefs = async (options: { configDir: string }): Promise<Ref[]> => {
export const getAutoRefs = async (
options: { configDir: string },
disabledRefs: string[] = []
): Promise<Ref[]> => {
const location = await findUp('package.json', { cwd: options.configDir });
const directory = path.dirname(location);

const { dependencies, devDependencies } = await fs.readJSON(location);
const deps = Object.keys({ ...dependencies, ...devDependencies }).filter(
(dep) => !disabledRefs.includes(dep)
);

const list = await Promise.all(
Object.keys({ ...dependencies, ...devDependencies }).map(async (d) => {
deps.map(async (d) => {
try {
const l = resolveFrom(directory, path.join(d, 'package.json'));

Expand Down Expand Up @@ -75,8 +81,34 @@ async function getManagerWebpackConfig(
const typescriptOptions = await presets.apply('typescript', { ...typeScriptDefaults }, options);
const babelOptions = await presets.apply('babel', {}, { ...options, typescriptOptions });

const autoRefs = await getAutoRefs(options);
const definedRefs = await presets.apply('refs', undefined, options);
const definedRefs: Record<string, any> | undefined = await presets.apply(
'refs',
undefined,
options
);

let disabledRefs: string[] = [];
if (definedRefs) {
disabledRefs = Object.entries(definedRefs)
.filter(([key, value]) => {
const { disable, disabled } = value;

if (disable || disabled) {
if (disabled) {
deprecatedDefinedRefDisabled();
}

delete definedRefs[key]; // Also delete the ref that is disabled in definedRefs

return true;
}

return false;
})
.map((ref) => ref[0]);
}

const autoRefs = await getAutoRefs(options, disabledRefs);
const entries = await presets.apply('managerEntries', [], options);

const refs: Record<string, Ref> = {};
Expand All @@ -93,18 +125,7 @@ async function getManagerWebpackConfig(
}

if (definedRefs) {
Object.entries(definedRefs).forEach(([key, value]: [string, any]) => {
const { disable, disabled } = value;

if (disable || disabled) {
if (disabled) {
deprecatedDefinedRefDisabled();
}

delete refs[key.toLowerCase()];
return;
}

Object.entries(definedRefs).forEach(([key, value]) => {
const url = typeof value === 'string' ? value : value.url;
const rest =
typeof value === 'string'
Expand Down

0 comments on commit 17826e3

Please sign in to comment.