feat: add default settings for the -y flag (#19)

* feat: add default settings for the -y flag

Signed-off-by: Allyedge <alimarslank@gmail.com>

* refactor: add documentation comments to initProject

* feat: set git init to true and package manager to npm by default

* style: fix typo in npmInit

* Update src/commands/init.js

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>

* Update src/commands/init.js

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>

* refactor: remove unnecessary isDefault checks

* refactor(init): cleaned up code and made a simple flow

* docs: update jsdoc

* style: fix prettier

* feat: set git init to true and package manager to npm by default

* style: fix typo in npmInit

* Update src/commands/init.js

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>

* Update src/commands/init.js

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>

* feat: add default settings for the -y flag

Signed-off-by: Allyedge <alimarslank@gmail.com>

* refactor: add documentation comments to initProject

* feat: set git init to true and package manager to npm by default

* Update src/commands/init.js

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>

* Update src/commands/init.js

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>

* refactor: remove unnecessary isDefault checks

* refactor(init): cleaned up code and made a simple flow

* docs: update jsdoc

* style: fix prettier

* feat: add default settings for the -y flag

Signed-off-by: Allyedge <alimarslank@gmail.com>

* refactor: add documentation comments to initProject

* feat: set git init to true and package manager to npm by default

* refactor: remove unnecessary isDefault checks

* refactor(init): cleaned up code and made a simple flow

* docs: update jsdoc

* style: fix prettier

* chore(s): stuff

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>
This commit is contained in:
Allyedge
2022-05-13 07:39:35 +02:00
committed by GitHub
parent 3c5924b409
commit e5f607e998
3 changed files with 52 additions and 37 deletions

View File

@@ -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.

View File

@@ -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,
};

View File

@@ -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';
}