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

[Tooling] yarn splash (beta) #2113

Merged
merged 40 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
08b6744
First iteration of the observer CLI
kaelig Sep 10, 2019
44da583
Load .ts files too
kaelig Sep 10, 2019
9912178
Add graph script
Sep 11, 2019
9d511ba
Enable compilation of .ts files
kaelig Sep 11, 2019
3adc842
Remove tinycolor2 dependency
kaelig Sep 11, 2019
a9da56e
Make filter more portable across environments
kaelig Sep 11, 2019
be17151
Add scoping to getGitStagedFiles
kaelig Sep 11, 2019
b0c0503
Try to use functions from treebuilder.ts
kaelig Sep 11, 2019
90801ff
Add types
Sep 11, 2019
5fde385
Fix filter
kaelig Sep 11, 2019
acd9c2e
Formatting
Sep 11, 2019
370d844
Remove absolute part of the path
kaelig Sep 11, 2019
8cb4113
Plug data from git status and improve listing
kaelig Sep 11, 2019
f7bfb82
Rename script to yarn splash, add watcher mode
kaelig Sep 12, 2019
61b233e
Align summary text to the right
kaelig Sep 12, 2019
e50011a
Change filtering
Sep 12, 2019
204bb60
Add temporary workaround to the skipIndexFile function
kaelig Sep 12, 2019
7ce1a5e
Move treebuilder.ts to scripts/splash
kaelig Sep 12, 2019
f10ffd3
Merge branch 'ink-cli' of github.com:Shopify/polaris-react into ink-cli
kaelig Sep 12, 2019
7493e70
Remove skipIndexFile
kaelig Sep 12, 2019
fb5019b
Add docs
kaelig Sep 12, 2019
0db5a4b
Add changelog item
kaelig Sep 12, 2019
72bc4ac
Merge branch 'master' of github.com:Shopify/polaris-react into ink-cli
kaelig Sep 12, 2019
230d07c
Add mention that the watcher is still alpha
kaelig Sep 12, 2019
961e3ae
Remove unused type dependency
kaelig Sep 12, 2019
4028392
Add missing space
kaelig Sep 12, 2019
af429e9
Add missing space
kaelig Sep 12, 2019
f397782
Verifies the index file is in a component folder
kaelig Sep 12, 2019
9d1873c
Account for .ts files
kaelig Sep 12, 2019
9b081de
Show utils.tsx files again
kaelig Sep 12, 2019
3502aef
Handle loading status properly
kaelig Sep 12, 2019
4a53045
Stop filtering index.ts files out
kaelig Sep 12, 2019
aed6ff3
Integrate with storybook
Sep 12, 2019
ec0fb8c
Make component grid layout responsive
kaelig Sep 12, 2019
12e60e6
Merge branch 'ink-cli' of github.com:Shopify/polaris-react into ink-cli
kaelig Sep 12, 2019
4a575dd
Delete watch mode
kaelig Sep 12, 2019
7dd8e0a
Update useEffects flow
Sep 12, 2019
7eeba2a
enum
Sep 13, 2019
768957f
Revert "enum"
kaelig Sep 13, 2019
1863788
Revert "Update useEffects flow"
kaelig Sep 13, 2019
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
12 changes: 12 additions & 0 deletions .storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// For more information refer the docs: https://storybook.js.org/configurations/custom-webpack-config

const path = require('path');
const spawn = require('child_process').spawn;

const postcssShopify = require('postcss-shopify');

Expand Down Expand Up @@ -111,6 +112,17 @@ module.exports = ({config, mode}) => {
},
];

config.plugins.push({
apply: (compiler) => {
compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
const spawnedProcess = spawn('yarn splash', {
shell: true,
stdio: 'inherit',
});
});
},
});

config.module.rules = [config.module.rules[0], ...extraRules];

if (isProduction) {
Expand Down
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Development workflow

- Added [`yarn splash` (beta)](/scripts/splash/), a command-line interface to observe the splash zone of a change across the component library ([#2113](https://github.com/Shopify/polaris-react/pull/2113))

### Dependency upgrades

### Code quality
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"readme-update-version": "node ./scripts/readme-update-version",
"version": "yarn run readme-update-version",
"storybook": "start-storybook -p 6006 --quiet",
"storybook:build": "yarn run copy-polaris-tokens && build-storybook -o build/storybook/static"
"storybook:build": "yarn run copy-polaris-tokens && build-storybook -o build/storybook/static",
"splash": "babel-node --extensions '.tsx','.ts' ./scripts/splash/index.tsx"
},
"stylelint": {
"extends": [
Expand Down Expand Up @@ -89,6 +90,7 @@
],
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/node": "^7.6.1",
"@percy/storybook": "^3.0.2",
"@shopify/jest-dom-mocks": "^2.1.1",
"@shopify/react-testing": "^1.7.3",
Expand All @@ -104,7 +106,7 @@
"@types/enzyme": "^3.10.3",
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/lodash": "^4.14.108",
"@types/node": "^11.13.8",
"@types/node": "^12.7.4",
kaelig marked this conversation as resolved.
Show resolved Hide resolved
"archiver": "^3.0.0",
"babel-core": "7.0.0-bridge.0",
"babel-loader": "^8.0.5",
Expand All @@ -118,9 +120,10 @@
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"fs-extra": "^7.0.1",
"glob": "^7.1.3",
"glob": "^7.1.4",
"gray-matter": "^4.0.2",
"in-publish": "^2.0.0",
"ink": "^2.3.0",
"isomorphic-fetch": "^2.2.1",
"js-yaml": "^3.13.1",
"marked": "^0.6.2",
Expand Down Expand Up @@ -153,8 +156,7 @@
"shx": "^0.3.2",
"storybook-chroma": "^2.1.1",
"svgo": "^1.2.2",
"typescript": "~3.5.1",
"yargs": "^13.2.2"
"typescript": "~3.5.1"
},
"peerDependencies": {
"react": "^16.8.6",
Expand Down Expand Up @@ -189,6 +191,7 @@
"@types/react-transition-group": "^2.0.7",
"hoist-non-react-statics": "^3.3.0",
"lodash": "^4.17.4",
"node-cmd": "^3.0.0",
"tslib": "^1.9.3"
}
}
6 changes: 6 additions & 0 deletions scripts/splash/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"jsx-a11y/accessible-emoji": "off",
"shopify/jsx-no-hardcoded-content": "off"
}
}
20 changes: 20 additions & 0 deletions scripts/splash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `yarn splash` (beta)

`yarn splash` is a command-line interface to observe the splash zone of a change across the component library.

It answers the question:

> When I modify a component, what parts of the system did that also touch (also known as the change’s “splash zone”)?

## How to use `yarn splash`

1. Edit files in `src/`, such as components 🧩 and style sheets 🎨
2. Run `yarn splash` to see the splash zone of your changes in the working directory

💡 Tip: <kbd>command</kbd> + click a file path to open it in your text editor

## Feedback and bug reports

Found an issue or want to share feedback?

Reach out on Slack in [#polaris-tooling](https://shopify.slack.com/messages/CCNUS0FML).
200 changes: 200 additions & 0 deletions scripts/splash/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import path from 'path';
import React, {useState, useEffect} from 'react';
import {Box, Text, Color, render} from 'ink';
import sortBy from 'lodash/sortBy';
import {getGitStagedFiles, getDependencies} from './treebuilder';

const excludedFileNames = (fileName) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these checks are a bit too lose. For example, .includes('test') will also filter file names that just happen to include those characters, even if it's not a test. I think it'd be good to be a bit more specific here for all checks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I agree. As we make it more codebase-agnostic, this won't scale.

!fileName.includes('test') && !fileName.includes('types');

const getEmojiForExtension = (extension) => {
switch (extension) {
case '.tsx':
kaelig marked this conversation as resolved.
Show resolved Hide resolved
case '.ts':
return '🧩';
case '.scss':
return '🎨';
default:
return '❔';
}
};

const formatDependencies = (dependencies) =>
dependencies
.filter(({fileName}) => excludedFileNames(fileName))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think we should filter here too?

I think, for example, if you made a change to Avatar/index.ts, or to a utils.ts file, I would still expect the tool to tell me accurately what the impact is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. Let me put those index and utils back in. If feedback says this is too overwhelming we can always filter them out again.

.map((dependency) => ({
pathname: `${path.dirname(dependency.fileName)}/`,
filename: path.basename(dependency.fileName),
dependencies: sortBy(
dependency.dependencies.filter(excludedFileNames).map((dependency) => ({
pathname: `${path.dirname(dependency)}/`,
filename: path.basename(dependency),
componentName: path
Copy link
Contributor

Choose a reason for hiding this comment

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

This might not necessarily be a component

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'll have to think about those edge cases, yeah.

.dirname(dependency)
.replace('src/components/', '')
.split('/')[0],
})),
['pathname', 'filename'],
),
}));

const Component = ({pathname, filename, dependencies}) => (
Copy link
Member

Choose a reason for hiding this comment

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

Please define these components (and really all top-level functions) using the function Component() style syntax. Using Arrow functions means that the components will all have no name to use for debugging which will be a pain when you get stack trace staying there was an error in Unknown component which was inside Unknown component.

<Box marginBottom={1} flexDirection="column">
<Box>
<Box width={3}>{getEmojiForExtension(path.extname(filename))}</Box>
<Text bold>
<Color greenBright>
{pathname}
{filename}
</Color>
</Text>
</Box>
<Box marginLeft={3}>
<Box width={23}>Component name</Box>
Files potentially affected (total: {dependencies.length})
</Box>
{dependencies.map(({pathname, filename, componentName}) => (
<Box
marginLeft={3}
key={pathname + filename}
flexDirection={process.stdout.columns > 80 ? 'row' : 'column'}
>
<Box width={23}>
<Color dim>{'<'}</Color>
<Color>{componentName}</Color>
<Color dim>{' />'}</Color>
</Box>
<Box textWrap="wrap">
<Color dim>{pathname}</Color>
{filename}
</Box>
</Box>
))}
</Box>
);

const Components = ({components, status}) => (
<React.Fragment>
{status === 'loading' && (
<Box marginLeft={4} marginBottom={1}>
⏳{' '}Please wait during compilation… Beep boop beep 🤖
</Box>
)}

{status === 'loaded' &&
components.map(({pathname, filename, dependencies}) => (
<Component
key={pathname + filename}
pathname={pathname}
filename={filename}
dependencies={dependencies}
/>
))}
</React.Fragment>
);

const Summary = ({
componentsModified,
dependencies,
status,
}: {
componentsModified: number;
dependencies: number;
status: string;
}) => (
<Box flexDirection="column">
<Box>
<Box width={30}>
<Text>Files modified:</Text>
</Box>
<Box justifyContent="flex-end" width={3}>
{componentsModified}
</Box>
</Box>
<Box>
<Box width={30}>
<Text>Files potentially affected:</Text>
</Box>
<Box justifyContent="flex-end" width={3}>
{status === 'loading' ? '⏳' : dependencies}
</Box>
</Box>
</Box>
);

const App = () => {
const [stagedFiles, setStagedFiles] = useState([]);
const [data, setData] = useState([]);
const [dataStatus, setDataStatus] = useState('loading');
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes more sense to make this a loading boolean prop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right now it's loading/loaded, but there can be other loading states, such as 'error' or 'cancelled' that I'd like to leave this open for.

Copy link
Member

@BPScott BPScott Sep 12, 2019

Choose a reason for hiding this comment

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

In that case can we have the vale be an enum rather than an arbitary string. There's an example of that in #2067

enum Status {
  Pending = 'PENDING',
  Loaded = 'LOADED',
  Errored = 'ERRORED',
}

Then in your useState:

 const [status, setStatus] = useState<Status>(Status.Pending);


useEffect(() => {
const getStagedFiles = async () => {
const staged = (await getGitStagedFiles('src/')) as string[];
setStagedFiles(staged);

if (staged.length === 0) {
setDataStatus('loaded');
}
};
getStagedFiles();
}, []);

useEffect(() => {
if (stagedFiles.length > 0) {
const dependencies = getDependencies(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not too happy with the API here, what do you think? Any suggestions for how to improve it?

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 also think we can improve it. Since we'll gain insights as you explore how to make this work for GitHub, webpack, and storybook… I'd like to wait to revamp it when you know more about all these other use cases.

'src/**/*.tsx',
'*.test.tsx',
stagedFiles,
);
setData(formatDependencies(dependencies));
setDataStatus('loaded');
Copy link
Contributor

Choose a reason for hiding this comment

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

You never set this prop to loading. Wouldn't you want to do that when a re-render is triggered, and you're re-running the script and the git status function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure what you mean by that, but let's talk about it during our next pairing session

}
}, [setData, stagedFiles]);

return (
<React.Fragment>
<Box marginBottom={1} flexDirection="column">
<Box>
<Box width={3}>💦</Box>
<Box>
<Text bold>yarn splash</Text>: Observe the splash zone of a change
across the entire library
</Box>
</Box>
</Box>
<Components components={data} status={dataStatus} />
<Summary
componentsModified={stagedFiles.length}
status={dataStatus}
dependencies={
new Map([
...data.map(({pathname, filename}) => [
pathname,
pathname + filename,
]),
...data.reduce(
(val, curr) =>
val.concat(
curr.dependencies.map(({pathname}) => [
pathname,
curr.pathname + curr.filename,
]),
),
[],
),
]).size
}
/>
<Box marginTop={1}>
<Color dim>
<Box width={3}>💡</Box>
<Box>
Tip: command + click a file path to open it in your text editor
</Box>
</Color>
</Box>
</React.Fragment>
);
};

render(<App />);
Loading