Skip to content

Commit 9d4f71f

Browse files
committed
fix: minor runner fix, and document CLI
1 parent e3559de commit 9d4f71f

File tree

3 files changed

+120
-6
lines changed

3 files changed

+120
-6
lines changed

README.md

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ structural search and replace for JavaScript and TypeScript, using jscodeshift
2525
- [`constructor(jscodeshift: JSCodeshift, root: Collection)`](#constructorjscodeshift-jscodeshift-root-collection)
2626
- [`.on(root: Collection)`](#onroot-collection)
2727
- [`.find()`](#find)
28+
- [`FindOptions`](#findoptions)
29+
- [`FindOptions.where` (`{ [captureName: string]: (path: ASTPath<any>) => boolean }`)](#findoptionswhere--capturename-string-path-astpathany--boolean-)
2830
- [`.find().replace()`](#findreplace)
2931
- [`.findStatements()`](#findstatements)
3032
- [`.findStatements().replace()`](#findstatementsreplace)
@@ -42,6 +44,12 @@ structural search and replace for JavaScript and TypeScript, using jscodeshift
4244
- [String Matching](#string-matching)
4345
- [Support Table](#support-table)
4446
- [| Backreferences](#-backreferences)
47+
- [Transform files](#transform-files)
48+
- [`exports.find` (optional)](#exportsfind-optional)
49+
- [`exports.where` (optional)](#exportswhere-optional)
50+
- [`exports.replace` (optional)](#exportsreplace-optional)
51+
- [`exports.astx` (optional)](#exportsastx-optional)
52+
- [CLI](#cli)
4553

4654
<!-- tocstop -->
4755

@@ -66,10 +74,8 @@ linebreaks well unless you work really hard at the regex.
6674

6775
Now there's a better option...you can refactor with confidence using `astx`!
6876

69-
```js
70-
// astx.js
71-
exports.find = `rmdir($path, $force)`
72-
exports.replace = `rmdir($path, { force: $force })`
77+
```sh
78+
astx -f 'rmdir($path, $force)' -r 'rmdir($path, { force: $force })' src
7379
```
7480

7581
What's going on here? Find and replace must be valid JS expressions or statements. `astx` parses them
@@ -215,6 +221,16 @@ You can interpolate AST nodes in the tagged template literal; it uses `jscodeshi
215221

216222
For example you could do `` astx.find`${j.identifier('foo')} + 3`() ``
217223

224+
### `FindOptions`
225+
226+
An object with the following optional properties:
227+
228+
#### `FindOptions.where` (`{ [captureName: string]: (path: ASTPath<any>) => boolean }`)
229+
230+
Where conditions for node captures. For example if your find pattern is `$a()`, you could have
231+
`{ where: { $a: path => /foo|bar/.test(path.node.name) } }`, which would only match zero-argument calls
232+
to `foo` or `bar`.
233+
218234
### `.find().replace()`
219235

220236
Finds and replaces matches for the given pattern within `root`.
@@ -420,3 +436,80 @@ foo(1, 1, { foo: 1 }, { foo: 1 }) // match
420436
foo(1, 2, { foo: 1 }, { foo: 1 }) // no match
421437
foo(1, 1, { foo: 1 }, { bar: 1 }) // no match
422438
```
439+
440+
# Transform files
441+
442+
Like `jscodeshift`, you can put code to perform a transform in a `.js` file (defaults to `astx.js` in the working directory, unless you use the `-t` CLI option).
443+
444+
The transform file API is a bit different from `jscodeshift` though. You can have the following exports:
445+
446+
## `exports.find` (optional)
447+
448+
A code string or AST node of the pattern to find in the files being transformed.
449+
450+
## `exports.where` (optional)
451+
452+
Where conditions for capture variables in `exports.find`.
453+
See [`FindOptions.where` (`{ [captureName: string]: (path: ASTPath<any>) => boolean }`)](#findoptionswhere--capturename-string-path-astpathany--boolean-) for more information.
454+
455+
## `exports.replace` (optional)
456+
457+
A code string, AST node, or replace function to replace matches of `exports.find` with.
458+
459+
The function arguments are the same as described in [`.find().replace()`](#findreplace) or
460+
[`.findStatements().replace()`](#findstatementsreplace), depending on whether `exports.find`
461+
is multiple statements or not.
462+
463+
## `exports.astx` (optional)
464+
465+
A function to perform an arbitrary transform using the `Astx` API. It gets called with an object with the following properties:
466+
467+
- `source` (`string`) - The source code of the file being transformed
468+
- `path` (`string`) - The path to the file being transformed
469+
- `root` (`Collection`) - the JSCodeshift Collection wrapping the parsed AST
470+
- `astx` (`Astx`) - the `Astx` API instance
471+
- `jscodeshift` (`JSCodeshift`) - the JSCodeshift instance
472+
- `j` (`JSCodeshift`) - shorthand for the same JSCodeshift instance
473+
- `expression` - tagged template literal for parsing code as an expression, like `jscodeshift.template.expression`
474+
- `statement` - tagged template literal for parsing code as a statement, like `jscodeshift.template.statement`
475+
- `statements` - tagged template literal for parsing code as an array of statements, like `jscodeshift.template.statements`
476+
- `report` (`(message: any) => void`)
477+
478+
# CLI
479+
480+
Astx includes a CLI for performing transforms. The CLI will process the given files, then print out a diff of what will be
481+
changed, and prompt you to confirm you want to write the changes.
482+
483+
It will parse with babel by default using the version installed in your project and your project's babel config, if any.
484+
You can pass other parsers with the `--parser` option, just like `jscodeshift`.
485+
486+
```
487+
Usage:
488+
489+
astx -f <code> -r <code> [<files...>] [<directories...>]
490+
491+
Quick search and replace in the given files and directories
492+
(make sure to quote code)
493+
494+
Example:
495+
496+
astx -f 'rmdir($path, $force)' -r 'rmdir($path, { force: $force })' src
497+
498+
astx -t <transformFile> [<files ...>] [<directories ...>]
499+
500+
Applies a transform file to the given files and directories
501+
502+
astx [<files ...>] [<directories ...>]
503+
504+
Applies the default transform file (astx.js in working directory)
505+
to the given files and directories
506+
507+
508+
Options:
509+
--help Show help [boolean]
510+
--version Show version number [boolean]
511+
-t, --transform path to the transform file. Can be either a local path or url
512+
--parser parser to use [string]
513+
-f, --find search pattern [string]
514+
-r, --replace replace pattern [string]
515+
```

src/cli/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,26 @@ const argv = yargs
3030
alias: 'r',
3131
describe: 'replace pattern',
3232
type: 'string',
33-
}).argv
33+
}).usage(`Usage:
34+
35+
$0 -f <code> -r <code> [<files...>] [<directories...>]
36+
37+
Quick search and replace in the given files and directories
38+
(make sure to quote code)
39+
40+
Example:
41+
42+
astx -f 'rmdir($path, $force)' -r 'rmdir($path, { force: $force })' src
43+
44+
$0 -t <transformFile> [<files ...>] [<directories ...>]
45+
46+
Applies a transform file to the given files and directories
47+
48+
$0 [<files ...>] [<directories ...>]
49+
50+
Applies the default transform file (astx.js in working directory)
51+
to the given files and directories
52+
`).argv
3453

3554
const paths = argv._.filter((x) => typeof x === 'string') as string[]
3655
if (!paths.length) {

src/runTransformOnFile.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ export const runTransformOnFile = (transform: Transform) => async (
9999
path: file,
100100
j,
101101
jscodeshift: j,
102-
report: (msg: any) => reports.push(msg),
102+
report: (msg: any) => {
103+
reports.push(msg)
104+
},
103105
...template,
104106
root,
105107
astx: new Astx(j, root),

0 commit comments

Comments
 (0)