Skip to content

Commit

Permalink
Fix: Fix duplicated components with default glob on windows (#1128)
Browse files Browse the repository at this point in the history
Closes #1127
  • Loading branch information
elevatebart authored and sapegin committed Sep 7, 2018
1 parent 087e7b8 commit a7a6cf4
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions loaders/utils/getComponentFiles.js
Expand Up @@ -3,12 +3,32 @@ const glob = require('glob');
const isFunction = require('lodash/isFunction');
const isString = require('lodash/isString');

const getComponentGlobs = components => {
if (isFunction(components)) {
return components();
} else if (Array.isArray(components)) {
return components;
} else if (isString(components)) {
return [components];
}
throw new Error(
`Styleguidist: components should be string, function or array, received ${typeof components}.`
);
};

const getFilesMatchingGlobs = (components, rootDir, ignore) =>
components
.map(listItem => {
// Check if the string looks like a glob pattern by using hasMagic
if (glob.hasMagic(listItem)) {
return glob.sync(path.resolve(rootDir, listItem), { ignore });
return glob.sync(listItem, {
cwd: rootDir,
ignore,
// in order to avoid detecting each component twice on windows
// when matching 2 cases of the same word, like {Src,src}
// we remove case-sensitivity on windows
nocase: process.platform === 'win32',
});
}
// Wrap path in an array so reduce always gets an array of arrays
return [listItem];
Expand All @@ -28,21 +48,14 @@ module.exports = function getComponentFiles(components, rootDir, ignore) {
return [];
}

let componentFiles;
if (isFunction(components)) {
componentFiles = getFilesMatchingGlobs(components(), rootDir, ignore);
} else if (Array.isArray(components)) {
componentFiles = getFilesMatchingGlobs(components, rootDir, ignore);
} else if (isString(components)) {
componentFiles = glob.sync(path.resolve(rootDir, components), { ignore });
} else {
throw new Error(
`Styleguidist: components should be string, function or array, received ${typeof components}.`
);
}
// Normalize components option into an Array
const componentGlobs = getComponentGlobs(components);

// Resolve list of components from globs
const componentFiles = getFilesMatchingGlobs(componentGlobs, rootDir, ignore);

// Make paths absolute
componentFiles = componentFiles.map(file => path.resolve(rootDir, file));
const componentFilesAbsolute = componentFiles.map(file => path.resolve(rootDir, file));

return componentFiles;
return componentFilesAbsolute;
};

0 comments on commit a7a6cf4

Please sign in to comment.