Module: Prettier

Defined in:
lib/prettier.rb,
lib/prettier/rake/task.rb

Defined Under Namespace

Modules: Rake

Constant Summary collapse

PLUGIN =
-File.expand_path('..', __dir__)
BINARY =
-File.join(PLUGIN, 'node_modules', 'prettier', 'bin-prettier.js')
VERSION =
-JSON.parse(File.read(File.join(PLUGIN, 'package.json')))['version']

Class Method Summary collapse

Class Method Details

.run(args) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prettier.rb', line 11

def self.run(args)
  quoted = args.map { |arg| arg.start_with?('-') ? arg : "\"#{arg}\"" }
  command = "node #{BINARY} --plugin \"#{PLUGIN}\" #{quoted.join(' ')}"

  stdout, stderr, status = Open3.capture3({ 'RBPRETTIER' => '1' }, command)
  STDOUT.puts(stdout)

  # If we completed successfully, then just exit out.
  exitstatus = status.exitstatus
  return exitstatus if exitstatus == 0

  if stderr.match?(%r{Cannot find module '/.+?/bin-prettier.js'})
    # If we're missing bin-prettier.js, then it's possible the user installed
    # the gem through git, which wouldn't have installed the requisite
    # JavaScript files.
    STDERR.puts(<<~MSG)
      Could not find the JavaScript files necessary to run prettier.

      If you installed this dependency through git instead of from rubygems,
      it does not install the necessary files by default. To fix this you can
      either install them yourself by cd-ing into the directory where this gem
      is located (#{File.expand_path('..', __dir__)}) and running:
        
        `yarn && yarn prepublishOnly`
         or
         `npm install && npm run prepublishOnly`
         or
         you can change the source in your Gemfile to point directly to rubygems.
    MSG
  else
    # Otherwise, just print out the same error that prettier emitted, as it's
    # unknown to us.
    STDERR.puts(stderr)
  end

  # Make sure we still exit with the same status code the prettier emitted.
  exitstatus
end