Skip to content

Commit 0e6e95a

Browse files
elevatebartsapegin
authored andcommittedSep 13, 2018
Fix: Regression: component patterns return nothing on Windows (#1133)
1 parent 5263b27 commit 0e6e95a

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed
 

‎loaders/utils/__tests__/getComponentFiles.spec.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import deabsDeep from 'deabsdeep';
33
import getComponentFiles from '../getComponentFiles';
44

55
const configDir = path.resolve(__dirname, '../../../test');
6-
const components = ['one.js', 'two.js'];
6+
const components = ['components/Annotation/Annotation.js', 'components/Button/Button.js'];
7+
const processedComponents = components.map(c => `~/${c}`);
78
const glob = 'components/**/[A-Z]*.js';
89
const globArray = ['components/Annotation/[A-Z]*.js', 'components/Button/[A-Z]*.js'];
910

@@ -16,13 +17,13 @@ it('getComponentFiles() should return an empty array if components is null', ()
1617

1718
it('getComponentFiles() should accept components as a function that returns file names', () => {
1819
const result = getComponentFiles(() => components, configDir);
19-
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
20+
expect(deabs(result)).toEqual(processedComponents);
2021
});
2122

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

2829
it('getComponentFiles() should accept components as a function that returns globs', () => {
@@ -35,13 +36,13 @@ it('getComponentFiles() should accept components as a function that returns glob
3536

3637
it('getComponentFiles() should accept components as an array of file names', () => {
3738
const result = getComponentFiles(components, configDir);
38-
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
39+
expect(deabs(result)).toEqual(processedComponents);
3940
});
4041

4142
it('getComponentFiles() should accept components as an array of absolute paths', () => {
4243
const absolutize = files => files.map(file => path.join(configDir, file));
4344
const result = getComponentFiles(absolutize(components), configDir);
44-
expect(deabs(result)).toEqual(['~/one.js', '~/two.js']);
45+
expect(deabs(result)).toEqual(processedComponents);
4546
});
4647

4748
it('getComponentFiles() should accept components as an array of globs', () => {

‎loaders/utils/getComponentFiles.js

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const path = require('path');
21
const glob = require('glob');
32
const isFunction = require('lodash/isFunction');
43
const isString = require('lodash/isString');
@@ -16,24 +15,18 @@ const getComponentGlobs = components => {
1615
);
1716
};
1817

19-
const getFilesMatchingGlobs = (components, rootDir, ignore) =>
20-
components
21-
.map(listItem => {
22-
// Check if the string looks like a glob pattern by using hasMagic
23-
if (glob.hasMagic(listItem)) {
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-
});
32-
}
33-
// Wrap path in an array so reduce always gets an array of arrays
34-
return [listItem];
35-
})
18+
const getFilesMatchingGlobs = (components, rootDir, ignore) => {
19+
ignore = ignore || [];
20+
return components
21+
.map(listItem =>
22+
glob.sync(listItem, {
23+
cwd: rootDir,
24+
ignore,
25+
absolute: true,
26+
})
27+
)
3628
.reduce((accumulator, current) => accumulator.concat(current), []);
29+
};
3730

3831
/**
3932
* Return absolute paths of components that should be rendered in the style guide.
@@ -54,8 +47,5 @@ module.exports = function getComponentFiles(components, rootDir, ignore) {
5447
// Resolve list of components from globs
5548
const componentFiles = getFilesMatchingGlobs(componentGlobs, rootDir, ignore);
5649

57-
// Make paths absolute
58-
const componentFilesAbsolute = componentFiles.map(file => path.resolve(rootDir, file));
59-
60-
return componentFilesAbsolute;
50+
return componentFiles;
6151
};

‎scripts/schemas/config.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
// in loaders/styleguide-loader.js
33

44
const EXTENSIONS = 'js,jsx,ts,tsx';
5-
const DEFAULT_COMPONENTS_PATTERN = `src/@(components|Components)/**/*.{${EXTENSIONS}}`;
5+
const DEFAULT_COMPONENTS_PATTERN =
6+
// HACK: on windows, the case insensitivity makes each component appear twice
7+
// to avoid this issue, the case management is removed on win32
8+
// it virtually changes nothing
9+
process.platform === 'win32'
10+
? /* istanbul ignore next: no windows on our test plan */ `src/components/**/*.{${EXTENSIONS}}`
11+
: `src/@(components|Components)/**/*.{${EXTENSIONS}}`;
612

713
const path = require('path');
814
const startCase = require('lodash/startCase');

0 commit comments

Comments
 (0)
Please sign in to comment.