const { resolve } = require('path');
const inquirer = require('inquirer');
const { readJson } = require('./json_file');

const defaultKibanaPath = () => process.cwd();

function validKibanaPath(kibanaPath) {
  try {
    return readJson(`${kibanaPath}/package.json`).name === 'kibana';
  } catch (e) {
    return false;
  }
}

exports.values = [
  {
    name: 'kibanaPath',
    message: 'Kibana path (relative path is ok):',
    filter: str => resolve(__dirname, '..', str),
    when: () => !validKibanaPath(defaultKibanaPath()),
    validate: validKibanaPath,
  },
  {
    name: 'url',
    message: 'The plugin username/repo:',
    validate: str => /^(\bbitbucket:|\bgitlab:)?[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+/.test(str),
  },
  {
    name: 'branch',
    message: 'Repo branch:',
    default: 'master',
  },
  {
    name: 'setVersion',
    type: 'confirm',
    message: 'Overwrite the plugin version?',
    default: true,
  },
  {
    name: 'setPath',
    type: 'confirm',
    message: 'Use plugin name as folder name?',
    default: true,
  },
];

exports.prompt = () => inquirer.prompt(exports.values)
.then(ans => Object.assign({ kibanaPath: defaultKibanaPath() }, ans));

@@ -160,16 +160,58 @@
 s.cwd();
+%0Aconst pluginName = () =%3E process.argv%5B2%5D;
 %0A%0Afuncti
@@ -361,16 +361,130 @@
 %0A  %7D%0A%7D%0A%0A
+function validPluginRepo(str) %7B%0A  return /%5E(%5Cbbitbucket:%7C%5Cbgitlab:)?%5Ba-zA-Z0-9_-%5D+%5C/%5Ba-zA-Z0-9_-%5D+/.test(str);%0A%7D%0A%0A
 exports.
@@ -785,93 +785,81 @@
     
-validate: str =%3E /%5E(%5Cbbitbucket:%7C%5Cbgitlab:)?%5Ba-zA-Z0-9_-%5D+%5C/%5Ba-zA-Z0-9_-%5D+/.test(str)
+when: () =%3E !validPluginRepo(pluginName()),%0A    validate: validPluginRepo
 ,%0A  
@@ -1266,16 +1266,18 @@
 assign(%7B
+%0A 
  kibanaP
@@ -1300,17 +1300,39 @@
 naPath()
- 
+,%0A  url: pluginName(),%0A
 %7D, ans))

allow passing repo info as first arg