diff --git a/snippets/snippet/index.js b/snippets/snippet/index.js index cd70fc3..f1c156d 100644 --- a/snippets/snippet/index.js +++ b/snippets/snippet/index.js @@ -1,4 +1,5 @@ /* eslint-disable no-sync */ +/* eslint-disable no-console */ const fs = require ('fs-extra'); const path = require ('path'); @@ -24,11 +25,35 @@ function run (folder, args) { * @returns {boolean} true if arguments match requirements */ function assert (folder, args) { - return (args.length === 1) - && (typeof args[0] === 'string') - && (/^[a-zA-Z]+$/u).test (args[0]) - && (typeof folder === 'string') - && (fs.existsSync (folder)); + const tests = [ + { + f: () => (args.length === 1), + reason: 'not enough arguments' + }, + { + f: () => (typeof args[0] === 'string'), + reason: 'name is not a string' + }, + { + f: () => (/^[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' + } + ]; + for (const test of tests) + if (!test.f ()) { + console.log (test.reason); + return false; + } + + return true; } module.exports = { run, assert }; diff --git a/snippets/snippet/template/index.js b/snippets/snippet/template/index.js index 87556c2..f1c156d 100644 --- a/snippets/snippet/template/index.js +++ b/snippets/snippet/template/index.js @@ -1,27 +1,59 @@ /* eslint-disable no-sync */ +/* eslint-disable no-console */ const fs = require ('fs-extra'); const path = require ('path'); /** - * copies the full template to the current folder + * 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) { +function run (folder, args) { + const snip_folder = path.join (folder, args[0]); const template = path.join (__dirname, 'template'); - fs.copy (template, folder); + 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) { - return (typeof folder === 'string') - && (fs.existsSync (folder)); +function assert (folder, args) { + const tests = [ + { + f: () => (args.length === 1), + reason: 'not enough arguments' + }, + { + f: () => (typeof args[0] === 'string'), + reason: 'name is not a string' + }, + { + f: () => (/^[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' + } + ]; + for (const test of tests) + if (!test.f ()) { + console.log (test.reason); + return false; + } + + return true; } module.exports = { run, assert };