From 5be9e89e0f88daede67fe0432b2179709841b169 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Wed, 15 Jan 2020 20:11:16 +0100 Subject: [PATCH] more versatile template --- snippets/snippet/index.js | 19 ++++++++++++++----- snippets/snippet/template/index.js | 19 ++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/snippets/snippet/index.js b/snippets/snippet/index.js index f1c156d..545c663 100644 --- a/snippets/snippet/index.js +++ b/snippets/snippet/index.js @@ -27,15 +27,15 @@ function run (folder, args) { function assert (folder, args) { const tests = [ { - f: () => (args.length === 1), - reason: 'not enough arguments' + f: () => (args.length < 2), + reason: 'too many arguments' }, { - f: () => (typeof args[0] === 'string'), + f: () => (args.length === 0 || typeof args[0] === 'string'), reason: 'name is not a string' }, { - f: () => (/^[a-z]+$/iu).test (args[0]), + f: () => (args.length === 0 || (/^[a-z]+$/iu).test (args[0])), reason: 'name can only contain [a-z]' }, { @@ -44,7 +44,16 @@ function assert (folder, args) { }, { f: () => (fs.existsSync (folder)), - reason: 'cwd does not exist' + 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) diff --git a/snippets/snippet/template/index.js b/snippets/snippet/template/index.js index f1c156d..545c663 100644 --- a/snippets/snippet/template/index.js +++ b/snippets/snippet/template/index.js @@ -27,15 +27,15 @@ function run (folder, args) { function assert (folder, args) { const tests = [ { - f: () => (args.length === 1), - reason: 'not enough arguments' + f: () => (args.length < 2), + reason: 'too many arguments' }, { - f: () => (typeof args[0] === 'string'), + f: () => (args.length === 0 || typeof args[0] === 'string'), reason: 'name is not a string' }, { - f: () => (/^[a-z]+$/iu).test (args[0]), + f: () => (args.length === 0 || (/^[a-z]+$/iu).test (args[0])), reason: 'name can only contain [a-z]' }, { @@ -44,7 +44,16 @@ function assert (folder, args) { }, { f: () => (fs.existsSync (folder)), - reason: 'cwd does not exist' + 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)