2020-01-18 21:14:40 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-01-15 13:30:18 +01:00
|
|
|
/* eslint-disable no-sync */
|
2020-01-15 14:43:13 +01:00
|
|
|
/* eslint-disable no-console */
|
2020-01-15 13:30:18 +01:00
|
|
|
|
|
|
|
const fs = require ('fs-extra');
|
|
|
|
const path = require ('path');
|
|
|
|
|
|
|
|
/**
|
2020-01-15 14:43:13 +01:00
|
|
|
* copies the full template to a new folder named after arg[0]
|
2020-01-15 13:30:18 +01:00
|
|
|
*
|
|
|
|
* @param {string} folder folder to run in
|
2020-01-15 14:43:13 +01:00
|
|
|
* @param {Array} args function arguments
|
2020-01-15 13:30:18 +01:00
|
|
|
*/
|
2020-01-15 14:43:13 +01:00
|
|
|
function run (folder, args) {
|
2020-01-17 17:50:10 +01:00
|
|
|
const snip_folder_path = [ folder ];
|
|
|
|
if (args.length > 0)
|
|
|
|
snip_folder_path.push (args[0]);
|
|
|
|
const snip_folder = path.join (...snip_folder_path);
|
2020-01-15 13:30:18 +01:00
|
|
|
const template = path.join (__dirname, 'template');
|
2020-01-17 17:50:10 +01:00
|
|
|
if (!fs.existsSync (snip_folder))
|
|
|
|
fs.mkdir (snip_folder);
|
2020-02-20 10:47:38 +01:00
|
|
|
for (const f of fs.readdirSync (template)) {
|
2020-01-17 17:50:10 +01:00
|
|
|
fs.copy (
|
|
|
|
path.join (template, f),
|
|
|
|
path.join (snip_folder, f),
|
|
|
|
{
|
|
|
|
recursive: true,
|
|
|
|
filter: (src, dest) => !fs.existsSync (dest)
|
|
|
|
}
|
|
|
|
);
|
2020-02-20 10:47:38 +01:00
|
|
|
}
|
2020-01-15 13:30:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if the arguments meet the requirements
|
|
|
|
*
|
|
|
|
* @param {string} folder folder to run in
|
2020-01-15 14:43:13 +01:00
|
|
|
* @param {Array} args function arguments
|
2020-01-15 13:30:18 +01:00
|
|
|
* @returns {boolean} true if arguments match requirements
|
|
|
|
*/
|
2020-01-15 14:43:13 +01:00
|
|
|
function assert (folder, args) {
|
|
|
|
const tests = [
|
|
|
|
{
|
2020-01-15 20:11:16 +01:00
|
|
|
f: () => (args.length < 2),
|
|
|
|
reason: 'too many arguments'
|
2020-01-15 14:43:13 +01:00
|
|
|
},
|
|
|
|
{
|
2020-01-15 20:11:16 +01:00
|
|
|
f: () => (args.length === 0 || typeof args[0] === 'string'),
|
2020-01-15 14:43:13 +01:00
|
|
|
reason: 'name is not a string'
|
|
|
|
},
|
|
|
|
{
|
2020-01-15 20:11:16 +01:00
|
|
|
f: () => (args.length === 0 || (/^[a-z]+$/iu).test (args[0])),
|
2020-01-15 14:43:13 +01:00
|
|
|
reason: 'name can only contain [a-z]'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
f: () => (typeof folder === 'string'),
|
|
|
|
reason: 'cwd is not a folder (internal error)'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
f: () => (fs.existsSync (folder)),
|
2020-01-15 20:11:16 +01:00
|
|
|
reason: 'cwd does not exist (internal error)'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
f: () => (args.length === 1 || fs.readdirSync (folder).length === 0),
|
|
|
|
reason: 'folder is not empty'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
f: () => (args.length === 0
|
|
|
|
|| !fs.existsSync (path.join (folder, args[0]))),
|
|
|
|
reason: 'folder already exists'
|
2020-01-15 14:43:13 +01:00
|
|
|
}
|
|
|
|
];
|
2020-02-20 10:47:38 +01:00
|
|
|
for (const test of tests) {
|
2020-01-15 14:43:13 +01:00
|
|
|
if (!test.f ()) {
|
|
|
|
console.log (test.reason);
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-20 10:47:38 +01:00
|
|
|
}
|
2020-01-15 14:43:13 +01:00
|
|
|
|
|
|
|
return true;
|
2020-01-15 13:30:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { run, assert };
|