Skip to content

Commit 809d04f

Browse files
authored
Merge pull request #1 from StevenLangbroek/v4/global_styles
v3 to v4: injectGlobalStyle to createGlobalStyle
2 parents bf664c0 + 96f1628 commit 809d04f

15 files changed

+538
-300
lines changed

.prettierrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"arrowParens": "avoid",
3+
"printWidth": 100,
4+
"jsxBracketSameLine": false,
5+
"semi": true,
6+
"singleQuote": true,
7+
"tabWidth": 2,
8+
"trailingComma": "es5"
9+
}

README.md

+48-46
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,35 @@ Automatic codemods to upgrade your styled-components code to newer versions.
44

55
## Installation
66

7-
```sh
8-
npm install -g styled-components-codemods
9-
```
7+
Thanks to `npx` no installation is needed, just run the command below!
108

119
## Usage
1210

13-
```sh
14-
Usage: styled-components-codemods [options] [command]
11+
```sh
12+
Usage: npx styled-components-codemods [options] [command]
1513

1614
Options:
1715

18-
-V, --version output the version number
19-
-h, --help output usage information
16+
-V, --version output the version number
17+
-h, --help output usage information
2018

2119
Commands:
2220

23-
v4 [files...] Upgrade your code to styled-components v4 (codemods .extend)
21+
v4 [...files] Run all v4 codemods
22+
v4-extendToStyled [...files] Run just the extendToStyled codemod
23+
v4-injectGlobalToCreateGlobalStyle [...files] Run just the injectGlobalToCreateGlobalStyle codemod
2424

2525
Examples:
2626

27-
$ styled-components-codemods v4 src/components/Box.js src/components/Button.js
27+
$ styled-components-codemods v4-extendToStyled src/components/Box.js src/components/Button.js
2828
$ styled-components-codemods v4 src/**/*.js (this will only work if your terminal expands globs)
2929
```
3030
3131
### Codemods
3232
33-
3433
#### v4
3534
36-
In version 4 of `styled-components` the `Component.extends` API will be removed in favor of only using `styled(Component)`. This codemod replaces all `.extends` calls to equivalent `styled()` calls instead.
35+
In version 4 of `styled-components` the `Component.extends` API will be removed in favor of only using `styled(Component)`. This codemod replaces all `.extends` calls to equivalent `styled()` calls instead. Furthermore, the injectGlobal API has been upgraded to slot into React's lifecycle more naturally. It refactors all `injectGlobal` calls, and warns you where they are, so you can export them and include them when rendering.
3736
3837
##### Limitations
3938
@@ -47,37 +46,42 @@ There is no way to distinguish whether `.extend` identifier is related to `style
4746
4847
<summary>Code Before</summary>
4948
50-
5149
```javascript
52-
StyledComponent.extend``
50+
StyledComponent.extend``;
5351
54-
StyledComponent.extend`color: red;`
52+
StyledComponent.extend`
53+
color: red;
54+
`;
5555
56-
StyledComponent.extend({ color: "red" })
56+
StyledComponent.extend({ color: "red" });
5757
58-
StyledComponent.extend
58+
StyledComponent.extend;
5959
60-
StyledComponent.extend``.extend
60+
StyledComponent.extend``.extend;
6161
62-
StyledComponent.extend({ color: red }).extend
62+
StyledComponent.extend({ color: red }).extend;
6363
64-
styled.div``.extend``
64+
styled.div``.extend``;
6565
66-
styled.div`color: red;`.extend`color: blue;`
66+
styled.div`
67+
color: red;
68+
`.extend`color: blue;`;
6769
68-
styled.div({ color: "red"}).extend({ color: "blue"})
70+
styled.div({ color: "red" }).extend({ color: "blue" });
6971
70-
StyledComponent.withComponent('div').extend``
72+
StyledComponent.withComponent("div").extend``;
7173
72-
StyledComponent.withComponent('div').extend`color: red;`
74+
StyledComponent.withComponent("div").extend`color: red;`;
7375
74-
StyledComponent.withComponent('div').extend()
76+
StyledComponent.withComponent("div").extend();
7577
76-
StyledComponent.withComponent('div').extend({ color: red })
78+
StyledComponent.withComponent("div").extend({ color: red });
7779
78-
StyledComponent.extend().extend().extend().extend``
80+
StyledComponent.extend()
81+
.extend()
82+
.extend().extend``;
7983
80-
StyledComponent.extend``.extend().extend``.extend``
84+
StyledComponent.extend``.extend().extend``.extend``;
8185
```
8286
8387
</details>
@@ -87,49 +91,47 @@ StyledComponent.extend``.extend().extend``.extend``
8791
<summary>Code after</summary>
8892
8993
```javascript
90-
import styled, { css } from 'styled-components'
94+
import styled, { css } from "styled-components";
9195
92-
styled(StyledComponent)``
96+
styled(StyledComponent)``;
9397
9498
styled(StyledComponent)`
9599
color: red;
96-
`
100+
`;
97101
98-
styled(StyledComponent)({ color: 'red' })
102+
styled(StyledComponent)({ color: "red" });
99103
100-
styled(StyledComponent)
104+
styled(StyledComponent);
101105
102-
styled(styled(StyledComponent)``)
106+
styled(styled(StyledComponent)``);
103107
104-
styled(styled(StyledComponent)({ color: red }))
108+
styled(styled(StyledComponent)({ color: red }));
105109
106-
styled(styled.div``)``
110+
styled(styled.div``)``;
107111
108112
styled(
109113
styled.div`
110114
color: red;
111115
`
112116
)`
113117
color: blue;
114-
`
118+
`;
115119
116-
styled(styled.div({ color: 'red' }))({ color: 'blue' })
120+
styled(styled.div({ color: "red" }))({ color: "blue" });
117121
118-
styled(StyledComponent.withComponent('div'))``
122+
styled(StyledComponent.withComponent("div"))``;
119123
120-
styled(StyledComponent.withComponent('div'))`
124+
styled(StyledComponent.withComponent("div"))`
121125
color: red;
122-
`
126+
`;
123127
124-
styled(StyledComponent.withComponent('div'))()
128+
styled(StyledComponent.withComponent("div"))();
125129
126-
styled(StyledComponent.withComponent('div'))({ color: red })
130+
styled(StyledComponent.withComponent("div"))({ color: red });
127131
128-
styled(styled(styled(styled(StyledComponent)())())())``
132+
styled(styled(styled(styled(StyledComponent)())())())``;
129133
130-
styled(styled(styled(styled(StyledComponent)``)())``)``
134+
styled(styled(styled(styled(StyledComponent)``)())``)``;
131135
```
132136
133137
</details>
134-
135-

bin/styled-components-codemods.js

+18-30
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,32 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
const path = require('path');
54
const program = require('commander');
6-
const { spawn } = require('child_process');
75
const pkg = require('../package.json');
6+
const { registerCodemods } = require('./utils');
87

9-
program
10-
.version(pkg.version)
11-
.command('v4 [files...]')
12-
.action((files) => {
13-
if (!Array.isArray(files) || files.length === 0) {
14-
throw new Error('Please provide a list of files to run the codemod on.');
15-
}
8+
/**
9+
* Add the version-specific codemods here.
10+
*/
11+
const codemods = {
12+
v4: ['extendToStyled', 'injectGlobalToCreateGlobalStyle'],
13+
};
1614

17-
console.log('Running v4 codemods...\n')
18-
const child = spawn(path.resolve(path.join(__dirname, '../node_modules/.bin/codemod')), ['--plugin', path.resolve(path.join(__dirname, '../src/v4/index.js')), ...files])
15+
program.version(pkg.version);
1916

20-
child.stdout.on('data', chunk => {
21-
console.log(chunk.toString())
22-
})
23-
// since these are streams, you can pipe them elsewhere
24-
child.stderr.on('data', chunk => {
25-
console.error(chunk.toString());
26-
});
27-
28-
child.on('close', (code) => {
29-
if (code === 0) {
30-
console.log('Finished running codemods!')
31-
}
32-
});
33-
});
17+
registerCodemods(codemods, program);
3418

3519
// must be before .parse() since node's emit() is immediate
36-
program.on('--help', function(){
37-
console.log('')
20+
program.on('--help', function() {
21+
console.log('');
3822
console.log('Examples:');
39-
console.log('')
40-
console.log(' $ styled-components-codemods v4 src/components/Box.js src/components/Button.js');
41-
console.log(' $ styled-components-codemods v4 src/**/*.js (this will only work if your terminal expands globs)');
23+
console.log('');
24+
console.log(
25+
' $ styled-components-codemods v4-extendToStyled src/components/Box.js src/components/Button.js'
26+
);
27+
console.log(
28+
' $ styled-components-codemods v4 src/**/*.js (this will only work if your terminal expands globs)'
29+
);
4230
});
4331

4432
program.parse(process.argv);

bin/utils.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const { spawnSync } = require('child_process');
4+
const glob = require('fast-glob');
5+
const { curry } = require('lodash/fp');
6+
const path = require('path');
7+
8+
const runCodemod = curry((version, files, mod) => {
9+
const pluginPath = path.resolve(path.join(__dirname, '..', 'src', version, `${mod}.js`));
10+
11+
spawnSync(path.resolve(path.join(__dirname, '../node_modules/.bin/codemod')), [
12+
'--plugin',
13+
pluginPath,
14+
...files,
15+
]);
16+
});
17+
18+
const runCodemods = (version, mods) => {
19+
return (...files) => {
20+
files = glob.sync(files.filter(f => typeof f === 'string'));
21+
22+
if (typeof files !== 'string' && !Array.isArray(files)) {
23+
throw new Error('No files provided.');
24+
}
25+
26+
console.log(`\nRunning the ${mods.join(', ')} codemods against the following files:\n`);
27+
console.log(files);
28+
29+
mods.forEach(runCodemod(version, files));
30+
31+
console.log('\nDone. Enjoy!\n');
32+
};
33+
};
34+
35+
exports.registerCodemods = curry((mods, program) => {
36+
Object.keys(mods).forEach(version => {
37+
program
38+
.command(`${version} [...files]`)
39+
.description(`Run all ${version} codemods`)
40+
.action(runCodemods(version, mods[version]));
41+
42+
mods[version].forEach(mod => {
43+
program
44+
.command(`${version}-${mod} [...files]`)
45+
.description(`Run just the ${mod} codemod`)
46+
.action((...files) => runCodemod(version, files, mod));
47+
});
48+
});
49+
});

manualRun.js

-43
This file was deleted.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
"babel-codemod": "^2.0.6",
2424
"babel-core": "^6.26.3",
2525
"commander": "^2.18.0",
26+
"fast-glob": "^2.2.3",
2627
"lodash": "^4.17.10"
2728
},
2829
"scripts": {
29-
"test": "jest",
30-
"start": "node manualRun.js"
30+
"test": "jest"
3131
},
3232
"jest": {
3333
"testEnvironment": "node"

src/v4/common.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
exports.manipulateOptions = function manipulateOptions(_, parserOpts) {
4+
parserOpts.plugins.push('asyncGenerators');
5+
parserOpts.plugins.push('classProperties');
6+
parserOpts.plugins.push('decorators');
7+
parserOpts.plugins.push('doExpressions');
8+
parserOpts.plugins.push('dynamicImport');
9+
parserOpts.plugins.push('flow');
10+
parserOpts.plugins.push('functionBind');
11+
parserOpts.plugins.push('functionSent');
12+
parserOpts.plugins.push('jsx');
13+
parserOpts.plugins.push('objectRestSpread');
14+
parserOpts.plugins.push('exportDefaultFrom');
15+
parserOpts.plugins.push('exportNamespaceFrom');
16+
};

0 commit comments

Comments
 (0)