Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add descriptions for dry-run and print #4

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-cups-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/codemod": patch
---

add descriptions for dry-run and print
28 changes: 21 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,53 @@ import * as NodeContext from "@effect/platform-node/NodeContext"
import { runMain } from "@effect/platform-node/Runtime"
import * as Console from "effect/Console"
import * as Effect from "effect/Effect"
import * as ReadonlyArray from "effect/ReadonlyArray"
import * as jscodeshift from "jscodeshift/src/Runner"
import assert from "node:assert"
import * as Fs from "node:fs"
import * as Path from "node:path"

const codemod = Args.choice<string>(
Fs.readdirSync(`${__dirname}/codemods`).map(file => {
const name = Path.basename(file, ".ts")
return [name, name] as const
}) as any,
const codemodFiles = Fs.readdirSync(`${__dirname}/codemods`)
assert(
ReadonlyArray.isNonEmptyReadonlyArray(codemodFiles),
"Could not find any code mod files",
)
const codemod = Args.choice(
ReadonlyArray.map(
codemodFiles,
file => [
Path.basename(file, ".ts"),
Path.join(`${__dirname}/codemods`, file),
],
),
{ name: "codemod" },
).pipe(
Args.map(codemod => `${__dirname}/codemods/${codemod}.ts`),
Args.withDescription("The code modification to run"),
)

const run = Command.make("codemod", {
codemod,
paths: Args.text({ name: "paths" }).pipe(
Args.repeated,
Args.map(ReadonlyArray.fromIterable), // TODO: Remove when `Args.repeated` returns Array
Args.withDescription("The paths to run the codemod on"),
),
options: {
dry: Options.boolean("dry-run").pipe(
Options.withAlias("d"),
Options.withDescription("If set, the codemod will not modify any files"),
),
print: Options.boolean("print").pipe(
Options.withAlias("p"),
Options.withDescription(
"If set, the codemod will print the changes to the console",
),
),
},
}).pipe(
Command.withHandler(({ codemod, options, paths }) =>
Effect.promise(() =>
jscodeshift.run(codemod, paths as Array<string>, {
jscodeshift.run(codemod, paths, {
...options,
babel: true,
parser: "ts",
Expand Down
Loading