From b6e6522fb95e1608efe1aa7ef5867ca0c75a1e15 Mon Sep 17 00:00:00 2001 From: Jimmy Beldone Date: Fri, 17 Nov 2023 12:09:41 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20better=20setup=20script=20&?= =?UTF-8?q?=20prompts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup/setup.mjs | 61 +++++++++++++++++++++++++++++++++++------- setup/setupPrompts.mjs | 13 ++++++++- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/setup/setup.mjs b/setup/setup.mjs index 8e5c934..48626bb 100644 --- a/setup/setup.mjs +++ b/setup/setup.mjs @@ -1,5 +1,6 @@ /* eslint-disable import/no-extraneous-dependencies */ import chalk from 'chalk'; +import { exec } from 'child_process'; import prompts from 'prompts'; import replace from 'replace'; @@ -37,6 +38,13 @@ const onCancel = () => { const pkgJsonPathPrefix = process.env.MODE === 'test' ? 'setupCopy/' : ''; const setupPath = process.env.MODE === 'test' ? './setupCopy' : './setup'; +// lancer la commande 'git init' +const gitInit = async () => { + const { stderr, stdout } = await exec('git init'); + console.log('stdout:', stdout); + console.log('stderr:', stderr); +}; + // Update package.json const updatePackage = async () => { writeMessage(pkgIntroMesage); @@ -114,7 +122,6 @@ const updatePackage = async () => { // Remove setup dependencies async function cleanDeps() { - const { exec } = await import('child_process'); const { stdout } = await exec('yarn remove chalk prompts replace -D'); console.log('stdout:', stdout); } @@ -126,13 +133,6 @@ const updatePackage = async () => { if (rmError) throw new Error(rmError); }); }); - - // writeMessage(finalMessage); - - // // remove all setup scripts from the 'tools' folder - // rimraf(setupPath, (rmError) => { - // if (rmError) throw new Error(rmError); - // }); }; // Initialize prompt @@ -157,7 +157,50 @@ const updatePackage = async () => { rimraf('.git', (error) => { if (error) throw new Error(error); writeMessage(gitDeleteMessage); - updatePackage(); + + // lance la commande 'git init' + gitInit().then(() => { + // ask for git remote url + prompts( + { + initial: '', + message: + 'Git remote URL (ex: "git@github.com:JimmyBeldone/gatsby-starter-lemonade.git")', + name: 'value', + type: 'text', + }, + { onCancel }, + ).then((res) => { + // set git remote url + exec( + `git remote add origin ${res.value}`, + (err) => { + if (err) { + console.error(err); + return; + } + + exec('git branch -M main', (errBranch) => { + if (errBranch) { + console.error(errBranch); + return; + } + // push to remote + exec( + 'git push -u origin master', + (errPush) => { + if (errPush) { + console.error(errPush); + return; + } + updatePackage(); + }, + ); + }); + }, + ); + }); + }); }); } } else { diff --git a/setup/setupPrompts.mjs b/setup/setupPrompts.mjs index da4f04f..26a8f40 100644 --- a/setup/setupPrompts.mjs +++ b/setup/setupPrompts.mjs @@ -1,7 +1,18 @@ +/* eslint-disable no-underscore-dangle */ +import path from 'path'; +import { fileURLToPath } from 'url'; + +// Obtenir le chemin du fichier actuel +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +// Obtenir le nom du dossier de l'application +const appName = path.basename(path.resolve(__dirname, '..')); + // Define prompts for use with npm 'prompt' module in setup script export default [ { - initial: 'my-new-project', + initial: appName, message: 'Project name', name: 'name', type: 'text',