diff --git a/src/commands/init.js b/src/commands/init.js index a574ef3..2d2d486 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -17,14 +17,8 @@ import { editDirs, editMain } from '../utilities/edits.js'; const { prompt } = prompts; export async function init({ flags }) { - if (flags?.includes('y')) { - // TODO for @Allyedge: make this functional - console.log("I see the -y flag there! Seems like you're lazy!\nBye!"); - process.exit(0); - } - + // * Check if node version is valid const node = await execa('node', ['--version']); - if (/v1(([0-6]\.[2-9])|([0-5]\.[0-9]))/gm.test(node.stdout)) { console.log( yellowBright( @@ -35,38 +29,32 @@ export async function init({ flags }) { process.exit(1); } - const data = await prompt([name, lang, main_dir, cmds_dir, default_prefix]); + let data; + let git_init; + let pm; + + if (flags?.includes('y')) { + const projectName = await prompt([name]); + git_init = true; + pm = 'npm'; + data = { + name: projectName.name, + lang: 'typescript', + main_dir: 'src', + cmds_dir: 'commands', + default_prefix: '!', + }; + } else { + data = await prompt([name, lang, main_dir, cmds_dir, default_prefix]); + git_init = (await prompt([gitInit])).gitinit; + await npm(); + } 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 { - const spin = ora({ - 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!` + - '\nMaybe you should run git init?' - ); - process.exit(1); - } - - spin.succeed('Git initialized!'); - } - - const pm = await npm(); + git_init ? await git(data) : console.log(`Skipping git init...\n`); let choice = ''; @@ -76,12 +64,35 @@ export async function init({ flags }) { } else choice = pm; await installDeps(choice, data.name); - await editMain(data.name); - await editDirs(data.main_dir, data.cmds_dir, data.name); } +/** + * It initializes git + * @param data - The data object that contains the name of the project. + */ +async function git(data) { + const spin = ora({ + 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!` + + '\nMaybe you should run git init?' + ); + process.exit(1); + } + + spin.succeed('Git initialized!'); +} + /** * Wait for a specified number of milliseconds, then return a promise that resolves to undefined. * @param {number} ms - The number of milliseconds to wait. diff --git a/src/prompts/init.js b/src/prompts/init.js index 2577528..824f10c 100644 --- a/src/prompts/init.js +++ b/src/prompts/init.js @@ -49,7 +49,7 @@ export const cmds_dir = { export const npmInit = { name: 'npm_init', type: 'confirm', - message: `Do you want to ${blueBright('me')} to initialize npm?`, + message: `Do you want ${blueBright('me')} to initialize npm?`, initial: true, }; diff --git a/src/utilities/npm.js b/src/utilities/npm.js index 2f73a6c..1e6706e 100644 --- a/src/utilities/npm.js +++ b/src/utilities/npm.js @@ -7,14 +7,18 @@ import { execa } from 'execa'; export async function npm() { const npm = await execa('npm', ['-v']); const npm_version = npm?.stdout; + const yarn = await execa('yarn', ['-v']); const yarn_version = yarn?.stdout; + if (npm_version && !yarn_version) { return 'npm'; } + if (!npm_version && yarn_version) { return 'yarn'; } + if (npm_version && yarn_version) { return 'both'; }