Skip to content

Commit

Permalink
New release with init command
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Best-Codes committed Jan 6, 2025
1 parent cab1a98 commit adc1d22
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 25 deletions.
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
# discraft

To install dependencies:
For now, see:
https://github.com/The-Best-Codes/discraft-js/blob/8947955b1e719599ac99ee62746ebb7e3b1275e6/README.md

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.42. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
This package is being redone.
32 changes: 23 additions & 9 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
"workspaces": {
"": {
"dependencies": {
"commander": "latest",
"consola": "latest",
"esbuild": "latest",
"esbuild-node-externals": "latest",
"glob": "latest",
"inquirer": "latest",
"commander": "^13.0.0",
"consola": "^3.3.3",
"esbuild": "^0.24.2",
"esbuild-node-externals": "^1.16.0",
"fs-extra": "^11.2.0",
"glob": "^11.0.0",
"inquirer": "^12.3.0",
},
"devDependencies": {
"@types/bun": "latest",
"bun": "latest",
"@types/bun": "^1.1.14",
"@types/fs-extra": "^11.0.4",
"bun": "^1.1.42",
},
"peerDependencies": {
"typescript": "latest",
"typescript": "^5.7.2",
},
},
},
Expand Down Expand Up @@ -124,6 +126,10 @@

"@types/bun": ["@types/bun@1.1.14", "", { "dependencies": { "bun-types": "1.1.37" } }, "sha512-opVYiFGtO2af0dnWBdZWlioLBoxSdDO5qokaazLhq8XQtGZbY4pY3/JxY8Zdf/hEwGubbp7ErZXoN1+h2yesxA=="],

"@types/fs-extra": ["@types/fs-extra@11.0.4", "", { "dependencies": { "@types/jsonfile": "*", "@types/node": "*" } }, "sha512-yTbItCNreRooED33qjunPthRcSjERP1r4MqCZc7wv0u2sUkzTFp45tgUfS5+r7FrZPdmCCNflLhVSP/o+SemsQ=="],

"@types/jsonfile": ["@types/jsonfile@6.1.4", "", { "dependencies": { "@types/node": "*" } }, "sha512-D5qGUYwjvnNNextdU59/+fI+spnwtTFmyQP0h+PfIOSkNfpU6AOICUOkm4i0OnSk+NyjdPJrxCDro0sJsWlRpQ=="],

"@types/node": ["@types/node@20.12.14", "", { "dependencies": { "undici-types": "~5.26.4" } }, "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg=="],

"@types/ws": ["@types/ws@8.5.13", "", { "dependencies": { "@types/node": "*" } }, "sha512-osM/gWBTPKgHV8XkTunnegTRIsvF6owmf5w+JtAfOw472dptdm0dlGv4xCt6GwQRcC2XVOvvRE/0bAoQcL2QkA=="],
Expand Down Expand Up @@ -170,8 +176,12 @@

"foreground-child": ["foreground-child@3.3.0", "", { "dependencies": { "cross-spawn": "^7.0.0", "signal-exit": "^4.0.1" } }, "sha512-Ld2g8rrAyMYFXBhEqMz8ZAHBi4J4uS1i/CxGMDnjyFWddMXLVcDp051DZfu+t7+ab7Wv6SMqpWmyFIj5UbfFvg=="],

"fs-extra": ["fs-extra@11.2.0", "", { "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", "universalify": "^2.0.0" } }, "sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw=="],

"glob": ["glob@11.0.0", "", { "dependencies": { "foreground-child": "^3.1.0", "jackspeak": "^4.0.1", "minimatch": "^10.0.0", "minipass": "^7.1.2", "package-json-from-dist": "^1.0.0", "path-scurry": "^2.0.0" }, "bin": { "glob": "dist/esm/bin.mjs" } }, "sha512-9UiX/Bl6J2yaBbxKoEBRm4Cipxgok8kQYcOPEhScPwebu2I0HoQOuYdIO6S3hLuWoZgpDpwQZMzTFxgpkyT76g=="],

"graceful-fs": ["graceful-fs@4.2.11", "", {}, "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ=="],

"iconv-lite": ["iconv-lite@0.4.24", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3" } }, "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA=="],

"inquirer": ["inquirer@12.3.0", "", { "dependencies": { "@inquirer/core": "^10.1.2", "@inquirer/prompts": "^7.2.1", "@inquirer/type": "^3.0.2", "ansi-escapes": "^4.3.2", "mute-stream": "^2.0.0", "run-async": "^3.0.0", "rxjs": "^7.8.1" }, "peerDependencies": { "@types/node": ">=18" } }, "sha512-3NixUXq+hM8ezj2wc7wC37b32/rHq1MwNZDYdvx+d6jokOD+r+i8Q4Pkylh9tISYP114A128LCX8RKhopC5RfQ=="],
Expand All @@ -182,6 +192,8 @@

"jackspeak": ["jackspeak@4.0.2", "", { "dependencies": { "@isaacs/cliui": "^8.0.2" } }, "sha512-bZsjR/iRjl1Nk1UkjGpAzLNfQtzuijhn2g+pbZb98HQ1Gk8vM9hfbxeMBP+M2/UUdwj0RqGG3mlvk2MsAqwvEw=="],

"jsonfile": ["jsonfile@6.1.0", "", { "dependencies": { "universalify": "^2.0.0" }, "optionalDependencies": { "graceful-fs": "^4.1.6" } }, "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ=="],

"locate-path": ["locate-path@6.0.0", "", { "dependencies": { "p-locate": "^5.0.0" } }, "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw=="],

"lru-cache": ["lru-cache@11.0.2", "", {}, "sha512-123qHRfJBmo2jXDbo/a5YOQrJoHF/GNQTLzQ5+IdK5pWpceK17yRc6ozlWd25FxvGKQbIUs91fDFkXmDHTKcyA=="],
Expand Down Expand Up @@ -236,6 +248,8 @@

"undici-types": ["undici-types@5.26.5", "", {}, "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="],

"universalify": ["universalify@2.0.1", "", {}, "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw=="],

"which": ["which@2.0.2", "", { "dependencies": { "isexe": "^2.0.0" }, "bin": { "node-which": "./bin/node-which" } }, "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA=="],

"wrap-ansi": ["wrap-ansi@6.2.0", "", { "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", "strip-ansi": "^6.0.0" } }, "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA=="],
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discraft",
"version": "1.6.2-beta.3",
"version": "1.6.2-beta.4",
"description": "Ultimate Discord bot framework",
"type": "module",
"packageManager": "bun@1.1.42",
Expand All @@ -22,6 +22,7 @@
],
"files": [
"package/dist",
"templates",
"README.md",
"LICENSE"
],
Expand All @@ -33,6 +34,7 @@
"homepage": "https://github.com/The-Best-Codes/discraft-js#readme",
"devDependencies": {
"@types/bun": "^1.1.14",
"@types/fs-extra": "^11.0.4",
"bun": "^1.1.42"
},
"peerDependencies": {
Expand All @@ -43,7 +45,8 @@
"consola": "^3.3.3",
"esbuild": "^0.24.2",
"esbuild-node-externals": "^1.16.0",
"fs-extra": "^11.2.0",
"glob": "^11.0.0",
"inquirer": "^12.3.0"
}
}
}
11 changes: 11 additions & 0 deletions package/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { program } from "commander";
import consola from "consola";
import { version } from "../../package.json";
import { build } from "./cli/build";
import { init } from "./cli/init";
import { start } from "./cli/start";

program
Expand Down Expand Up @@ -38,4 +39,14 @@ program
});
});

program
.command("init")
.description("Initialize a new Discraft project")
.action(() => {
init().catch((error) => {
consola.error("An error occurred during initialization:", error);
process.exit(1);
});
});

program.parse(process.argv);
149 changes: 149 additions & 0 deletions package/src/cli/init/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { execSync } from "child_process";
import consola from "consola";
import fs from "fs-extra";
import inquirer from "inquirer";
import path from "path";

async function detectPackageManager(): Promise<string | undefined> {
const userAgent = process.env.npm_config_user_agent;
if (userAgent) {
if (userAgent.startsWith("bun")) {
return "bun";
}
if (userAgent.startsWith("npm")) {
return "npm";
}
if (userAgent.startsWith("yarn")) {
return "yarn";
}
if (userAgent.startsWith("pnpm")) {
return "pnpm";
}
}

try {
execSync("bun --version", { stdio: "ignore" });
return "bun";
} catch (e) {}
try {
execSync("npm --version", { stdio: "ignore" });
return "npm";
} catch (e) {}
try {
execSync("yarn --version", { stdio: "ignore" });
return "yarn";
} catch (e) {}
try {
execSync("pnpm --version", { stdio: "ignore" });
return "pnpm";
} catch (e) {}

return undefined;
}

async function copyTemplate(templatePath: string, projectPath: string) {
const files = [
"clients",
"commands",
"events",
"utils",
".env.example",
".gitignore",
"index.ts",
"package.json",
"tsconfig.json",
];

for (const file of files) {
const sourcePath = path.join(templatePath, file);
const destPath = path.join(projectPath, file);

try {
if (fs.lstatSync(sourcePath).isDirectory()) {
await fs.copy(sourcePath, destPath);
} else {
await fs.copyFile(sourcePath, destPath);
}
} catch (e) {
consola.error(`Failed to copy ${sourcePath} to ${destPath}`, e);
throw new Error(`Failed to copy template files, ${e}`);
}
}
}

async function init() {
const currentWorkingDirectory = process.cwd();
const packageRoot = path.join(__dirname, "..", "..", ".."); //navigate to package root from here
const templatePath = path.join(packageRoot, "templates", "ts");

const useCurrentDir = await inquirer.prompt([
{
type: "confirm",
name: "useCurrent",
message: "Initialize a project in the current directory?",
default: true,
},
]);

let projectDir = currentWorkingDirectory;

if (!useCurrentDir.useCurrent) {
const projectDirResponse = await inquirer.prompt([
{
type: "input",
name: "projectName",
message: "What is your project directory?",
default: "my-project",
},
]);
projectDir = path.join(
currentWorkingDirectory,
projectDirResponse.projectName,
);
}
const packageManager = await detectPackageManager();

const packageManagerResponse = await inquirer.prompt([
{
type: "list",
name: "packageManager",
message: "Select a package manager to install dependencies:",
choices: [
{ name: "npm", value: "npm" },
{ name: "yarn", value: "yarn" },
{ name: "pnpm", value: "pnpm" },
{ name: "bun", value: "bun" },
{ name: "Don't install dependencies", value: "none" },
],
default: packageManager ?? "none",
},
]);

consola.info(`Initializing project in ${projectDir}...`);

try {
await fs.ensureDir(projectDir);
await copyTemplate(templatePath, projectDir);
consola.success("Copied template files successfully.");

if (packageManagerResponse.packageManager !== "none") {
consola.info(
`Installing dependencies with ${packageManagerResponse.packageManager}...`,
);
execSync(`${packageManagerResponse.packageManager} install`, {
cwd: projectDir,
stdio: "inherit",
});
consola.success("Installed dependencies.");
}

consola.success("Project initialized successfully!");
consola.info(
`Go to ${path.relative(currentWorkingDirectory, projectDir)} and run "${packageManagerResponse.packageManager} run build" and "${packageManagerResponse.packageManager} run start" to get started.`,
);
} catch (error: any) {
consola.error(`Failed to initialize project: ${error.message}`);
}
}

export { init };
9 changes: 9 additions & 0 deletions templates/ts/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dist
.discraft
node_modules
.env
bun.lockb
bun.lock
package-lock.json
yarn.lock
pnpm-lock.yaml
4 changes: 2 additions & 2 deletions templates/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"build": "discraft build"
},
"devDependencies": {
"discraft": "1.6.2-beta.3"
"discraft": "latest"
},
"dependencies": {
"consola": "^3.3.3",
"discord.js": "^14.17.2",
"dotenv": "^16.4.7"
}
}
}

0 comments on commit adc1d22

Please sign in to comment.