Skip to content

Commit

Permalink
Generateur de projet
Browse files Browse the repository at this point in the history
  • Loading branch information
Captainfive committed Nov 9, 2018
1 parent 3a51d47 commit 49cc5f7
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 441 deletions.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ typings/

# next.js build output
.next
temp/
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 0 additions & 8 deletions Readme/README.md

This file was deleted.

71 changes: 71 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

// Require Node.js Dependencies
const { readdir, readFile, writeFile } = require("fs").promises;
const { join } = require("path");

// Require Third-party Dependencies
const execa = require("execa");
const inquirer = require("inquirer");

// CONSTANTS
const ROOT_DIR = join(__dirname, "..");
const TEMPLATE_DIR = join(ROOT_DIR, "template");

/**
* @async
* @function main
* @desc Main Generator CLI
* @returns {Promise<void>}
*/
async function main() {
const cwd = process.cwd();
if (cwd === ROOT_DIR || cwd === __dirname) {
throw new Error("Cannot execute at the root of the project");
}

// Create initial package.json
await execa("npm init -y");

// Write default projects files
(await readdir(
join(TEMPLATE_DIR, "defaultFiles")
)).map((file) => writeFile(file));

// Ask projectName and projectDesc
const questions = [
{
message: "What is the name of your project",
type: "input",
name: "projectname"
},
{
message: "Add a description (optional)",
type: "input",
name: "projectdesc"
}
];
const response = await inquirer.prompt(questions);

// Handle Package.json
{
const cwdPackage = join(cwd, "package.json");

const buf = await readFile(cwdPackage);
const obj = JSON.parse(buf.toString());
obj.name = `@slimio/${response.projectname}`;
obj.description = response.projectdesc;

await writeFile(cwdPackage, JSON.stringify(obj, null, 2));
}

// Handle README.md
const buf = await readFile(join(TEMPLATE_DIR, "README.md"));

const finalReadme = buf.toString()
.replace(/\${package}/gm, `@slimio/${response.projectname}`)
.replace(/\${desc}/gm, `${response.projectdesc}`);

await writeFile(join(cwd, "README.md"), finalReadme);
}
main().catch(console.error);
File renamed without changes.
Loading

0 comments on commit 49cc5f7

Please sign in to comment.