Class: JsxRosetta::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/jsx_rosetta/cli.rb

Overview

Command-line interface for the gem.

Subcommands:

install                    npm-install the Node sidecar dependencies.
translate FILE [-o DIR]    JSX/TSX → ViewComponent files written to DIR
                           (default: current directory). TSX is detected
                           via the .tsx extension or --tsx.
parse FILE                 Print the parsed Babel AST as pretty JSON.
pages-routes DIR [-o PATH] Walk a Next.js `pages/` directory and emit a
                           Rails config/routes.rb skeleton.
version                    Print the gem version.
help                       Show usage.

Constant Summary collapse

EXIT_OK =
0
EXIT_USAGE =
64
EXIT_FAILURE =
1
USAGE_TEXT =
<<~USAGE
  Usage: jsx_rosetta <command> [args]

  Commands:
    install                    Install the gem's Node sidecar dependencies (runs `npm install`).
    translate FILE [-o DIR]    Translate JSX/TSX into ViewComponent files in DIR (default: ".").
                               Pass --tsx to force TypeScript parsing if the input is .jsx.
                               Pass --as=view to emit a Rails view template (`<snake>.html.erb`)
                               instead of a ViewComponent class + sidecar template — appropriate
                               for pages tied to a route.
                               Pass --as=phlex to emit a single-file Phlex 2.x view class
                               (`<snake>.rb`) instead of a ViewComponent. Configure the class
                               name with --phlex-suffix=Component or --phlex-namespace=Components
                               (mutually exclusive; default is bare class name).
                               Pass --rails-routes DIR (with --as=phlex) to place the output
                               at <controller>/<action>.rb with class
                               Views::<Controller>::<Action> < Views::Base, derived from a
                               route table scanned out of DIR (a Next.js pages directory).
    routes FILE [-o OUT.rb]    Parse <Route path=... element={<X/>} /> patterns from FILE
                               and emit a reviewable Ruby script that calls `rails generate
                               controller` and prints suggested config/routes.rb additions.
    pages-routes DIR [-o PATH] Walk a Next.js `pages/` directory tree and emit a
                               Rails config/routes.rb skeleton derived from the file
                               layout. Use --ext .tsx,.jsx,.ts,.js to override the
                               default `.tsx,.jsx` filter, and --allow-any-dir to
                               skip the `basename == 'pages'` safety check.
                               Pass --controllers DIR to also emit one
                               `<controller>_controller.rb` per controller in DIR
                               (existing files are not overwritten).
    parse FILE                 Parse the input and print the Babel AST as JSON.
    version                    Print the gem version.
    help                       Show this help.

  Environment:
    JSX_ROSETTA_NODE           Absolute path to a node executable (default: PATH lookup).
USAGE

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV.dup, stdout: $stdout, stderr: $stderr) ⇒ CLI

Returns a new instance of CLI.



65
66
67
68
69
# File 'lib/jsx_rosetta/cli.rb', line 65

def initialize(argv = ARGV.dup, stdout: $stdout, stderr: $stderr)
  @argv = argv
  @stdout = stdout
  @stderr = stderr
end

Instance Method Details

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jsx_rosetta/cli.rb', line 71

def run
  command = @argv.shift
  case command
  when "install" then run_install
  when "translate" then run_translate
  when "routes" then run_routes
  when "pages-routes" then run_pages_routes
  when "parse" then run_parse
  when "version", "-v", "--version" then run_version
  when nil, "help", "-h", "--help" then print_help(EXIT_OK)
  else
    @stderr.puts "jsx_rosetta: unknown command: #{command.inspect}"
    print_help(EXIT_USAGE)
  end
end