Home Reference Source Repository

src/Generator.js

import path from 'path';

import fs from 'fs-extra';
import mapStream from 'map-stream';
import vFs from 'vinyl-fs';

const CONFIG_FILE_NAME = 'dokata.json';
const TEMPLATE_DIR_NAME = 'template';

function log(file, cb) {
  console.log(file.path);
  cb(null, file);
}


export default class Generator {

  constructor(baseDir, {output = './'} = {}) {
    this.baseDir = baseDir;
    this.templateDir = path.join(this.baseDir, TEMPLATE_DIR_NAME);
    this.outputDir = path.resolve(output);
    this.outputName = path.basename(this.outputDir);
    this.config = this.loadConfig();
  }

  execute() {
    vFs.src(path.join(this.templateDir, '**/*'))
      .pipe(mapStream(log))
      .pipe(vFs.dest(this.outputDir));
  }

  /**
   * Load config from dokata config file
   *
   * @return {Object}
   */
  loadConfig() {
    const defaultConfig = {
      context: {},
      questions: [],
      postProcess: []
    };
    return Object.assign(
      {},
      defaultConfig,
      fs.readJsonSync(path.join(this.baseDir, CONFIG_FILE_NAME))
    );
  }
}


const gen = new Generator(path.resolve('./example/templates/dokata-template-example'), {output: 'hoge/fuga/foo'});
gen.execute();
console.log(gen.outputDir, gen.outputName);