Skip to content

Commit a7a6cf4

Browse files
elevatebartsapegin
authored andcommittedSep 7, 2018
Fix: Fix duplicated components with default glob on windows (#1128)
Closes #1127
1 parent 087e7b8 commit a7a6cf4

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed
 

‎loaders/utils/getComponentFiles.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@ const glob = require('glob');
33
const isFunction = require('lodash/isFunction');
44
const isString = require('lodash/isString');
55

6+
const getComponentGlobs = components => {
7+
if (isFunction(components)) {
8+
return components();
9+
} else if (Array.isArray(components)) {
10+
return components;
11+
} else if (isString(components)) {
12+
return [components];
13+
}
14+
throw new Error(
15+
`Styleguidist: components should be string, function or array, received ${typeof components}.`
16+
);
17+
};
18+
619
const getFilesMatchingGlobs = (components, rootDir, ignore) =>
720
components
821
.map(listItem => {
922
// Check if the string looks like a glob pattern by using hasMagic
1023
if (glob.hasMagic(listItem)) {
11-
return glob.sync(path.resolve(rootDir, listItem), { ignore });
24+
return glob.sync(listItem, {
25+
cwd: rootDir,
26+
ignore,
27+
// in order to avoid detecting each component twice on windows
28+
// when matching 2 cases of the same word, like {Src,src}
29+
// we remove case-sensitivity on windows
30+
nocase: process.platform === 'win32',
31+
});
1232
}
1333
// Wrap path in an array so reduce always gets an array of arrays
1434
return [listItem];
@@ -28,21 +48,14 @@ module.exports = function getComponentFiles(components, rootDir, ignore) {
2848
return [];
2949
}
3050

31-
let componentFiles;
32-
if (isFunction(components)) {
33-
componentFiles = getFilesMatchingGlobs(components(), rootDir, ignore);
34-
} else if (Array.isArray(components)) {
35-
componentFiles = getFilesMatchingGlobs(components, rootDir, ignore);
36-
} else if (isString(components)) {
37-
componentFiles = glob.sync(path.resolve(rootDir, components), { ignore });
38-
} else {
39-
throw new Error(
40-
`Styleguidist: components should be string, function or array, received ${typeof components}.`
41-
);
42-
}
51+
// Normalize components option into an Array
52+
const componentGlobs = getComponentGlobs(components);
53+
54+
// Resolve list of components from globs
55+
const componentFiles = getFilesMatchingGlobs(componentGlobs, rootDir, ignore);
4356

4457
// Make paths absolute
45-
componentFiles = componentFiles.map(file => path.resolve(rootDir, file));
58+
const componentFilesAbsolute = componentFiles.map(file => path.resolve(rootDir, file));
4659

47-
return componentFiles;
60+
return componentFilesAbsolute;
4861
};

0 commit comments

Comments
 (0)
Please sign in to comment.