Skip to content

Conversation

@dotcs
Copy link
Contributor

@dotcs dotcs commented May 12, 2017

This PR fixes the getExampleFilename method and re-implements the logic such that the filename can be case-insensitive as requested here: #423 (comment).

Copy link
Member

@sapegin sapegin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is very cool feature!

// Search is case-insensitive, so `README.md` or `BUTTON.md` is also
// valid.
const readmeIndex = lowerFiles.indexOf('readme.md');
const componentIndex = lowerFiles.indexOf(`${componentName.toLowerCase()}.md`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d move this line after the first condition; and you don't need an else because you have return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I think we still need else branch because it can be that neither a readme.md nor a mycomponent.md file is available. In this case readmeIndex and componentIndex have both the value -1 and both conditions are wrong. In this case false is returned as in the previous implementation. Please have a look in the updated code.

if (fs.existsSync(file)) {
return file;
}
const files = fs.readdirSync(dirname);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we cache it somehow? Imagine a project with all components in a single folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had a look, but I'm not sure where caching could be done. getExampleFilename is called in loaders/props-loader.js and processComponent.js. The first one is already cached via webpack as it's a cachable loader (please correct me if I'm wrong). The second one is called to process a component in case it has changed, so I think in this case we should update anyway. Where in the application logic would you see the caching layer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean to cache readdirSync because it will be called for each component possible with the same results if all of them are in the same folder.

@dotcs
Copy link
Contributor Author

dotcs commented May 12, 2017

@sapegin Thanks for the feedback. I'll have a look hopefully somewhen later this weekend. I'll see if I can implement cache and see what I can do about failing tests (somehow they worked locally ... ??). I'll come back to you when I'm done.

@dotcs dotcs changed the title Fix: Rewrite getExampleFilename logic to make it case insensitive (#423) (WIP) Fix: Rewrite getExampleFilename logic to make it case insensitive (#423) May 12, 2017
@codecov-io
Copy link

codecov-io commented May 27, 2017

Codecov Report

Merging #440 into master will increase coverage by 0.05%.
The diff coverage is 88.88%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #440      +/-   ##
==========================================
+ Coverage   94.32%   94.38%   +0.05%     
==========================================
  Files          82       82              
  Lines        1146     1158      +12     
  Branches      254      258       +4     
==========================================
+ Hits         1081     1093      +12     
  Misses         61       61              
  Partials        4        4
Impacted Files Coverage Δ
src/rsg-components/Playground/Playground.js 84.37% <ø> (ø) ⬆️
src/rsg-components/Preview/Preview.js 50% <0%> (ø) ⬆️
loaders/utils/getProps.js 100% <100%> (ø) ⬆️
loaders/utils/getComponentFiles.js 100% <100%> (ø) ⬆️
loaders/utils/client/evalInContext.js 100% <100%> (ø) ⬆️
loaders/styleguide-loader.js 100% <100%> (ø) ⬆️
loaders/props-loader.js 79.31% <100%> (+0.73%) ⬆️
scripts/utils/sanitizeConfig.js 100% <100%> (ø) ⬆️
src/utils/utils.js 96.96% <100%> (ø) ⬆️
scripts/make-webpack-config.js 100% <100%> (ø) ⬆️
... and 18 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 97d233b...99d7e61. Read the comment docs.

@dotcs
Copy link
Contributor Author

dotcs commented May 27, 2017

@sapegin Sorry for the long delay. I've been quite busy within the last two weeks. I've implemented a cache layer into the getExampleFilename implementation. Can you have a look if this fits what you have thought of?

The idea goes like this:
The props-loader is marked as cachable, so webpack will only call the real implementation if the underlying file system has changed somehow. In this case we have to invalidate the cache. But for all calls of getExampleFilename within one iteration after files have changed the cache can be used to reduce file io reads.

What this PR misses is documentation of the cache as the second param of the getExampleFilename implementation. I leave this up to you if you think it should be documented.

Side note: I had to fix some lint issues (see commit dcf17a9) because code from master was invalid...?!

@dotcs dotcs changed the title (WIP) Fix: Rewrite getExampleFilename logic to make it case insensitive (#423) Fix: Rewrite getExampleFilename logic to make it case insensitive (#423) May 27, 2017
@sapegin
Copy link
Member

sapegin commented May 28, 2017

Side note: I had to fix some lint issues (see commit dcf17a9) because code from master was invalid...?!

Looks like you need to npm install after merging master to your branch.

getExampleFilename: {
type: 'function',
default: componentPath => {
default: (componentPath, cache) => {
Copy link
Member

@sapegin sapegin May 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain benefits of your implementation over _.memoize?

I’d make a cached case-insensitive version of fs.existsSync and pass it to to getExampleFilename. Passing just an object doesn’t look useful and will be hard to explain in the docs.

sapegin added a commit that referenced this pull request Jun 24, 2017
@sapegin sapegin closed this Jun 24, 2017
sapegin added a commit that referenced this pull request Jun 28, 2017
## New features

### Copy pathline to clipboard button

![](http://wow.sapegin.me/3t2u3S0P0L2T/Image%202017-06-28%20at%204.17.16%20PM.png)

(#485, #471 by @SaraVieira)

### Easier way to override style guide React components

New config option `styleguideComponents` to override React components used to render a style guide.

```javascript
module.exports = {
	styleguideComponents: {
		Logo: path.join(__dirname, 'styleguide/components/Logo'),
		StyleGuideRenderer: path.join(__dirname, 'styleguide/components/StyleGuide'),
	},
};
```

(#504)

### Customizable logging

New `logger` option:

```javascript
module.exports = {
	logger: {
    // One of: info, debug, warn
    // Suppress messages
		info: () => {},
    // Override display function
		warn: message => console.warn(`NOOOOOO: ${message}`),
	},
};
```

(#472, #510)

## Bug fixes

* Allow `devServer.watchConfig` to be configured, this also means Vagrant is now supported (#515, #516 by @esphen)
* Default `getExampleFilename` should be case-insensitive (#423, #460, #440, #482)
* Allow dynamic JSS styles
* Isolate search placeholder styles (#509, #491 by @n1313)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants