"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _path = _interopRequireDefault(require("path"));

var _https = _interopRequireDefault(require("https"));

var _webpack = _interopRequireDefault(require("webpack"));

var _express = _interopRequireDefault(require("express"));

var _child_process = require("child_process");

var _webpackDevMiddleware = _interopRequireDefault(require("webpack-dev-middleware"));

var _HMRMiddleware = _interopRequireDefault(require("../middlewares/HMRMiddleware"));

var _utils = require("../utils");

var _webpackDocs = _interopRequireDefault(require("../configs/webpack.docs.config"));

var _httpsOptions = require("./httpsOptions");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// import fs from 'fs';
var _default = isSSTest => {
  const options = (0, _utils.getOptions)();
  const httpsOptions = (0, _httpsOptions.httpsOptionsWithUserFriendlyError)(options);
  const {
    docs: {
      server
    }
  } = options;
  const {
    port,
    branch,
    host,
    domain,
    iphost
  } = server;
  const appPath = process.cwd();
  const app = (0, _express.default)();
  app.use(_express.default.json());
  app.use(_express.default.urlencoded({
    extended: true
  }));
  const docsConfig = (0, _webpackDocs.default)(isSSTest);
  const compiler = (0, _webpack.default)(docsConfig);
  const wdm = (0, _webpackDevMiddleware.default)(compiler, {
    logLevel: 'error',
    publicPath: docsConfig.output.publicPath,
    headers: {
      'Access-Control-Allow-Origin': '*'
    }
  });
  app.use(wdm);
  app.use((0, _HMRMiddleware.default)(compiler, {
    path: '/sockjs-node/info'
  }));
  app.use('/docs/external/', _express.default.static(_path.default.join(appPath, 'docs', 'external')));
  app.use('/docs', _express.default.static(_path.default.join(__dirname, '..', '..', 'templates', 'docs')));
  app.use('/docs/*', (req, res) => {
    res.sendFile(_path.default.join(__dirname, '..', '..', 'templates', 'docs', 'index.html'));
  });
  app.use('/author/get', (req, res) => {
    const {
      query: {
        componentName = ''
      }
    } = req;
    res.json({
      stdout: (0, _child_process.execSync)(`git blame ${componentName}`).toString()
    });
  });
  app.use('/cli/version', (req, res) => {
    res.json({
      version: require('../../package.json').version
    });
  });

  if (branch) {
    app.post('/repo/merge', (req, res) => {
      const {
        ref
      } = req.body;

      if (ref && ref.endsWith(branch)) {
        (0, _child_process.spawnSync)('git', ['pull', 'origin', branch], {
          stdio: 'inherit'
        });
      }

      res.send('done');
    });
  }

  const httpsServer = _https.default.createServer(httpsOptions, app);

  if (!isSSTest) {
    httpsServer.listen(port, err => {
      if (err) {
        throw err;
      }

      (0, _utils.log)(`Listening at ${(0, _utils.getServerURL)(server, 'htt' + 'ps')}/docs/`);
    });
  }

  const httpPort = Number(port) + 1;
  const http = app.listen(httpPort, err => {
    if (err) {
      throw err;
    }

    if (isSSTest) {
      (0, _utils.log)(`Listening at ${(0, _utils.getServerURL)(Object.assign(server, {
        host: iphost,
        port: port
      }), 'htt' + 'p')}/docs/`);
    } else {
      (0, _utils.log)(`Listening at ${(0, _utils.getServerURL)({
        host,
        domain,
        port
      }, 'htt' + 'p')}/docs/`);
    }
  });
  return {
    compiler,
    app,
    http,
    https: httpsServer,
    wdm
  };
};

exports.default = _default;