{"size": 3070, "ext": "js", "max_stars_repo_path": "lib/option/owner.js", "max_stars_repo_name": "chinesedfan/cortex", "max_stars_repo_stars_event_min_datetime": "2020-04-30T08:20:50.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-10T07:19:09.000Z", "max_issues_repo_path": "lib/option/owner.js", "max_issues_repo_name": "chinesedfan/cortex", "max_issues_repo_issues_event_min_datetime": "2019-11-23T07:01:29.000Z", "max_issues_repo_issues_event_max_datetime": "2019-11-23T07:01:30.000Z", "max_forks_repo_path": "lib/option/owner.js", "max_forks_repo_name": "chinesedfan/cortex", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 20.0653594771, "max_line_length": 93, "alphanum_fraction": 0.52247557}
'use strict';

var node_path   = require('path');
var cortex_json = require('read-cortex-json');

var AVAILABLE_ACTIONS = [
  'add',
  'rm',
  'ls'
];


var usage = '    cortex owner add <username> <pkg>\n' +
  '    cortex owner rm <username> <pkg>';

function argument_error(option, value) {
  return {
    code: 'EPARSEARG',
    message: 'Invalid arguments, usage:\n' + usage,
    data: {
      usage: usage,
      command: 'owner',
      option: option,
      value: value
    }
  };
}


exports.options = {
  action: {
    enumerable: false,
    type: Boolean,
    info: 'the action to be processed, could be "add", "rm", or, "ls".',
    set: function(action) {
      var done = this.async();

      if (!action) {
        action = this.get('_').shift();
      }

      if (!~AVAILABLE_ACTIONS.indexOf(action)) {
        done(argument_error('action', action));

      } else {
        done(null, action);
      }
    }
  },

  user: {
    enumerable: false,
    info: 'the user to be added or removed.',
    set: function(user) {
      var action = this.get('action');
      var done = this.async();

      if (action === 'ls') {
        return done(null);
      }

      if (!user) {
        user = this.get('_').shift();
      }

      if (!user) {
        return done(argument_error('user', user));
      }

      done(null, user);
    }
  },

  cwd: {
    type: node_path,
    info: 'current working directory.',
    default: process.cwd()
  },

  pkg: {
    enumerable: false,
    type: String,
    info: 'the package name',
    set: function(name) {
      var done = this.async();

      // 'a@0.1.0' -> 'a'
      if (name) {
        return done(null, name.split('@')[0]);
      }

      if (!name) {
        name = this.get('_').shift();
      }

      if (name) {
        return done(null, name);
      }

      cortex_json.package_root(this.get('cwd'), function (cwd) {
        if (!cwd) {
          return done({
            code: 'ENOTAREPO',
            message: 'package.json not found, or you might specify a package to add owners.',
            data: {
              cwd: cwd
            }
          });
        }

        // Read local package
        // `cortex owner add kael`
        // try to read package name from package.json
        exports._get_name(cwd, done);
      });
    }
  }
};


exports._get_name = function (cwd, callback) {
  cortex_json.read(cwd, function(err, pkg) {
    if (err) {
      return callback(err);
    }

    var pkg_name = pkg.name;

    if (!pkg_name) {
      return callback({
        code: 'ENOPKG',
        message: 'you must specify a package to add owners.'
      });
    }

    callback(null, pkg_name);
  });
};


exports.info = 'Manage package owners';

exports.usage = [
  'cortex profile <sub-command>',
  '',
  'Where <sub-command> is one of:',
  '  add <username> [<pkg>]: adds a new owner to a package',
  '  rm <username> [<pkg>] : remove a owner',
  '  ls [<pkg>]            : list the owners of the current package',
  '',
  'If <pkg> is not specified, cortex will use the package in the <cwd>.'
];
