"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _fs = require("fs");
var _path = require("path");
var _chalk = _interopRequireDefault(require("../../compiled/chalk"));
var _fsExtra = _interopRequireDefault(require("../../compiled/fs-extra"));
var _glob = _interopRequireDefault(require("../../compiled/glob"));
var _mustache = _interopRequireDefault(require("../../compiled/mustache"));
var _prompts = _interopRequireDefault(require("../../compiled/prompts"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class Generator {
  constructor({
    baseDir,
    args
  }) {
    this.baseDir = baseDir;
    this.args = args;
    this.prompts = {};
  }
  async run() {
    const questions = this.prompting();
    this.prompts = await (0, _prompts.default)(questions, {
      onCancel() {
        process.exit(1);
      }
    });
    await this.writing();
  }
  prompting() {
    return [];
  }
  async writing() {}
  copyTpl(opts) {
    const tpl = (0, _fs.readFileSync)(opts.templatePath, 'utf-8');
    const content = _mustache.default.render(tpl, opts.context);
    _fsExtra.default.mkdirpSync((0, _path.dirname)(opts.target));
    console.log(`${_chalk.default.green('Write:')} ${(0, _path.relative)(this.baseDir, opts.target)}`);
    (0, _fs.writeFileSync)(opts.target, content, 'utf-8');
  }
  copyDirectory(opts) {
    const files = _glob.default.sync('**/*', {
      cwd: opts.path,
      dot: true,
      ignore: ['**/node_modules/**']
    });
    files.forEach(file => {
      const absFile = (0, _path.join)(opts.path, file);
      if ((0, _fs.statSync)(absFile).isDirectory()) return;
      if (file.endsWith('.tpl')) {
        this.copyTpl({
          templatePath: absFile,
          target: (0, _path.join)(opts.target, file.replace(/\.tpl$/, '')),
          context: opts.context
        });
      } else {
        console.log(`${_chalk.default.green('Copy: ')} ${file}`);
        const absTarget = (0, _path.join)(opts.target, file);
        _fsExtra.default.mkdirpSync((0, _path.dirname)(absTarget));
        (0, _fs.copyFileSync)(absFile, absTarget);
      }
    });
  }
}
var _default = Generator;
exports.default = _default;