diff --git a/src/commands/init.js b/src/commands/init.js index f598573..4ebff08 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -1,3 +1,4 @@ +import { findUp } from 'find-up'; import prompts from 'prompts'; import ora from 'ora'; import { redBright, yellowBright } from 'colorette'; @@ -16,23 +17,51 @@ import { cloneRepo, installDeps } from '../utilities/install.js'; import { editMain } from '../utilities/edits.js'; const { prompt } = prompts; +// TODO make this functional and better! export async function init({ flags }) { if (flags?.includes('y')) { - // TODO make this functional - console.log("I see a flag there! Seems like you're lazy!\nBye!"); + console.log("I see the -y flag there! Seems like you're lazy!\nBye!"); process.exit(0); } - const node = await execa('node', ['--version']); - if (node.stdout.match(/v1(([0-6]\.[2-9])|([0-5]\.[0-9]))/gm)?.length) { + 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` ) ); - return process.exit(1); + process.exit(1); } + const pkg = await findUp('package.json'); + if (!pkg) { + console.log(`No ${redBright('package.json')} found!`); + const npm = await prompt([npmInit]); + if (!npm.npminit) { + console.log( + `${redBright('Failed')} to initialize Sern!` + + '\nMaybe you should run npm init?' + ); + process.exit(1); + } + const spin = ora({ + text: 'Initializing npm...', + spinner: 'aesthetic', + }).start(); + const exee = await execa('npm', ['init', '-y']).catch( + () => null + ); /* .stdout.pipe(process.stdout) */ + await wait(300); + if (!exee || exee?.failed) { + spin.fail( + `${redBright('Failed')} to initialize npm!` + + '\nMaybe you should run npm init?' + ); + process.exit(1); + } + spin.succeed('Npm initialized!'); + } /** * TODO edit main_dir and cmds_dir according to user input as well as default_prefix * will need help @Allyedge @@ -55,8 +84,9 @@ export async function init({ flags }) { `${redBright('Failed')} to initialize git!` + '\nMaybe you should run git init?' ); - return process.exit(1); - } else spin.succeed('Git initialized!'); + process.exit(1); + } + spin.succeed('Git initialized!'); } const pm = await npm(); diff --git a/src/index.js b/src/index.js index 6971622..b7409d2 100644 --- a/src/index.js +++ b/src/index.js @@ -4,9 +4,9 @@ import { init } from './commands/init.js'; import { help } from './commands/help.js'; const regex = /(?<=--|-)\w+/gm; -const flags = process.argv.slice(2).join(' ').match(regex); - const rawArgs = process.argv.slice(2); +const flags = rawArgs.join(' ').match(regex); + const args = rawArgs .join(' ') .trim() diff --git a/src/prompts/init.js b/src/prompts/init.js index 80945c8..56eec0e 100644 --- a/src/prompts/init.js +++ b/src/prompts/init.js @@ -1,8 +1,6 @@ import { blueBright } from 'colorette'; -/** - * @deprecated - */ +// TODO refactor the intents stuff and add more questions const Intents = [ 'DIRECT_MESSAGES', 'DIRECT_MESSAGE_REACTIONS', @@ -24,7 +22,7 @@ const Intents = [ ].map((i, j) => ({ title: i, value: j, short: `${j}` })); //! bad way export const lang = { - message: 'What language you want the project to be in?', + message: 'What language do you want the project to be in?', name: 'lang', type: 'select', choices: [ @@ -57,7 +55,7 @@ export const intent = { export const default_prefix = { message: - 'What is the default prefix for your bot? Type "none" if it is completely based on Application Commands', + 'What is the default prefix for your bot? Type "none" if it is completely Application-Command based', name: 'prefix', type: 'text', @@ -68,15 +66,12 @@ export const token = { message: 'What is your bot token? Type "no" to skip', name: 'token', type: 'password', - validate: (/** @type {string} */ token) => { if (token === 'no') return true; - return token.match( - /(?mfa\.[a-z0-9_-]{20,})|(?[a-z0-9_-]{23,28}\.[a-z0-9_-]{6,7}\.[a-z0-9_-]{27})/i - )?.length + return (/(?mfa\.[a-z0-9_-]{20,})|(?[a-z0-9_-]{23,28}\.[a-z0-9_-]{6,7}\.[a-z0-9_-]{27})/i).test(token) ? true - : 'Invalid token'; - }, + : 'Invalid token' + } }; export const main_dir = { @@ -95,13 +90,15 @@ export const cmds_dir = { dir === 'src' ? 'You can not use src as a directory' : true, }; -export const projectInit = { - name: 'projectinit', +/** + * @type {import('prompts').PromptObject} + */ +export const npmInit = { + name: 'npm_init', type: 'confirm', - message: `Do you want to ${blueBright('me')} to initialize project?`, + 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 ec5cb17..8bc5fa8 100644 --- a/src/utilities/edits.js +++ b/src/utilities/edits.js @@ -19,4 +19,9 @@ export async function editMain(name) { const result = () => writeFile(pjLocation, JSON.stringify(output, null, 2)); return result(); + if (!output) throw new Error("Can't read your package.json."); + + output.name = name; + + return writeFile(pjLocation, JSON.stringify(output, null, 2)); }