Skip to content

Commit

Permalink
[docs] Demo codesandbox deps (#10158)
Browse files Browse the repository at this point in the history
  • Loading branch information
caub authored and oliviertassinari committed Feb 4, 2018
1 parent 98e065e commit 2239f6f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 34 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module.exports = {
'import/unambiguous': 'off', // scripts
'import/namespace': ['error', { allowComputed: true }],
'import/no-extraneous-dependencies': 'off',
'import/order': ['error', {
'groups': [['index', 'sibling', 'parent', 'internal', 'external', 'builtin']],
'newlines-between': 'never'
}],

'react/jsx-indent': 'off', // Incompatible with prettier
'react/jsx-closing-bracket-location': 'off', // Incompatible with prettier
Expand Down
27 changes: 2 additions & 25 deletions docs/src/modules/components/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CodeIcon from 'material-ui-icons/Code';
import Tooltip from 'material-ui/Tooltip';
import Github from 'docs/src/modules/components/GitHub';
import MarkdownElement from 'docs/src/modules/components/MarkdownElement';
import { getDependencies } from 'docs/src/modules/utils/helpers';

const styles = theme => ({
root: {
Expand Down Expand Up @@ -69,24 +70,6 @@ function compress(object) {
.replace(/=+$/, ''); // Remove ending '='
}

function conditionalDependencies(raw) {
const dependencies = [
'react-autosuggest',
'autosuggest-highlight',
'downshift',
'react-select',
'react-text-mask',
'react-number-format',
];

return dependencies.reduce((acc, dependency) => {
if (raw.indexOf(`from '${dependency}`) !== -1) {
acc[dependency] = 'latest';
}
return acc;
}, {});
}

class Demo extends React.Component {
state = {
codeOpen: false,
Expand All @@ -105,13 +88,7 @@ class Demo extends React.Component {
files: {
'package.json': {
content: {
dependencies: {
react: 'latest',
'react-dom': 'latest',
'material-ui': 'next',
'material-ui-icons': 'latest',
...conditionalDependencies(this.props.raw),
},
dependencies: getDependencies(this.props.raw),
},
},
'demo.js': {
Expand Down
23 changes: 18 additions & 5 deletions docs/src/modules/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ export function titleize(string) {

return string
.split('-')
.map(word => word.split(''))
.map(letters => {
const first = letters.shift();
return [first.toUpperCase(), ...letters].join('');
})
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}

Expand All @@ -31,3 +27,20 @@ export function pageToTitle(page) {

return titleize(name);
}

export function getDependencies(raw) {
const deps = {
react: 'latest',
'react-dom': 'latest',
'material-ui': 'next',
};
const re = /^import\s.*\sfrom\s+'([^']+)'/gm;
let m;
// eslint-disable-next-line no-cond-assign
while ((m = re.exec(raw))) {
// handle scope names
const name = m[1].charAt(0) === '@' ? m[1].split('/', 2).join('/') : m[1].split('/', 1)[0];
deps[name] = deps[name] || 'latest';
}
return deps;
}
52 changes: 52 additions & 0 deletions docs/src/modules/utils/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { assert } from 'chai';
import { getDependencies } from './helpers';

const s1 = `import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Input, { InputLabel } from 'material-ui/Input';
import { FormControl, FormHelperText } from 'material-ui/Form';
import Select from 'material-ui/Select';
import FooBar, { Qux } from '@foo-bar/bip';
const styles = theme => ({
container: {
display: 'flex',
flexWrap: 'wrap',
},
formContro
`;

const s2 = `import React from 'react';
import PropTypes from 'prop-types';
import * as _ from '@unexisting/thing';
import Autosuggest from 'react-autosuggest';
import match from 'autosuggest-highlight/match';
import parse from 'autosuggest-highlight/parse';
import TextField from 'material-ui/TextField';
import Paper from 'material-ui/Paper';
import { MenuItem } from 'material-ui/Menu';
import { withStyles } from 'material-ui/styles';
const suggestions = [`;

describe('docs getDependencies helpers', () => {
it('generate the right npm dependencies', () => {
assert.deepEqual(getDependencies(s1), {
react: 'latest',
'react-dom': 'latest',
'material-ui': 'next',
'prop-types': 'latest',
'@foo-bar/bip': 'latest',
});
assert.deepEqual(getDependencies(s2), {
react: 'latest',
'react-dom': 'latest',
'material-ui': 'next',
'prop-types': 'latest',
'@unexisting/thing': 'latest',
'react-autosuggest': 'latest',
'autosuggest-highlight': 'latest',
});
});
});
3 changes: 0 additions & 3 deletions docs/src/modules/utils/prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import 'prismjs/components/prism-javascript';
import 'prismjs/components/prism-css';
import 'prismjs/components/prism-diff';
import 'prismjs/components/prism-jsx';

/* eslint-disable import/no-webpack-loader-syntax */
import lightTheme from 'prismjs/themes/prism.css';
import darkTheme from 'prismjs/themes/prism-okaidia.css';
/* eslint-enable import/no-webpack-loader-syntax */

export { lightTheme, darkTheme };

Expand Down
1 change: 1 addition & 0 deletions examples/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
'import/no-named-as-default': 'off',
'import/default': 'off',
'import/no-named-as-default-member': 'off',
'import/order': 'off',
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"size:overhead:why": "size-limit --why ./test/size/overhead.js",
"spellcheck": "eslint . --config .eslintrc.spellcheck.js && echo \"eslint: no lint errors\"",
"test": "yarn lint && yarn flow && yarn typescript && yarn test:unit",
"test:unit": "cross-env NODE_ENV=test mocha test/**/*.spec.js src/{,**/}*.spec.js",
"test:unit": "cross-env NODE_ENV=test mocha test/**/*.spec.js src/{,**/}*.spec.js docs/src/**/*.spec.js",
"test:watch": "yarn test:unit -w",
"test:coverage": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha test/**/*.spec.js src/{,**/}*.spec.js && nyc report -r lcovonly",
"test:coverage:html": "cross-env NODE_ENV=test BABEL_ENV=coverage nyc mocha test/**/*.spec.js src/{,**/}*.spec.js && nyc report --reporter=html",
Expand Down

0 comments on commit 2239f6f

Please sign in to comment.