pulsar/build/tasks/publish-packages.coffee

109 lines
3.3 KiB
CoffeeScript
Raw Normal View History

2013-10-22 03:23:52 +04:00
async = require 'async'
request = require 'request'
# Configure and publish all packages in package.json to atom.io
#
# This task should be run whenever you want to be sure that atom.io contains
# all the packages and versions referenced in Atom's package.json file.
module.exports = (grunt) ->
2013-10-23 02:09:09 +04:00
{spawn} = require('./task-helpers')(grunt)
2014-02-25 05:38:19 +04:00
baseUrl = "https://atom.io/api/packages"
2013-10-22 03:23:52 +04:00
packageExists = (packageName, token, callback) ->
requestSettings =
url: "#{baseUrl}/#{packageName}"
json: true
headers:
authorization: token
request.get requestSettings, (error, response, body={}) ->
if error?
callback(error)
else
callback(null, response.statusCode is 404)
createPackage = (packageName, token, callback) ->
requestSettings =
url: baseUrl
json: true
headers:
authorization: token
method: 'POST'
body:
repository: "atom/#{packageName}"
request.get requestSettings, (error, response, body={}) ->
if error?
callback(error)
else if response.statusCode isnt 201
2013-10-22 03:23:52 +04:00
message = body.message ? body.error ? body
callback(new Error("Creating package failed: #{message}"))
else
callback()
createPackageVersion = (packageName, tag, token, callback) ->
requestSettings =
url: "#{baseUrl}/#{packageName}/versions"
json: true
method: 'POST'
headers:
authorization: token
2013-10-22 07:11:04 +04:00
body:
tag: tag
2013-10-22 03:23:52 +04:00
request.get requestSettings, (error, response, body={}) ->
if error?
callback(error)
else if response.statusCode isnt 201
2013-10-22 03:23:52 +04:00
message = body.message ? body.error ? body
if message is 'Version exists'
callback()
else
callback(new Error("Creating new version failed: #{message}"))
else
callback()
2013-10-23 02:09:09 +04:00
getToken = (callback) ->
if token = process.env.ATOM_ACCESS_TOKEN
callback(null, token)
else
spawn {cmd: 'security', args: ['-q', 'find-generic-password', '-ws', 'GitHub API Token']}, (error, result, code) ->
token = result.toString() unless error?
callback(error, token)
2013-10-22 03:23:52 +04:00
2013-10-23 02:09:09 +04:00
grunt.registerTask 'publish-packages', 'Publish all bundled packages', ->
2013-10-22 03:23:52 +04:00
done = @async()
2013-10-23 02:09:09 +04:00
getToken (error, token) ->
unless token
grunt.log.error('Token not found in keychain or ATOM_ACCESS_TOKEN environment variable')
done(false)
2013-10-22 03:23:52 +04:00
2013-10-23 02:09:09 +04:00
{packageDependencies} = grunt.file.readJSON('package.json') ? {}
tasks = []
for name, version of packageDependencies
do (name, version) ->
tasks.push (callback) ->
grunt.verbose.writeln("Publishing #{name}@#{version}")
2013-10-23 02:09:09 +04:00
tag = "v#{version}"
packageExists name, token, (error, exists) ->
if error?
callback(error)
return
2013-10-22 03:23:52 +04:00
2013-10-23 02:09:09 +04:00
if exists
createPackage name, token, (error) ->
if error?
callback(error)
else
createPackageVersion(name, tag, token, callback)
else
createPackageVersion(name, tag, token, callback)
async.waterfall tasks, (error) ->
if error?
grunt.log.error(error.message)
done(false)
else
done()