diff --git a/.gitignore b/.gitignore index e920c16..e9486c7 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index d9f5480..4d68825 100644 --- a/README.md +++ b/README.md @@ -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 +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 + +# npm +npm run name-of-the-transform -- -#### `sort-jsx-props` +# jscodeshift +jscodeshift -t ./transforms/name-of-the-transform.js +``` -Sort props of JSX `` in A-Z order. +## Transforms -```sh -jscodeshift -t codemods/transforms/sort-jsx-props.js +### 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 ` + +```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 +```jsx +/* INPUT */ + + +/* OUTPUT */ + +``` + +### 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'; ``` diff --git a/package.json b/package.json new file mode 100644 index 0000000..f652185 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "codemods", + "description": "Transforms for use with JSCodeshift", + "version": "0.0.0", + "author": "Jamie Mason (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" + } +}