Skip to content

Commit

Permalink
v1.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
fgarrec0397 committed Sep 3, 2022
1 parent 1170ba8 commit d9db885
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 24 deletions.
4 changes: 3 additions & 1 deletion 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 <your-project-name>` to start a new [Granity app](https://github.com/fgarrec0397/Granity)
115 changes: 93 additions & 22 deletions bin/index.js
Expand Up @@ -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" });
Expand All @@ -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 = () => {
Expand All @@ -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 () => {
Expand All @@ -107,9 +178,9 @@ const handleFinish = async () => {
const init = async () => {
await handleStart();
cloneRepository();
await cleanUpRepo();
cleanUpRepo();
setupFolder();
installDependencies();
await handleFinish();
};

init();
2 changes: 1 addition & 1 deletion 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",
Expand Down

0 comments on commit d9db885

Please sign in to comment.