Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add some extra options to the script #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"bracketSpacing": true,
"printWidth": 100,
"semi": false,
"singleQuote": true,
"trailingComma": "all"
}
113 changes: 90 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,73 @@
import { execSync } from "child_process";
import { join } from "path";
import { rmSync } from "fs";
import readline from "readline";
import chalk from "chalk";
import os from "os";

const commandSilencer = os.platform() === "win32" ? '> nul 2>&1' : '> /dev/null 2>&1'

const repoUrls = {
barebones: "https://github.com/BootNodeDev/dAppBooster.git",
example: "https://github.com/BootNodeDev/dAppBoosterLandingPage.git",
};

const repoUrl = "https://github.com/bootnodedev/dappbooster.git";
const projectName = process.argv[2];

// Check if the project name is valid
if (!projectName || !/^[a-zA-Z0-9-_]+$/.test(projectName)) {
console.error("Invalid directory name. Please enter a valid project name.");
console.error(`${chalk.red.bold("Invalid directory name. Please enter a valid project name.")}`);
process.exit(1);
}

cloneRepo(projectName);
// Prompt the user to select the repository type
promptUserForRepoType()
.then((repoType) => {
const repoUrl = repoUrls[repoType];
cloneRepo(projectName, repoUrl);
})
.catch((error) => {
console.error(chalk.red.bold("Error:"), error.message);
process.exit(1);
});

/**
* @description Prompt the user to select the repository type
* @returns {Promise<string>} The selected repository type
*/
function promptUserForRepoType() {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.question(
`You can choose to start with a ${chalk.green.bold('Barebones')} project or a project with ${chalk.green.bold('Examples')} and demo code (B/e):`,
(answer) => {
rl.close();
const selection = answer.trim().toUpperCase();
if (selection === "B" || selection === "") {
resolve("barebones");
} else if (selection === "E") {
resolve("example");
} else {
reject(new Error("Invalid selection. Please choose B or E."));
}
}
);
});
}

/**
* @description Get the latest tag in the repository
* @param {Object} execOptions The options to pass to the exec
* @returns {string} The latest tag in the repository
*/
function getLatestTag(execOptions) {
// Fetch all tags
execSync(`git fetch --tags ${commandSilencer}`, execOptions);

function getLatestTag() {
// Get all tags, sorted by version
const tags = execSync("git tag -l --sort=-v:refname")
.toString()
Expand All @@ -26,7 +80,13 @@ function getLatestTag() {
return tags[0];
}

function cloneRepo(projectName) {
/**
* @description Clone the specified repository
* @param {string} projectName The name of the project
* @param {string} repoUrl The URL of the repository
* @returns {void}
*/
function cloneRepo(projectName, repoUrl) {
const sanitizedProjectName = projectName.replace(/[^a-zA-Z0-9-_]/g, "-");
const projectDir = join(process.cwd(), sanitizedProjectName);
const execOptions = {
Expand All @@ -35,7 +95,7 @@ function cloneRepo(projectName) {
};

try {
console.log(`Cloning dAppBooster...`);
console.log(`\nCloning dAppBooster...`);

// Clone the repository
execSync(
Expand All @@ -46,43 +106,50 @@ function cloneRepo(projectName) {
// Change to the project directory
process.chdir(projectDir);

// Fetch all tags
execSync("git fetch --tags", execOptions);

// Get the latest tag
const latestTag = getLatestTag();

if (!latestTag) {
throw new Error("No tags found in the repository");
if (latestTag) {
console.log(`Checking out latest tag: ${latestTag}`);
execSync(`git checkout "${latestTag}"`, execOptions);
} else {
console.log(`No tags found, checking out ${chalk.bold('main')} branch...`);
execSync("git checkout main", execOptions);
}

// Checkout the latest tag
execSync(`git checkout "${latestTag}"`, execOptions);

// Remove the .git directory
rmSync(join(projectDir, ".git"), { recursive: true, force: true });

// Initialize a new git repository
execSync("git init", execOptions);

console.log(`dAppBooster repository cloned in ${chalk.bold(projectDir)}`);
console.log(`Latest version: ${chalk.bold(latestTag)}`);
// Success
console.log(`\ndAppBooster repository cloned in ${chalk.bold(projectDir)}`);
if (latestTag) {
console.log(`Latest version: ${chalk.bold(latestTag)}`);
}

console.log(`\n---\n`);
// Some extra instructions for the user
console.log(`
${chalk.green.bold(
"You can now start your project with the following commands:"
)}
${chalk.blue.bold("You can now start your project with the following commands:")}

${chalk.blue("# Change to the project directory")}
${chalk.gray.italic("# Change to the project directory")}
$ ${chalk.cyan(`cd ${sanitizedProjectName}`)}

${chalk.blue("# Install dependencies")}
${chalk.gray.italic("# Install dependencies")}
$ ${chalk.cyan("pnpm install")}

${chalk.blue("# Copy the example environment file")}
${chalk.gray.italic("# Copy the example environment file")}
$ ${chalk.cyan("cp .env.example .env.local")}

${chalk.blue("# Start the development server")}
${chalk.gray.italic("# Start the development server")}
$ ${chalk.cyan("pnpm dev")}

---

Remember to also check out the docs in ${chalk.green.bold("https://docs.dappbooster.dev/")}

`);
} catch (error) {
console.error(`${chalk.bold.red("An error occurred:")}`, error.message);
Expand Down
31 changes: 0 additions & 31 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dappbooster",
"version": "0.3.3",
"version": "0.3.4",
"description": "Script to easily start your project with dAppBooster",
"main": "index.js",
"bin": {
Expand Down
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.