/*
 * Copyright (C) Sapphirecode - All Rights Reserved
 * This file is part of snippeteer which is released under BSD-3-Clause.
 * See file 'LICENSE' for full license details.
 * Created by Timo Hocker <timo@scode.ovh>, January 2020
 */

/* eslint-disable no-sync */
/* eslint-disable no-console */

'use strict';

const fs = require ('fs-extra');
const path = require ('path');

/**
 * copies the full template to a new folder named after arg[0]
 *
 * @param {string} folder folder to run in
 * @param {Array} args function arguments
 */
function run (folder, args) {
  const template = path.join (
    __dirname,
    'template',
    args.length === 1 && (/^node$/ui).test
      ? 'node'
      : 'general'
  );

  for (const f of fs.readdirSync (template)) {
    fs.copy (
      path.join (template, f),
      path.join (folder, f),
      { filter: (src, dest) => !fs.existsSync (dest) }
    );
  }
}

/**
 * checks if the arguments meet the requirements
 *
 * @param {string} folder folder to run in
 * @returns {boolean} true if arguments match requirements
 */
function assert (folder) {
  const tests = [
    {
      f:      () => (typeof folder === 'string'),
      reason: 'cwd is not a folder (internal error)'
    },
    {
      f:      () => (fs.existsSync (folder)),
      reason: 'cwd does not exist (internal error)'
    }
  ];
  for (const test of tests) {
    if (!test.f ()) {
      console.log (test.reason);
      return false;
    }
  }

  return true;
}

module.exports = { run, assert };