Class: DPL::Provider::NPM

Inherits:
DPL::Provider show all
Defined in:
lib/dpl/provider/npm.rb

Constant Summary collapse

NPMRC_FILE =
'~/.npmrc'
DEFAULT_NPM_REGISTRY =
'registry.npmjs.org'

Instance Method Summary collapse

Instance Method Details

#api_keyObject



63
64
65
# File 'lib/dpl/provider/npm.rb', line 63

def api_key
  option(:api_key, :api_token)
end

#check_appObject



14
15
# File 'lib/dpl/provider/npm.rb', line 14

def check_app
end

#check_authObject



23
24
25
26
# File 'lib/dpl/provider/npm.rb', line 23

def check_auth
  setup_auth
  log "Authenticated with email #{option(:email)} and API key #{api_key[-4..-1].rjust(20, '*')}"
end

#needs_key?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/dpl/provider/npm.rb', line 10

def needs_key?
  false
end

#npm_versionObject



59
60
61
# File 'lib/dpl/provider/npm.rb', line 59

def npm_version
  `npm --version`
end

#npmrc_file_contentObject



50
51
52
53
54
55
56
57
# File 'lib/dpl/provider/npm.rb', line 50

def npmrc_file_content
  log "NPM version: #{npm_version}"
  if npm_version =~ /^1/
    "_auth = ${NPM_API_KEY}\nemail = #{option(:email)}"
  else
    "//#{package_registry}/:_authToken=${NPM_API_KEY}"
  end
end

#package_registryObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/dpl/provider/npm.rb', line 39

def package_registry
  if File.exists?('package.json')
    data = JSON.parse(File.read('package.json'))
    if data['publishConfig'] && data['publishConfig']['registry']
      return URI(data['publishConfig']['registry']).host
    end
  end

  DEFAULT_NPM_REGISTRY
end

#push_appObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/dpl/provider/npm.rb', line 28

def push_app
  log "NPM API key format changed recently. If your deployment fails, check your API key in ~/.npmrc."
  log "http://docs.travis-ci.com/user/deployment/npm/"
  log "#{NPMRC_FILE} size: #{File.size(File.expand_path(NPMRC_FILE))}"

  command = "env NPM_API_KEY=#{api_key} npm publish"
  command << " --tag #{option(:tag)}" if options[:tag]
  context.shell "#{command}"
  FileUtils.rm(File.expand_path(NPMRC_FILE))
end

#setup_authObject



17
18
19
20
21
# File 'lib/dpl/provider/npm.rb', line 17

def setup_auth
  file = File.open(File.expand_path(NPMRC_FILE), 'w')
  file.puts(npmrc_file_content)
  file.flush
end