From d9db88506b0f200f24bb96b6e2f0cade57936eaf Mon Sep 17 00:00:00 2001 From: fga97 Date: Sat, 3 Sep 2022 11:47:33 -0400 Subject: [PATCH] v1.1.0 release --- README.md | 4 +- bin/index.js | 115 +++++++++++++++++++++++++++++++++++++++++---------- package.json | 2 +- 3 files changed, 97 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 78a6d72..f5631a0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # create-granity-app -Create a Granity project with one line command +Create a Granity project with one line command. + +Run `npx create-granity-app ` to start a new [Granity app](https://github.com/fgarrec0397/Granity) diff --git a/bin/index.js b/bin/index.js index 77c89fb..6100c2a 100644 --- a/bin/index.js +++ b/bin/index.js @@ -7,22 +7,35 @@ import latestVersion from "latest-version"; const gitRepo = "https://github.com/fgarrec0397/Granity.git"; const projectName = process.argv[2]; const gitCheckoutCommand = `git clone --depth 1 ${gitRepo} ${projectName}`; -const installDepsCommand = `cd ${projectName} && npm install && cd app && npm install && cd ../server && npm install` +const installDepsCommand = `cd ${projectName} && npm install && cd app && npm install && cd ../server && npm install`; const itemsToDelete = [ + ".git", + ".github", "CODE_OF_CONDUCT.md", "CONTRIBUTING.md", "LICENSE", - ".git", - ".github", ] +/** + * <------------------------------- UTILITIES -------------------------------> + */ + +/** + * + * @param {string} text + */ const displayMessage = (text) => { console.log(); console.log(text); console.log(); } +/** + * + * @param {string} command The command to run + * @returns {boolean} Returns true if the command has been executed with success + */ const runCommand = command => { try { execSync(`${command}`, { stdio: "inherit" }); @@ -35,34 +48,70 @@ const runCommand = command => { return true; }; -const deleteItem = async (dir, logDeletedItem) => { - await fs.rm(dir, { recursive: true }, err => { +/** + * + * @param {string} item Item to delete, can be a file or a folder + * @param {boolean} logDeletedItem If set to true, it will log the deleted item + */ +const deleteItem = (item, logDeletedItem) => { + fs.rm(item, { recursive: true }, err => { if (err) { throw err } if (logDeletedItem) { - console.log(`${dir} is deleted!`) + console.log(`${item} is deleted!`) } }) } -const deleteItems = async (items, logDeletedItem) => { - await items.forEach(async x => { - await deleteItem(`${projectName}/${x}`, logDeletedItem); +/** + * + * @param {string[]} items Array of items to delete, can be a file, a folder or both + * @param {boolean} logDeletedItem If set to true, it will log the deleted item + */ +const deleteItems = (items, logDeletedItem) => { + items.forEach(x => { + deleteItem(`${projectName}/${x}`, logDeletedItem); }); } +/** + * Exit the process + * @param {boolean} withError If set to true, exit the process with error + */ const exit = (withError) => { displayMessage("Process has terminated.") process.exit(withError ? 1 : 0); }; +/** + * A function to create the package.json file + * @TODO Could extract the write function to keep this function more generic + */ +const createPackageJsonFile = () => { + const packageJSONfileTemplate = `{\n \"name\": \"${projectName}\",\n \"version\": \"1.0.0\",\n \"description\": \"My cool new game!\",\n \"scripts\": {\n \"server\": \"cd server && npm start\",\n \"client\": \"cd app && npm start\",\n \"dev\": \"concurrently \\\"npm run server\\\" \\\"npm run client\\\"\"\n },\n \"author\": \"\",\n \"license\": \"MIT\",\n \"devDependencies\": {\n \"concurrently\": \"^7.3.0\"\n }\n}\n`; + + fs.writeFile(`${projectName}/package.json`, packageJSONfileTemplate, 'utf8', function (err) { + if (err) { + console.log("An error occured while writing JSON Object to File."); + return console.log(err); + } + + displayMessage("package.json file created.") + }); + +} + + +/** + * <------------------------------- CLI ACTIONS -------------------------------> + */ + const handleStart = async () => { - const granityCLILastVersion = await latestVersion("granity"); + const granityCLILastVersion = await latestVersion("create-granity-app"); displayMessage(`Welcome to create-granity-app!`); displayMessage(`You are currently running the v${granityCLILastVersion} CLI runner.`); - displayMessage("Enjoy the ride, you're almost there..."); }; const cloneRepository = () => { @@ -75,20 +124,42 @@ const cloneRepository = () => { } } -const cleanUpRepo = async () => { +const cleanUpRepo = () => { displayMessage("Hold on! Just removing our crap for you..."); - await deleteItems(itemsToDelete); + deleteItems(itemsToDelete); +} + +const setupFolder = () => { + createPackageJsonFile(); + const templateReadMeContent = fs.readFileSync(`${projectName}/TEMPLATE_README.md`, 'utf-8'); + + fs.writeFile(`${projectName}/README.md`, templateReadMeContent, 'utf8', function (err) { + if (err) { + console.log("An error occured while writing JSON Object to File."); + return console.log(err); + } + + displayMessage("README file setup."); + }); + + deleteItem(`${projectName}/TEMPLATE_README.md`); } const installDependencies = () => { - displayMessage(`Installing dependencies for ${projectName}`); - - const installingCommand = runCommand(installDepsCommand); - - if (!installingCommand) { - exit(true); - } + // I know this is shit, but it will be fixed in a near future when I will have time + setTimeout(async () => { + displayMessage(`Installing dependencies for ${projectName}`); + + const installingCommand = runCommand(installDepsCommand); + + if (!installingCommand) { + exit(true); + } + + // When the setTimeout issue is fixed, move this down to the init function + await handleFinish(); + }, 1000) }; const handleFinish = async () => { @@ -107,9 +178,9 @@ const handleFinish = async () => { const init = async () => { await handleStart(); cloneRepository(); - await cleanUpRepo(); + cleanUpRepo(); + setupFolder(); installDependencies(); - await handleFinish(); }; init(); diff --git a/package.json b/package.json index 2c08cf4..aed47a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "create-granity-app", - "version": "1.0.40", + "version": "1.1.0", "description": "Create a Granity project with one line command.", "main": "index.js", "type": "module",