Class: Spud::BuildTools::PackageJSON::Task

Inherits:
Task
  • Object
show all
Defined in:
lib/spud/build_tools/package.json/task.rb

Instance Attribute Summary

Attributes inherited from Task

#filename, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Task

#args

Constructor Details

#initialize(name:, filename:, opening_commands:) ⇒ Task

Returns a new instance of Task.



36
37
38
39
# File 'lib/spud/build_tools/package.json/task.rb', line 36

def initialize(name:, filename:, opening_commands:)
  super(name: name, filename: filename)
  @opening_commands = opening_commands
end

Class Method Details

.mount!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spud/build_tools/package.json/task.rb', line 8

def self.mount!
  return unless File.exist?('package.json')

  opening_commands = %w[npm run]
  if File.exist?('package.lock')
    if `command -v npm`.empty?
      puts 'package.json detected, but no installation of `npm` exists. Skipping npm...'
      return
    end
  elsif File.exist?('yarn.lock')
    if `command -v yarn`.empty?
      puts 'package.json detected, but no installation of `yarn` exists. Skipping yarn...'
      return
    else
      opening_commands = %w[yarn run]
    end
  end

  source = File.read('package.json')
  json = JSON.parse(source)
  scripts = json['scripts']
  return unless scripts

  scripts.keys.each do |name|
    new(name: name, filename: 'package.json', opening_commands: opening_commands)
  end
end

Instance Method Details

#invokeObject



41
42
43
# File 'lib/spud/build_tools/package.json/task.rb', line 41

def invoke(*)
  system(*(@opening_commands + [name]))
end