{"size": 1662, "ext": "js", "max_stars_repo_path": "cli/commands/setup_commands/npm.js", "max_stars_repo_name": "CrocInc/artifactory-client", "max_stars_repo_stars_event_min_datetime": "2017-10-10T17:17:44.000Z", "max_stars_repo_stars_event_max_datetime": "2017-10-10T17:17:44.000Z", "max_issues_repo_path": "cli/commands/setup_commands/npm.js", "max_issues_repo_name": "CrocInc/artifactory-client", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "cli/commands/setup_commands/npm.js", "max_forks_repo_name": "CrocInc/artifactory-client", "max_forks_repo_forks_event_min_datetime": "2017-10-10T17:32:56.000Z", "max_forks_repo_forks_event_max_datetime": "2017-11-29T13:47:17.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 27.7, "max_line_length": 100, "alphanum_fraction": 0.5932611312}
﻿var child_process = require('child_process');
var fs = require("fs");

var ArtClient = require('./../../../');

exports.command = 'npm <repo> [scope]'
exports.desc = 'Setup .npmrc config for specified repo and scope'
exports.builder = function (yargs) {
  return yargs
    //.usage('Usage: art-client npm-config [options]')
    //.demand(['repo'])
    .option('global', {
      alias: 'g',
      desc: 'setup .npmrc in user profile (otherwise in current folder)'
    })
    .example('art-client npm-config repo croc --url https://artifacts.company.com/ -u user -p pwd');

  //return yargs.commandDir('remote_cmds')
}
exports.handler = function (argv) {

  var client = new ArtClient(argv.url);
  client.getNpmConfig(argv.user, argv.password, argv.repo, argv.scope).then(function (result) {
    console.log('OK');
    if (argv.global) {
      runNpmConfig(result);
    } else {
      fs.writeFileSync(".npmrc", result);
    }
    //console.log(result);
    process.exit(0);
  }).catch(function (err) {
    console.log('Fail');
    console.log(err);
    process.exit(1);
  });

}

function runNpmConfig(result) {
  result.split('\n').forEach(function (line) {

    if (line && line.indexOf("=") > 0) {
      var idx = line.indexOf("=");
      var key = line.substring(0, idx);
      var value = line.substring(idx+1);
      var args = ["config", "set"];
      args.push(key);
      args.push(value);
      if (argv.global) {
        args.push("-g");
      }
      console.log(args.join(","));
      var child = child_process.spawnSync("npm.cmd", args, {cwd:"."});
      if (child.status == 0) {
        console.log(child.stdout.toString());
      } 
    }
  });
}