Skip to content
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

feat: allow use of array of component paths in the config file #794

Merged
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/Components.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Each section consists of (all fields are optional):

* `name` — section title.
* `content` — location of a Markdown file containing the overview content.
* `components` — a glob pattern string or a function returning a list of components. The same rules apply as for the root `components` option.
* `components` — a glob pattern string, an array of component paths or a function returning a list of components. The same rules apply as for the root `components` option.
* `sections` — array of subsections (can be nested).
* `description` — A small description of this section.
* `ignore` — string/array of globs that should not be included in the section.
Expand Down
3 changes: 2 additions & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ Styleguidist uses [Bublé](https://buble.surge.sh/guide/) to run ES6 code on the

#### `components`

Type: `String` or `Function`, default: `src/components/**/*.{js,jsx,ts,tsx}`
Type: `String`, `Function` or `Array`, default: `src/components/**/*.{js,jsx,ts,tsx}`

* when `String`: a [glob pattern](https://github.com/isaacs/node-glob#glob-primer) that matches all your component modules.
* when `Function`: a function that returns an array of module paths.
* when `Array`: an array of module paths.

All paths are relative to config folder.

Expand Down
15 changes: 13 additions & 2 deletions loaders/utils/__tests__/getComponentFiles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ it('getComponentFiles() should accept components as a function that returns abso
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
});

it('getComponentFiles() should accept components as an array of file names', () => {
const result = getComponentFiles(components, configDir);
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
});

it('getComponentFiles() should accept components as a function that returns absolute paths', () => {
const absolutize = files => files.map(file => path.join(configDir, file));
const result = getComponentFiles(absolutize(components), configDir);
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
});

it('getComponentFiles() should accept components as a glob', () => {
const result = getComponentFiles(glob, configDir);
expect(deabs(result)).toEqual([
Expand All @@ -42,7 +53,7 @@ it('getComponentFiles() should ignore specified patterns', () => {
]);
});

it('getComponentFiles() should throw if components is not a function or a string', () => {
it('getComponentFiles() should throw if components is not a function, array or a string', () => {
const fn = () => getComponentFiles(42, configDir);
expect(fn).toThrowError('should be string or function');
expect(fn).toThrowError('should be string, function or array');
});
4 changes: 3 additions & 1 deletion loaders/utils/getComponentFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ module.exports = function getComponentFiles(components, rootDir, ignore) {
let componentFiles;
if (isFunction(components)) {
componentFiles = components();
} else if (Array.isArray(components)) {
componentFiles = components;
} else if (isString(components)) {
componentFiles = glob.sync(path.resolve(rootDir, components), { ignore });
} else {
throw new Error(
`Styleguidist: components should be string or function, received ${typeof components}.`
`Styleguidist: components should be string, function or array, received ${typeof components}.`
);
}

Expand Down