-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scripts): allow transforms to be run as npm scripts
- Loading branch information
1 parent
7dc1164
commit b1d6f1c
Showing
3 changed files
with
102 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,4 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
package-lock.json | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,96 @@ | ||
# codemods | ||
|
||
This repository contains a collection of codemod scripts for use with | ||
[JSCodeshift](https://github.com/facebook/jscodeshift). | ||
A collection of transforms for use with | ||
[facebook/jscodeshift](https://github.com/facebook/jscodeshift). | ||
|
||
### Setup & Run | ||
## Installation | ||
|
||
```sh | ||
npm install -g jscodeshift | ||
git clone https://github.com/JamieMason/codemods.git | ||
jscodeshift -t <codemod-script> <file> | ||
cd codemods | ||
yarn install | ||
``` | ||
|
||
Use the `-d` option for a dry-run and use `-p` to print the output for | ||
comparison. | ||
## Usage | ||
|
||
### Included Scripts | ||
``` | ||
# yarn | ||
yarn name-of-the-transform <path-to-file> | ||
# npm | ||
npm run name-of-the-transform -- <path-to-file> | ||
#### `sort-jsx-props` | ||
# jscodeshift | ||
jscodeshift -t ./transforms/name-of-the-transform.js <path-to-file> | ||
``` | ||
|
||
Sort props of JSX `<Component />` in A-Z order. | ||
## Transforms | ||
|
||
```sh | ||
jscodeshift -t codemods/transforms/sort-jsx-props.js <file> | ||
### import-from-root | ||
|
||
Rewrite deep imports to import from a packages' root index. | ||
|
||
> Set the Environment Variable `IMPORT_FROM_ROOT` to apply this transform only to packages whose | ||
> name starts with that string: `IMPORT_FROM_ROOT=some-package yarn import-from-root <path-to-file>` | ||
```js | ||
/* INPUT */ | ||
import { foo } from 'some-package/foo/bar/baz'; | ||
|
||
/* OUTPUT */ | ||
import { foo } from 'some-package'; | ||
``` | ||
|
||
#### `sort-object-props` | ||
### sort-jsx-props | ||
|
||
Sort members of Object Literals in A-Z order. | ||
Sort props of JSX Components alphabetically. | ||
|
||
```sh | ||
jscodeshift -t codemods/transforms/sort-object-props.js <file> | ||
```jsx | ||
/* INPUT */ | ||
<Music zootWoman={true} rickJames={true} zapp={true} /> | ||
|
||
/* OUTPUT */ | ||
<Music rickJames={true} zapp={true} zootWoman={true} /> | ||
``` | ||
|
||
### sort-object-props | ||
|
||
Sort members of Object Literals alphabetically. | ||
|
||
```js | ||
/* INPUT */ | ||
const players = { messi: true, bergkamp: true, ginola: true }; | ||
|
||
/* OUTPUT */ | ||
const players = { bergkamp: true, ginola: true, messi: true }; | ||
``` | ||
|
||
### use-named-exports | ||
|
||
Naively convert a default export to a named export using the name of the file, which may clash with | ||
other variable names in the file. This codemod would need following up on with ESLint and some | ||
manual fixes. | ||
|
||
```js | ||
/* INPUT */ | ||
// ~/Dev/repo/src/apps/health/server.js | ||
export default mount('/health', app); | ||
|
||
/* OUTPUT */ | ||
// ~/Dev/repo/src/apps/health/server.js | ||
export const server = mount('/health', app); | ||
``` | ||
|
||
### use-named-imports | ||
|
||
Naively convert a default import to a named import using the original name, which may not match what | ||
the module is actually exporting. This codemod would need following up on with ESLint and some | ||
manual fixes. | ||
|
||
```js | ||
/* INPUT */ | ||
import masthead from '@sky-uk/koa-masthead'; | ||
|
||
/* OUTPUT */ | ||
import { masthead } from '@sky-uk/koa-masthead'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "codemods", | ||
"description": "Transforms for use with JSCodeshift", | ||
"version": "0.0.0", | ||
"author": "Jamie Mason <jamie@foldleft.io> (https://github.com/JamieMason)", | ||
"bugs": "https://github.com/JamieMason/codemods/issues", | ||
"dependencies": { | ||
"jscodeshift": "0.5.1" | ||
}, | ||
"homepage": "https://github.com/JamieMason/codemods#readme", | ||
"license": "MIT", | ||
"private": true, | ||
"repository": "JamieMason/codemods", | ||
"scripts": { | ||
"import-from-root": "jscodeshift --extensions js,jsx -t ./transforms/import-from-root.js", | ||
"sort-jsx-props": "jscodeshift --extensions js,jsx -t ./transforms/sort-jsx-props.js", | ||
"sort-object-props": "jscodeshift --extensions js,jsx -t ./transforms/sort-object-props.js", | ||
"use-named-exports": "jscodeshift --extensions js,jsx -t ./transforms/use-named-exports.js", | ||
"use-named-imports": "jscodeshift --extensions js,jsx -t ./transforms/use-named-imports.js" | ||
} | ||
} |