Class: CapsuleCD::Javascript::JavascriptEngine

Inherits:
Engine
  • Object
show all
Defined in:
lib/capsulecd/javascript/javascript_engine.rb

Instance Attribute Summary

Attributes inherited from Engine

#config

Instance Method Summary collapse

Methods inherited from Engine

#initialize, #post_build_step, #post_package_step, #post_release_step, #post_runner_retrieve_payload, #post_source_configure, #post_source_process_pull_request_payload, #post_source_process_push_payload, #post_source_release, #post_test_step, #pre_build_step, #pre_package_step, #pre_release_step, #pre_runner_retrieve_payload, #pre_source_configure, #pre_source_process_pull_request_payload, #pre_source_process_push_payload, #pre_source_release, #pre_test_step, #start

Constructor Details

This class inherits a constructor from CapsuleCD::Engine

Instance Method Details

#build_stepObject



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
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/capsulecd/javascript/javascript_engine.rb', line 10

def build_step
  super

  @_is_bower = File.exist?(@source_git_local_path + '/bower.json')
  @_is_npm = File.exist?(@source_git_local_path + '/package.json')

  # validate that the metadata files exist
  if !@_is_bower && !@_is_npm
    fail CapsuleCD::Error::BuildPackageInvalid, 'package.json or bower.json file must be present for Javascript packages'
  end

  # we can't bump the npm version here because the npm version patch command will set it.
  # howerver we need to make sure the bower.json and package.json versions are insync.
  # we'll take the latest version of either the package.json or bower.json and set that as the version of both.
  sync_versions

  # now that the bower and package versions are in sync, lets bump the version of bower.json
  # (because package.json will be bumped automatically.)
  if @_is_bower
    bower_file = File.read(@source_git_local_path + '/bower.json')
    bower_data = JSON.parse(bower_file)
    next_version = bump_version(SemVer.parse(bower_data['version']))
    bower_data['version'] = next_version.to_s
    File.write(@source_git_local_path + '/bower.json', JSON.pretty_generate(bower_data))
  end


  # TODO: check if this module name and version already exist.

  # check for/create any required missing folders/files
  unless File.exist?(@source_git_local_path + '/test')
    FileUtils.mkdir(@source_git_local_path + '/test')
  end
  unless File.exist?(@source_git_local_path + '/.gitignore')
    CapsuleCD::GitUtils.create_gitignore(@source_git_local_path, ['Node','Yeoman'])
  end
end

#package_stepObject

run npm publish



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/capsulecd/javascript/javascript_engine.rb', line 121

def package_step
  super

  # commit changes to the cookbook. (test run occurs before this, and it should clean up any instrumentation files, created,
  # as they will be included in the commmit and any release artifacts)
  CapsuleCD::GitUtils.commit(@source_git_local_path, 'Committing automated changes before packaging.') rescue puts 'No changes to commit..'
  if @_is_bower && !@_is_npm
    bower_file = File.read(@source_git_local_path + '/bower.json')
    bower_data = JSON.parse(bower_file)
    next_version = SemVer.parse(bower_data['version'])
    @source_release_commit = CapsuleCD::GitUtils.tag(@source_git_local_path, "v#{next_version}")

  else

    # run npm publish
    Open3.popen3("npm version #{@config.engine_version_bump_type} -m '(v%s) Automated packaging of release by CapsuleCD'", chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      fail 'npm version bump failed' unless external.value.success?
    end
    @source_release_commit = CapsuleCD::GitUtils.get_latest_tag_commit(@source_git_local_path)
  end


end

#release_stepObject

this step should push the release to the package repository (ie. npm, chef supermarket, rubygems)



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/capsulecd/javascript/javascript_engine.rb', line 155

def release_step
  super

  if @_is_npm
    npmrc_path = File.expand_path('~/.npmrc')

    unless @config.npm_auth_token
      fail CapsuleCD::Error::ReleaseCredentialsMissing, 'cannot deploy page to npm, credentials missing'
    end

    # write the knife.rb config file.
    File.open(npmrc_path, 'w+') do |file|
      file.write("//registry.npmjs.org/:_authToken=#{@config.npm_auth_token}")
    end

    # run npm publish
    Open3.popen3('npm publish .', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::ReleasePackageError, 'npm publish failed. Check log for exact error'
      end
    end
  end
end

#test_stepObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/capsulecd/javascript/javascript_engine.rb', line 48

def test_step
  super

  if @_is_npm
    # the module has already been downloaded. lets make sure all its dependencies are available.
    Open3.popen3('npm install', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestDependenciesError, 'npm install failed. Check module dependencies'
      end
    end
    # create a shrinkwrap file.
    Open3.popen3('npm shrinkwrap', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestDependenciesError, 'npm shrinkwrap failed. Check log for exact error'
      end
    end

    # run test command
    test_cmd = @config.engine_cmd_test || 'npm test'
    Open3.popen3(ENV, test_cmd, chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestRunnerError, test_cmd + ' failed. Check log for exact error'
      end
    end unless @config.engine_disable_test
  end

  if @_is_bower
    # lets make sure all the bower dependencies are available.
    Open3.popen3('bower install --allow-root', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
      { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
        Thread.new do
          until (line = stream_buffer.gets).nil?
            puts "#{name} -> #{line}"
          end
        end
      end
      # wait for process
      external.join
      unless external.value.success?
        fail CapsuleCD::Error::TestDependenciesError, 'npm install failed. Check module dependencies'
      end
    end
  end
end