#!/usr/bin/env node
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _require = require("child_process"),
    execSync = _require.execSync;

var path = require("path");

var _ = require("lodash");

var inquirer = require('inquirer');

var fs = require('fs');

var ncp = require('ncp');

var fsExtra = require("fs-extra");

var mainData = "'use strict';\nimport { Server } from \"dkaframework\";\n\n(async() => {\n    await Server({\n        serverPort : 8084,\n        app : async(app, opts, next) => {\n        \n            next();\n        }\n    }).then(async(res) => {\n        console.log(res);\n    }).catch(async(err) => {\n        console.log(err);\n    });\n})();\n                        ";
var lang = {
  indonesia: [{
    type: 'input',
    name: 'name',
    message: "Apa Nama Paket Anda (tanpa spasi : ",
    "default": function _default() {
      return 'halo_dunia';
    }
  }, {
    type: 'input',
    name: 'version',
    message: "Apa Versi Paket Anda : ",
    "default": function _default() {
      return '1.0.0';
    }
  }, {
    type: 'input',
    name: 'description',
    message: "Apa Deskripsi Paket Anda : "
  }, {
    type: 'input',
    name: 'main',
    message: "Dimana Lokasi File Point Masuk Anda : ",
    "default": function _default() {
      return 'index.js';
    }
  }, {
    type: 'input',
    name: 'repository',
    message: "Alamat Git Repo Anda : "
  }, {
    type: 'input',
    name: 'author',
    message: "Apa nama Pemilik Project Ini : "
  }, {
    type: 'input',
    name: 'license',
    message: "Apa Jenis Lisensi Yang Diterapkan di project Ini : ",
    "default": function _default() {
      return 'MIT';
    }
  }, {
    type: 'input',
    name: 'private',
    message: "Apakah Project Ini Bisa Di akses publik ",
    "default": function _default() {
      return true;
    }
  }],
  english: [{
    type: 'input',
    name: 'name',
    message: "What Your Package Name (without spacing : ",
    "default": function _default() {
      return 'hello_world';
    }
  }, {
    type: 'input',
    name: 'version',
    message: "What Your package Version : ",
    "default": function _default() {
      return '1.0.0';
    }
  }, {
    type: 'input',
    name: 'description',
    message: "What Your Description Package : "
  }, {
    type: 'input',
    name: 'main',
    message: "What The Entry Point : ",
    "default": function _default() {
      return 'index.js';
    }
  }, {
    type: 'input',
    name: 'repository',
    message: "What The Repository URL : "
  }, {
    type: 'input',
    name: 'author',
    message: "What Author Name This Project : "
  }, {
    type: 'input',
    name: 'license',
    message: "What The License Used : ",
    "default": function _default() {
      return 'MIT';
    }
  }, {
    type: 'input',
    name: 'private',
    message: "Grant Access Public ",
    "default": function _default() {
      return true;
    }
  }]
};
inquirer.prompt([{
  type: 'list',
  name: 'lang',
  message: 'Please Use language used : ',
  choices: ['Indonesia', 'English'],
  filter: function filter(val) {
    return val.toLowerCase();
  }
}]).then( /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(answers) {
    return _regenerator["default"].wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            _context6.next = 2;
            return inquirer.prompt(lang[answers.lang]).then( /*#__PURE__*/function () {
              var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(getResponse) {
                var res, mPath, mIndexFile, mBabelConfig, a, b;
                return _regenerator["default"].wrap(function _callee5$(_context5) {
                  while (1) {
                    switch (_context5.prev = _context5.next) {
                      case 0:
                        res = _.extend(getResponse, {
                          repository: {
                            url: getResponse.repository
                          },
                          scripts: {
                            dev: "dka -b ".concat(getResponse.main)
                          },
                          "devDependencies": {
                            "dkaframework": "latest"
                          }
                        });
                        mPath = path.join(process.cwd(), './package.json');
                        mIndexFile = path.join(process.cwd(), "./".concat(getResponse.main));
                        mBabelConfig = path.join(process.cwd(), "./.babelrc");
                        a = new Promise( /*#__PURE__*/function () {
                          var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(resolve, rejected) {
                            return _regenerator["default"].wrap(function _callee$(_context) {
                              while (1) {
                                switch (_context.prev = _context.next) {
                                  case 0:
                                    _context.next = 2;
                                    return fs.writeFile(mPath, JSON.stringify(res, null, 2), 'utf-8', function (err) {
                                      if (!err) {
                                        resolve();
                                      } else {
                                        rejected({
                                          status: false,
                                          msg: "Cannot Create Package JSON"
                                        });
                                      }
                                    });

                                  case 2:
                                  case "end":
                                    return _context.stop();
                                }
                              }
                            }, _callee);
                          }));

                          return function (_x3, _x4) {
                            return _ref3.apply(this, arguments);
                          };
                        }());
                        b = new Promise( /*#__PURE__*/function () {
                          var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(resolve, rejected) {
                            return _regenerator["default"].wrap(function _callee2$(_context2) {
                              while (1) {
                                switch (_context2.prev = _context2.next) {
                                  case 0:
                                    _context2.next = 2;
                                    return fs.writeFile(mIndexFile, mainData, 'utf-8', function (err) {
                                      if (!err) {
                                        resolve();
                                      } else {
                                        rejected({
                                          status: false,
                                          msg: "Cannot Create Index.js"
                                        });
                                      }
                                    });

                                  case 2:
                                  case "end":
                                    return _context2.stop();
                                }
                              }
                            }, _callee2);
                          }));

                          return function (_x5, _x6) {
                            return _ref4.apply(this, arguments);
                          };
                        }());
                        _context5.next = 8;
                        return Promise.all([a, b]).then( /*#__PURE__*/function () {
                          var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(res) {
                            return _regenerator["default"].wrap(function _callee3$(_context3) {
                              while (1) {
                                switch (_context3.prev = _context3.next) {
                                  case 0:
                                    _context3.next = 2;
                                    return execSync('yarn install', {
                                      stdio: 'inherit'
                                    });

                                  case 2:
                                    console.log("Success ! : Happy Codding :))");
                                    console.log("run Code With \"yarn run dev\" ");

                                  case 4:
                                  case "end":
                                    return _context3.stop();
                                }
                              }
                            }, _callee3);
                          }));

                          return function (_x7) {
                            return _ref5.apply(this, arguments);
                          };
                        }())["catch"]( /*#__PURE__*/function () {
                          var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(err) {
                            return _regenerator["default"].wrap(function _callee4$(_context4) {
                              while (1) {
                                switch (_context4.prev = _context4.next) {
                                  case 0:
                                    console.error(err);

                                  case 1:
                                  case "end":
                                    return _context4.stop();
                                }
                              }
                            }, _callee4);
                          }));

                          return function (_x8) {
                            return _ref6.apply(this, arguments);
                          };
                        }());

                      case 8:
                      case "end":
                        return _context5.stop();
                    }
                  }
                }, _callee5);
              }));

              return function (_x2) {
                return _ref2.apply(this, arguments);
              };
            }());

          case 2:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));

  return function (_x) {
    return _ref.apply(this, arguments);
  };
}());
//# sourceMappingURL=dka_create_app.js.map