Class: Incr::Command::Npm

Inherits:
Object
  • Object
show all
Defined in:
lib/incr/command/npm.rb

Instance Method Summary collapse

Constructor Details

#initialize(args, global_options) ⇒ Npm

Returns a new instance of Npm.



8
9
10
11
12
13
14
15
16
# File 'lib/incr/command/npm.rb', line 8

def initialize(args, global_options)
  @segment = args[0]

  @package_json_filename = File.join('.', global_options[:versionFileDirectory], 'package.json')
  @package_json_lock_filename = File.join('.', global_options[:versionFileDirectory], 'package-lock.json')
  @tag_pattern = global_options[:tagNamePattern]
  @commit = global_options[:commit]
  @tag = global_options[:tag]
end

Instance Method Details

#executeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/incr/command/npm.rb', line 18

def execute
  package_json = parse_content(@package_json_filename)
  if package_json == nil
    return
  end

  file_version = package_json['version']
  old_version = SemVersion.new(file_version)
  new_version = Incr::Service::Version.increment_segment(old_version, @segment)

  Incr::Service::FileHelper.replace_once(@package_json_filename, version_pattern(old_version.to_s), version_pattern(new_version.to_s))
  Incr::Service::FileHelper.replace_once(@package_json_lock_filename, version_pattern(old_version.to_s), version_pattern(new_version.to_s))

  new_tag = @tag_pattern % new_version.to_s

  puts new_tag

  repository = Incr::Service::Repository.new('.')
  repository.add(@package_json_filename)
  repository.add(@package_json_lock_filename)
  repository.commit(new_tag) if @commit
  repository.tag(new_tag) if @tag
end