Skip to content

Fix(config): better resolving of components on windows #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions loaders/utils/getComponentFiles.js
Original file line number Diff line number Diff line change
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;
};