"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.generateHandlers = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _cli = require("@webiny/cli");
var _getHandlerPath = require("./getHandlerPath");
const generateHandlers = (type, app, paths) => {
  return {
    type: "hook-before-build",
    async hook({
      projectApplication
    }) {
      for (let i = 0; i < paths.length; i++) {
        const current = paths[i];
        const from = (0, _getHandlerPath.getHandlerPath)(type, app, ...current, "handler.js");
        const to = _path.default.join(projectApplication.paths.workspace, ...current, "build", "handler.js");
        if (!_fs.default.existsSync(from)) {
          _cli.cli.debug(`Prebuilt handler not found at %s.`, from.replace(_cli.cli.project.root, ""));
          continue;
        }
        if (!_fs.default.existsSync(_path.default.dirname(to))) {
          _fs.default.mkdirSync(_path.default.dirname(to), {
            recursive: true
          });
        }
        _fs.default.copyFileSync(from, to);
      }
    }
  };
};
exports.generateHandlers = generateHandlers;

//# sourceMappingURL=generateHandlers.js.map