Code coverage report for lib/commands/publish.js

Statements: 36.96% (17 / 46)      Branches: 9.09% (2 / 22)      Functions: 27.27% (3 / 11)      Lines: 40.54% (15 / 37)      Ignored: none     

All files » lib/commands/ » publish.js
1 2 3 4 5 6 7 8 9 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71    1 1 1 1 1 1 1   1                                             1 1 1                               1 1                             1   1  
'use strict'
 
var log = require('winston')
var fs = require('fs')
var _ = require('lodash')
var hash = require('hash.js')
var util = require('../util.js')
var api = require('../api.js')
var project = require('../project.js')
 
function publishPackage(mlpm, auth, localExport) {
  project.createZip(mlpm, function(err, zip) {
    if (err) return console.log(err)
 
    var buffer = zip.generate({type: 'nodebuffer'})
 
    // TODO: process.nextTick()?
    mlpm.sha2sum = hash.sha256().update( buffer ).digest('hex')
 
    if ( localExport ) {
      return fs.writeFile( mlpm.name + '-' + mlpm.version + '.zip', buffer, function(err) {
        if (err) return console.log(err)
      })
    }
 
    api.publish(mlpm, buffer, auth, function(err, body) {
      if (err) return console.log(err)
 
      console.log( 'published ' + mlpm.name + '@' + mlpm.version )
    })
  })
}
 
function dryRun() {
  project.getConfig(function (err, pkgConfig) {
    Eif (err) return console.log(err)
 
    log.info( pkgConfig.name + '@' + pkgConfig.version )
 
    project.getFiles(pkgConfig, false, function(err, files) {
      if (err) return console.log(err)
 
      var cwd = process.cwd()
 
      _.each(files, function(file) {
        log.info( file.path.replace(cwd, '') )
      })
    })
  })
}
 
function publish(args) {
  Eif ( args.dryrun ) return dryRun()
 
  util.getAuth(args.admin, function(err, auth) {
    if (err) return console.log(err)
 
    project.getConfig(function(err, mlpm) {
      if (err) return console.log(err)
 
      if ( mlpm.private && !args.export ) return console.log('private; can\'t publish')
 
      publishPackage( mlpm, auth, args.export )
    })
  })
}
 
publish.usage = 'mlpm publish [--dry-run]'
 
module.exports = publish