Skip to content

Commit 17069be

Browse files
committed
📚 API documentation and website config
1 parent 473473e commit 17069be

File tree

6 files changed

+341
-4
lines changed

6 files changed

+341
-4
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module.exports = {
44
},
55
moduleFileExtensions: ['ts', 'js'],
66
testRegex: '^.+\\.(spec|test)\\.(ts|js)$',
7-
testPathIgnorePatterns: ['/node_modules/', 'lib'],
87
snapshotSerializers: ['jest-serializer-html-string'],
98
watchPlugins: [
109
'jest-watch-typeahead/filename',
@@ -20,6 +19,7 @@ module.exports = {
2019
},
2120
testPathIgnorePatterns: [
2221
'/node_modules/',
22+
'/plugin_packages/',
2323
'<rootDir>/packages/initializer/template/',
2424
],
2525
};

website/docs/api/codeshift-cli.mdx

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
id: cli
3+
title: codeshift/cli
4+
slug: /cli
5+
---
6+
7+
To download and run codemods, we provide a CLI tool called `@codeshift/cli`.
8+
9+
`@codeshift/cli` is responsible for running the provided transform against your entire codebase.
10+
Under the hood, it is a wrapper of jscodeshift's cli, which provides additional functionality
11+
12+
1. Ability to run community codemods hosted on npm
13+
2. Runs versioned codemods in sequence
14+
3. Always runs the latest version of a codemod
15+
16+
The cli allows you to run transforms either from the [community folder](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community) or on your local machine as per the original implementation of jscodeshift
17+
18+
**Note:** Codemods will be designed to do the heavy lifting, but they'll often not be perfect so some manual work may still be required in order to successfully migrate.
19+
20+
## Usage/Installation
21+
22+
We recommend running the CLI with `npx` to ensure you always have the latest version.
23+
24+
`$ npx @codeshift/cli --packages mylib@1.0.0 /project/src`
25+
26+
But it can also be installed normally:
27+
28+
`npm install -g @codeshift/cli` or `yarn global add @codeshift/cli`
29+
30+
Or globally:
31+
32+
`npm install --save-dev @codeshift/cli` or `yarn add -D @codeshift/cli`
33+
34+
## Options
35+
36+
### --transform, -t
37+
38+
The transform to run, transforms can be either a single file or directory with an index.
39+
40+
**example:**
41+
42+
- `npx @codeshift/cli --transform codemods/my-special-mod /project/src/file.js`
43+
- `npx @codeshift/cli --transform codemods/my-special-mod/index.ts /project/src/file.js`
44+
45+
### --packages
46+
47+
Runs transforms for the specified comma separated list of packages, optionally include a version for each package to run all transforms since that version
48+
49+
**example:**
50+
51+
- `npx @codeshift/cli --packages @atlaskit/button /project/src`
52+
- `npx @codeshift/cli --packages @atlaskit/button@3.0.0,@atlaskit/range@4.0.0 /project/src`
53+
54+
### --parser, -p
55+
56+
Parser to use for parsing the source files you are code modding.
57+
58+
**options:**
59+
60+
- babel (default)
61+
- babylon
62+
- flow
63+
- ts
64+
- tsx
65+
66+
**example:**
67+
68+
- `npx @codeshift/cli --parser tsx /project/src/file.ts`
69+
- `npx @codeshift/cli -p babel /project/src/file.ts`
70+
71+
### --extensions, -e
72+
73+
Transform files with these file extensions (comma separated list) (default: js)
74+
75+
**example:**
76+
77+
- `npx @codeshift/cli --extensions ts,tsx /project/src/file.js`
78+
- `npx @codeshift/cli -e js /project/src/file.js`
79+
80+
### --ignore-pattern
81+
82+
Ignore files that match a provided glob expression
83+
84+
**example:**
85+
86+
- `@codeshift/cli --ignore-pattern node_modules /project/src/file.js`
87+
88+
### --version, -v
89+
90+
Get current version number
91+
92+
**example:**
93+
94+
- `@codeshift/cli --version`
95+
- `@codeshift/cli -v`
96+
97+
### --help
98+
99+
Print all help text to the command line
100+
101+
**example:**
102+
103+
- `@codeshift/cli --help`
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
id: test-utils
3+
title: codeshift/test-utils
4+
slug: /test-utils
5+
---
6+
7+
CodeshiftCommunity provides a set of test utilities to help unit test codemods.
8+
9+
## Installation
10+
11+
`@codeshift/test-utils` is pre-bundled with every codemod that is published to the [community folder](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community),
12+
so there's no need to install it manually.
13+
14+
However, it is also available for use outside of this project and compatible with jscodeshift.
15+
16+
`npm install --save-dev @codeshift/test-utils` or `yarn add -D @codeshift/test-utils`
17+
18+
## API
19+
20+
### `applyTransform(transform, input, options = { parser: 'babel' })`
21+
22+
Runs a transform against the provided code and returns the resulting file.
23+
24+
We provide this method as opposed to [jscodeshift's test utils](https://github.com/facebook/jscodeshift#unit-testing) to maintain jest's skip/only and snapshot features
25+
26+
**Returns**
27+
28+
`string`: Resulting file after transform has been applied
29+
30+
**Example**
31+
32+
```jsx
33+
import * as transformer from '../transform';
34+
import { applyTransform } from '@codeshift/test-utils';
35+
36+
it('should wrap avatar in a tooltip if name is defined', () => {
37+
const result = applyTransform(
38+
transformer,
39+
`
40+
import Avatar from 'avatar';
41+
42+
const App = () => {
43+
return <Avatar name="foo" />;
44+
}
45+
`,
46+
{ parser: 'tsx' },
47+
);
48+
49+
expect(result).toMatchInlineSnapshot(`
50+
"import Tooltip from 'tooltip';
51+
import Avatar from 'avatar';
52+
53+
const App = () => {
54+
return <Tooltip content=\\"foo\\"><Avatar name=\\"foo\\" /></Tooltip>;
55+
}"
56+
`);
57+
```
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
---
2+
id: utils
3+
title: codeshift/utils
4+
slug: /utils
5+
---
6+
7+
CodeshiftCommunity provides a set of utilities to help perform common codemod operations.
8+
9+
## Installation
10+
11+
`@codeshift/utils` is pre-bundled with every codemod that is published to the [community folder](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community),
12+
so there's no need to install it manually.
13+
14+
However, it is also available for use outside of this project and compatible with jscodeshift.
15+
16+
`npm install --save-dev @codeshift/utils` or `yarn add -D @codeshift/utils`
17+
18+
## Imports
19+
20+
### `hasImportDeclaration(j, source, importPath)`
21+
22+
Finds an import declaration by source name
23+
24+
**Returns**
25+
26+
`boolean`
27+
28+
**Example**
29+
30+
```jsx
31+
// src/App.js
32+
import React from 'react';
33+
```
34+
35+
```js
36+
import { hasImportDeclaration } from '@codeshift/utils';
37+
38+
hasImportDeclaration(j, source, 'react'); // True
39+
```
40+
41+
### `getImportDeclaration(j, source, importPath)`
42+
43+
Returns an import declaration by source name
44+
45+
**Returns**
46+
47+
`Collection`: Collection containing 1 or more imports
48+
49+
**Example**
50+
51+
```jsx
52+
// src/App.js
53+
import React from 'react';
54+
```
55+
56+
```js
57+
import { getImportDeclaration } from '@codeshift/utils';
58+
59+
getImportDeclaration(j, source, 'react');
60+
```
61+
62+
### `getDefaultImportSpecifier(j, source, specifier)`
63+
64+
Finds a default import specifier by name
65+
66+
**Returns**
67+
68+
`Collection`: Collection containing all matched default import specifiers
69+
70+
**Example**
71+
72+
```jsx
73+
// src/App.js
74+
import React from 'react';
75+
```
76+
77+
```js
78+
import { getDefaultImportSpecifier } from '@codeshift/utils';
79+
80+
getDefaultImportSpecifier(j, source, 'React'); // Collection containing 'React'
81+
```
82+
83+
### `getImportSpecifier(j, source, specifier)`
84+
85+
Finds an import specifier by name
86+
87+
**Returns**
88+
89+
`Collection`: Collection containing all matched import specifiers
90+
91+
**Example**
92+
93+
```jsx
94+
// src/App.js
95+
import React, { useEffect } from 'react';
96+
```
97+
98+
```js
99+
import { getImportSpecifier } from '@codeshift/utils';
100+
101+
getImportSpecifier(j, source, 'useEffect'); // Collection containing 'useEffect'
102+
```
103+
104+
## JSX
105+
106+
### `getJSXAttributesByName(j, source, attributeName)`
107+
108+
Finds a JSX attributeName (aka prop) by name
109+
110+
**Returns**
111+
112+
`Collection`: Collection containing all matched jsx attributes
113+
114+
**Example**
115+
116+
```jsx
117+
// src/App.js
118+
import React from 'react';
119+
120+
const App = () => <Button primary>Say hello</Button>;
121+
```
122+
123+
```js
124+
import { getJSXAttributesByName } from '@codeshift/utils';
125+
126+
getJSXAttributesByName(j, source, 'primary'); // Collection containing 'primary'
127+
```
128+
129+
## Comments
130+
131+
### `addCommentBefore(j, source, message)`
132+
133+
Appends a comment before the provided node
134+
135+
**Returns**
136+
137+
`void`
138+
139+
**Example**
140+
141+
```jsx
142+
// src/App.js
143+
import React from 'react';
144+
145+
const App = () => <Button primary>Say hello</Button>;
146+
```
147+
148+
```js
149+
import { addCommentBefore } from '@codeshift/utils';
150+
151+
addCommentBefore(
152+
j,
153+
path.find(j.ImportDeclaration),
154+
'This should be removed in favour of mylib',
155+
);
156+
```
157+
158+
```js
159+
// src/App.js
160+
// TODO: (Codemod) This should be removed in favour of mylib
161+
import React from 'react';
162+
163+
const App = () => <Button primary>Say hello</Button>;
164+
```

website/docusaurus.config.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ module.exports = {
1818
},
1919
items: [
2020
{
21-
to: 'docs/',
22-
activeBasePath: 'docs',
23-
docid: 'introduction',
21+
type: 'doc',
22+
docId: 'introduction',
2423
label: 'Docs',
2524
position: 'left',
2625
},
26+
{
27+
type: 'doc',
28+
docId: 'api/cli',
29+
label: 'API',
30+
position: 'left',
31+
},
2732
{
2833
href: 'https://github.com/CodeshiftCommunity/CodeshiftCommunity',
2934
label: 'GitHub',

website/sidebars.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ module.exports = {
3636
items: ['recipes/import-manipulation', 'recipes/react'],
3737
},
3838
],
39+
api: [
40+
{
41+
type: 'category',
42+
label: 'Packages',
43+
collapsed: false,
44+
items: ['api/cli', 'api/utils', 'api/test-utils'],
45+
},
46+
],
3947
};

0 commit comments

Comments
 (0)