/* eslint-disable no-sync */ /* eslint-disable no-console */ 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 snip_folder = path.join (folder, args[0]); const template = path.join (__dirname, 'template'); 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) { const tests = [ { f: () => (args.length < 2), reason: 'too many arguments' }, { f: () => (args.length === 0 || typeof args[0] === 'string'), reason: 'name is not a string' }, { f: () => (args.length === 0 || (/^[a-z]+$/iu).test (args[0])), reason: 'name can only contain [a-z]' }, { f: () => (typeof folder === 'string'), reason: 'cwd is not a folder (internal error)' }, { f: () => (fs.existsSync (folder)), 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' } ]; for (const test of tests) if (!test.f ()) { console.log (test.reason); return false; } return true; } module.exports = { run, assert };