Class: Incr::Command::Npm

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

Constant Summary collapse

VERSION_PATTERN =

pattern for any semver version, including pre-release label (ie. 1.0.0-alpha)

"[\\w\\.\\-]*"
LOOKBEHIND_PATTERNS =

pattern preceding the version in package.json and package-lock.json (v1 and v2)

[
  "\\A{[^{}]*\"version\": \"\\K",
  "\"\": {[^{}]*\"version\": \"\\K"
]

Instance Method Summary collapse

Constructor Details

#initialize(args, global_options) ⇒ Npm

Returns a new instance of Npm.



16
17
18
19
20
21
22
23
24
25
# File 'lib/incr/command/npm.rb', line 16

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

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

Instance Method Details

#executeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/incr/command/npm.rb', line 27

def execute
  if !File.exist?(@package_json_filename)
    warn("'#{@package_json_filename}': file not found.")
    return
  end

  if !File.exist?(@package_json_lock_filename)
    warn("'#{@package_json_lock_filename}': file not found.")
    return
  end

  package_json = JSON.parse(IO.read(@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)

  replace_file_version(@package_json_filename, new_version.to_s)
  replace_file_version(@package_json_lock_filename, new_version.to_s)

  new_tag = @tag_pattern % new_version.to_s

  puts new_tag

  if not @noop
    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
end