25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

pre-commit 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env ruby
  2. require 'json'
  3. def puts_c(color, str)
  4. puts "\x1b[#{color}m#{str}\x1b[0m"
  5. end
  6. class Source
  7. attr_accessor :path, :v_regex
  8. def initialize(path, v_regex)
  9. @path = path
  10. @v_regex = v_regex
  11. end
  12. end
  13. class Bumper
  14. attr_accessor :sources, :bumped, :target_v
  15. def initialize(sources)
  16. @sources = sources
  17. end
  18. def start
  19. # get package.json version
  20. package = JSON.parse File.read 'package.json'
  21. @target_v = package['version']
  22. @bumped = false
  23. @sources.each {|source| bump_source(source)}
  24. # if bumped, do extra stuff and notify the user
  25. if @bumped
  26. # keep .mjs & .js|.min.js in sync
  27. puts "> building and minifying `mustache.mjs`..."
  28. `npm run build`
  29. # stage files for commit
  30. `git add package.json`
  31. @sources.each {|source| `git add #{source.path}`}
  32. `git add mustache.min.js`
  33. `git commit -m ":ship: bump to version #{@target_v}"`
  34. # notify codemonkey
  35. puts "staged bumped files and created commit"
  36. puts_c 32, "successfully bumped version to #{@target_v}!"
  37. puts_c 33, "don't forget to `npm publish`!"
  38. end
  39. exit 0
  40. end
  41. def bump_source(source)
  42. file_buffer = File.read source.path
  43. if match = file_buffer.match(source.v_regex)
  44. file_v = match.captures[0]
  45. if @target_v != file_v
  46. did_bump
  47. puts "> bumping version in file '#{source.path}': #{file_v} -> #{@target_v}..."
  48. file_buffer[source.v_regex, 1] = @target_v
  49. File.open(source.path, 'w') { |f| f.write file_buffer }
  50. end
  51. else
  52. puts_c 31, "ERROR: Can't find version in '#{source.path}'"
  53. exit 1
  54. end
  55. end
  56. def did_bump
  57. if !@bumped
  58. puts 'bump detected!'
  59. end
  60. @bumped = true
  61. end
  62. end
  63. bumper = Bumper.new([
  64. Source.new('mustache.mjs', /version: '([\d\.]*)'/),
  65. Source.new('mustache.js.nuspec', /<version>([\d\.]*)<\/version>/),
  66. ])
  67. bumper.start