1/* eslint-disable standard/no-callback-literal */ 2 3const columns = require('cli-columns') 4const figgyPudding = require('figgy-pudding') 5const libteam = require('libnpm/team') 6const npmConfig = require('./config/figgy-config.js') 7const output = require('./utils/output.js') 8const otplease = require('./utils/otplease.js') 9const usage = require('./utils/usage') 10 11module.exports = team 12 13team.subcommands = ['create', 'destroy', 'add', 'rm', 'ls', 'edit'] 14 15team.usage = usage( 16 'team', 17 'npm team create <scope:team> [--otp <otpcode>]\n' + 18 'npm team destroy <scope:team> [--otp <otpcode>]\n' + 19 'npm team add <scope:team> <user> [--otp <otpcode>]\n' + 20 'npm team rm <scope:team> <user> [--otp <otpcode>]\n' + 21 'npm team ls <scope>|<scope:team>\n' + 22 'npm team edit <scope:team>' 23) 24 25const TeamConfig = figgyPudding({ 26 json: {}, 27 loglevel: {}, 28 parseable: {}, 29 silent: {} 30}) 31 32function UsageError () { 33 throw Object.assign(new Error(team.usage), {code: 'EUSAGE'}) 34} 35 36team.completion = function (opts, cb) { 37 var argv = opts.conf.argv.remain 38 if (argv.length === 2) { 39 return cb(null, team.subcommands) 40 } 41 switch (argv[2]) { 42 case 'ls': 43 case 'create': 44 case 'destroy': 45 case 'add': 46 case 'rm': 47 case 'edit': 48 return cb(null, []) 49 default: 50 return cb(new Error(argv[2] + ' not recognized')) 51 } 52} 53 54function team ([cmd, entity = '', user = ''], cb) { 55 // Entities are in the format <scope>:<team> 56 otplease(npmConfig(), opts => { 57 opts = TeamConfig(opts).concat({description: null}) 58 entity = entity.replace(/^@/, '') 59 switch (cmd) { 60 case 'create': return teamCreate(entity, opts) 61 case 'destroy': return teamDestroy(entity, opts) 62 case 'add': return teamAdd(entity, user, opts) 63 case 'rm': return teamRm(entity, user, opts) 64 case 'ls': { 65 const match = entity.match(/[^:]+:.+/) 66 if (match) { 67 return teamListUsers(entity, opts) 68 } else { 69 return teamListTeams(entity, opts) 70 } 71 } 72 case 'edit': 73 throw new Error('`npm team edit` is not implemented yet.') 74 default: 75 UsageError() 76 } 77 }).then( 78 data => cb(null, data), 79 err => err.code === 'EUSAGE' ? cb(err.message) : cb(err) 80 ) 81} 82 83function teamCreate (entity, opts) { 84 return libteam.create(entity, opts).then(() => { 85 if (opts.json) { 86 output(JSON.stringify({ 87 created: true, 88 team: entity 89 })) 90 } else if (opts.parseable) { 91 output(`${entity}\tcreated`) 92 } else if (!opts.silent && opts.loglevel !== 'silent') { 93 output(`+@${entity}`) 94 } 95 }) 96} 97 98function teamDestroy (entity, opts) { 99 return libteam.destroy(entity, opts).then(() => { 100 if (opts.json) { 101 output(JSON.stringify({ 102 deleted: true, 103 team: entity 104 })) 105 } else if (opts.parseable) { 106 output(`${entity}\tdeleted`) 107 } else if (!opts.silent && opts.loglevel !== 'silent') { 108 output(`-@${entity}`) 109 } 110 }) 111} 112 113function teamAdd (entity, user, opts) { 114 return libteam.add(user, entity, opts).then(() => { 115 if (opts.json) { 116 output(JSON.stringify({ 117 added: true, 118 team: entity, 119 user 120 })) 121 } else if (opts.parseable) { 122 output(`${user}\t${entity}\tadded`) 123 } else if (!opts.silent && opts.loglevel !== 'silent') { 124 output(`${user} added to @${entity}`) 125 } 126 }) 127} 128 129function teamRm (entity, user, opts) { 130 return libteam.rm(user, entity, opts).then(() => { 131 if (opts.json) { 132 output(JSON.stringify({ 133 removed: true, 134 team: entity, 135 user 136 })) 137 } else if (opts.parseable) { 138 output(`${user}\t${entity}\tremoved`) 139 } else if (!opts.silent && opts.loglevel !== 'silent') { 140 output(`${user} removed from @${entity}`) 141 } 142 }) 143} 144 145function teamListUsers (entity, opts) { 146 return libteam.lsUsers(entity, opts).then(users => { 147 users = users.sort() 148 if (opts.json) { 149 output(JSON.stringify(users, null, 2)) 150 } else if (opts.parseable) { 151 output(users.join('\n')) 152 } else if (!opts.silent && opts.loglevel !== 'silent') { 153 output(`\n@${entity} has ${users.length} user${users.length === 1 ? '' : 's'}:\n`) 154 output(columns(users, {padding: 1})) 155 } 156 }) 157} 158 159function teamListTeams (entity, opts) { 160 return libteam.lsTeams(entity, opts).then(teams => { 161 teams = teams.sort() 162 if (opts.json) { 163 output(JSON.stringify(teams, null, 2)) 164 } else if (opts.parseable) { 165 output(teams.join('\n')) 166 } else if (!opts.silent && opts.loglevel !== 'silent') { 167 output(`\n@${entity} has ${teams.length} team${teams.length === 1 ? '' : 's'}:\n`) 168 output(columns(teams.map(t => `@${t}`), {padding: 1})) 169 } 170 }) 171} 172