69 lines
1.7 KiB
JavaScript
Raw Normal View History

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');
2020-01-15 11:50:44 +01:00
const path = require ('path');
2020-01-15 11:40:33 +01:00
2020-01-15 13:30:18 +01:00
/**
* 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 snip_folder = path.join (folder, args[0]);
2020-01-15 11:50:44 +01:00
const template = path.join (__dirname, 'template');
2020-01-15 13:30:18 +01:00
fs.mkdir (snip_folder);
fs.copy (template, snip_folder);
}
/**
* checks if the arguments meet the requirements
*
* @param {string} folder folder to run in
* @param {Array} args function arguments
* @returns {boolean} true if arguments match requirements
*/
function assert (folder, args) {
2020-01-15 14:43:13 +01:00
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
}
];
for (const test of tests)
if (!test.f ()) {
console.log (test.reason);
return false;
}
return true;
2020-01-15 13:30:18 +01:00
}
module.exports = { run, assert };