From b24a053d1cb8c00d49a96b6d536dd17205b9fa0e Mon Sep 17 00:00:00 2001 From: Evo <85353424+EvolutionX-10@users.noreply.github.com> Date: Thu, 7 Jul 2022 21:22:49 +0530 Subject: [PATCH] feat(init): add --sync flag (#53) --- src/commands/init.js | 21 ++++++++++++++++++--- src/index.js | 1 + 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/commands/init.js b/src/commands/init.js index e62413e..3eb19cf 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -19,6 +19,9 @@ import { editDirs, editMain } from '../utilities/edits.js'; import { writeFile } from 'fs/promises'; const { prompt } = prompts; +/** + * @param {{ y: string; sync: string; }} flags + */ export async function init(flags) { // * Check if node version is valid const node = await execa('node', ['--version']); @@ -46,6 +49,8 @@ export async function init(flags) { main_dir: 'src', cmds_dir: 'commands', }; + } else if (flags.sync) { + data = await prompt([lang, main_dir, cmds_dir]); } else { data = await prompt([name, lang, main_dir, cmds_dir]); git_init = (await prompt([gitInit])).gitinit; @@ -59,22 +64,32 @@ export async function init(flags) { commands: data.cmds_dir, }, }; + const file = JSON.stringify(config, null, 2); - if (Object.keys(data).length < 4) process.exit(1); + const requiredData = flags.sync !== undefined ? 3 : 4; + const receivedData = Object.keys(data).length; + const hasRequiredData = receivedData < requiredData; - await cloneRepo(data.lang, data.name); + if (hasRequiredData) process.exit(1); + + if (!flags.sync) await cloneRepo(data.lang, data.name); const pkg = await findUp('package.json', { cwd: process.cwd() + '/' + data.name, }); - if (!pkg) throw new Error('No package.json found! Clone Failed'); + if (!pkg) throw new Error('No package.json found!'); if (pkg) { await writeFile(pkg.replace('package.json', 'sern.config.json'), file); } + if (flags.sync) { + console.log('Project was successfully synced!'); + process.exit(0); + } + git_init ? await git(data) : console.log(`Skipping git init...\n`); let choice; diff --git a/src/index.js b/src/index.js index 20920f4..553df19 100644 --- a/src/index.js +++ b/src/index.js @@ -19,6 +19,7 @@ program .command(init.name) .description('Quickest way to scaffold a new project') .option('-y', 'Finishes setup as default') + .option('-s, --sync', 'Syncs the project and generates sern.config.json') .action(init); program