From bfe21818cc877697686f3cc7b2aac413425505d0 Mon Sep 17 00:00:00 2001 From: Allyedge Date: Wed, 11 May 2022 15:20:01 +0200 Subject: [PATCH] refactor: improve the spacing between code lines --- dumpfiles.js | 2 +- src/commands/help.js | 2 +- src/commands/init.js | 13 +++++++++++++ src/index.js | 1 + src/prompts/init.js | 2 +- src/utilities/edits.js | 1 + src/utilities/install.js | 14 ++++++++++++++ 7 files changed, 32 insertions(+), 3 deletions(-) diff --git a/dumpfiles.js b/dumpfiles.js index 9e996d5..5c5b9f4 100644 --- a/dumpfiles.js +++ b/dumpfiles.js @@ -1,4 +1,4 @@ -//! message for sern devs: ignore this!!! +//! Message for Sern CLI developers. Please ignore this file. const SpinnerName = [ 'dots', diff --git a/src/commands/help.js b/src/commands/help.js index b808402..21877f5 100644 --- a/src/commands/help.js +++ b/src/commands/help.js @@ -1,3 +1,3 @@ export function help() { - console.log('This is sern cli help section\n\n' + 'Fill me up later!'); + console.log('This is the Sern CLI help section.\n\n' + 'Fill me up later!'); } diff --git a/src/commands/init.js b/src/commands/init.js index 598c338..627ac44 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -25,12 +25,14 @@ export async function init({ flags }) { } const node = await execa('node', ['--version']); + if (/v1(([0-6]\.[2-9])|([0-5]\.[0-9]))/gm.test(node.stdout)) { console.log( yellowBright( `\nYou are using Node ${node.stdout}\nPlease upgrade to Node 16.10.x or higher!\n` ) ); + process.exit(1); } @@ -39,7 +41,9 @@ export async function init({ flags }) { if (Object.keys(data).length < 5) process.exit(1); await cloneRepo(data.lang, data.name); + const git_init = await prompt([gitInit]); + if (!git_init.gitinit) { console.log(`\nAlright\n`); } else { @@ -47,8 +51,11 @@ export async function init({ flags }) { text: 'Initializing git...', spinner: 'aesthetic', }).start(); + const exe = await execa('git', ['init', data.name]); + await wait(300); + if (!exe || exe?.failed) { spin.fail( `${redBright('Failed')} to initialize git!` + @@ -56,17 +63,23 @@ export async function init({ flags }) { ); process.exit(1); } + spin.succeed('Git initialized!'); } const pm = await npm(); + let choice = ''; + if (pm === 'both') { const chosen = await prompt([which_manager]); choice = chosen.manager; } else choice = pm; + await installDeps(choice, data.name); + await editMain(data.name); + await editDirs(data.main_dir, data.cmds_dir, data.name); } diff --git a/src/index.js b/src/index.js index b7409d2..794aa3a 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ const args = rawArgs .filter((e) => !/(--|-)\w+/gm.test(e)); const cmdName = args[0]; + const commands = new Map([ ['help', help], ['', help], diff --git a/src/prompts/init.js b/src/prompts/init.js index 414600f..2577528 100644 --- a/src/prompts/init.js +++ b/src/prompts/init.js @@ -22,7 +22,6 @@ export const lang = { export const default_prefix = { message: 'What is the default prefix for your bot? Type "none" if it is completely Application-Command based', - name: 'prefix', type: 'text', initial: '!', @@ -53,6 +52,7 @@ export const npmInit = { message: `Do you want to ${blueBright('me')} to initialize npm?`, initial: true, }; + export const gitInit = { name: 'gitinit', type: 'confirm', diff --git a/src/utilities/edits.js b/src/utilities/edits.js index c7fbb34..484cb1f 100644 --- a/src/utilities/edits.js +++ b/src/utilities/edits.js @@ -51,6 +51,7 @@ export async function editDirs( const tsconfig = await findUp('tsconfig.json', { cwd: process.cwd() + '/' + name, }); + if (tsconfig) { const output = JSON.parse(await readFile(tsconfig, 'utf8')); if (!output) throw new Error("Can't read your tsconfig.json."); diff --git a/src/utilities/install.js b/src/utilities/install.js index d25d0bb..f43c775 100644 --- a/src/utilities/install.js +++ b/src/utilities/install.js @@ -16,18 +16,26 @@ export async function installDeps(choice, name) { const pkg = await findUp('package.json', { cwd: process.cwd() + '/' + name, }); + if (!pkg) throw new Error('No package.json found!'); + const output = JSON.parse(await readFile(pkg, 'utf8')); + if (!output) throw new Error("Can't read file."); + const deps = output.dependencies; + if (!deps) throw new Error("Can't find dependencies."); + const spin = ora({ text: `Installing dependencies...`, spinner: 'aesthetic', }).start(); + const result = await execa(choice, ['install'], { cwd: process.cwd() + '/' + name, }).catch(() => null); + if (!result || result?.failed) { spin.fail(`${redBright('Failed')} to install dependencies!`); return process.exit(1); @@ -44,7 +52,9 @@ export async function cloneRepo(lang, name) { 'clone', `https://github.com/sern-handler/templates.git`, // ? See the idea of @Allyedge having templates built in cli ]); + copyRecursiveSync(`templates/templates/${lang}`, name); + fs.rmSync(`templates/`, { recursive: true, force: true }); } @@ -57,10 +67,14 @@ export async function cloneRepo(lang, name) { */ function copyRecursiveSync(src, dest) { var exists = fs.existsSync(src); + var stats = exists && fs.statSync(src); + var isDirectory = exists && stats.isDirectory(); + if (isDirectory) { fs.mkdirSync(dest); + fs.readdirSync(src).forEach(function (childItemName) { copyRecursiveSync( path.join(src, childItemName),