runInteractiveCodemod function
Interactively runs a "codemod" by using stdout
to display a diff for each
potential patch and stdin
to accept input from the user on what to do with
said patch; returns an appropriate exit code when complete.
suggestor
will generate patches for each file in files
. Each patch will
be shown to the user to be accepted or skipped.
If defaultYes
is true, then the default option for each patch prompt will
be yes (meaning that just hitting "enter" will accept the patch).
Otherwise, the default action is no (meaning that just hitting "enter" will
skip the patch).
Additional CLI args are accepted via args
to make it easy to configure
certain options at runtime:
-h, --help Prints this help output.
-v, --verbose Outputs all logging to stdout/stderr.
--yes-to-all Forces all patches accepted without prompting the user. Useful for scripts.
--stderr-assume-tty Forces ansi color highlighting of stderr. Useful for debugging.
To run a codemod from the command line, setup a .dart
file with a main
block like so:
import 'dart:io';
import 'package:codemod/codemod.dart';
void main(List<String> args) {
exitCode = runInteractiveCodemod(
[...], // input files,
ExampleSuggestor(),
args: args,
);
}
For debugging purposes, logs will be written to stderr. By default, only
severe logs are reported. If verbose mode is enabled, all logs will be
reported. It's recommended that if you need to see these logs while running
a codemod for debugging purposes that you redirect stderr to a file and
monitor it using tail -f
, otherwise the logs may be overwritten and lost
every time this function clears the terminal to render a patch diff.
$ touch stderr.txt && tail -f stderr.txt
$ dart example_codemod.dart --verbose 2>stderr.txt
Additionally, you can retain ansi color highlighting of these logs when
redirecting to a file by passing the --stderr-assume-tty
flag:
$ dart example_codemod.dart --verbose --stderr-assume-tty 2>stderr.txt
Implementation
Future<int> runInteractiveCodemod(
Iterable<String> filePaths,
Suggestor suggestor, {
Iterable<String> args = const [],
bool defaultYes = false,
String? additionalHelpOutput,
String? changesRequiredOutput,
}) =>
runInteractiveCodemodSequence(
filePaths,
[suggestor],
args: args,
defaultYes: defaultYes,
additionalHelpOutput: additionalHelpOutput,
changesRequiredOutput: changesRequiredOutput,
);