Skip to content

Commit adc1d22

Browse files
New release with init command
1 parent cab1a98 commit adc1d22

7 files changed

Lines changed: 202 additions & 25 deletions

File tree

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
# discraft
22

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

5-
```bash
6-
bun install
7-
```
8-
9-
To run:
10-
11-
```bash
12-
bun run index.ts
13-
```
14-
15-
This project was created using `bun init` in bun v1.1.42. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
6+
This package is being redone.

bun.lock

Lines changed: 23 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "discraft",
3-
"version": "1.6.2-beta.3",
3+
"version": "1.6.2-beta.4",
44
"description": "Ultimate Discord bot framework",
55
"type": "module",
66
"packageManager": "bun@1.1.42",
@@ -22,6 +22,7 @@
2222
],
2323
"files": [
2424
"package/dist",
25+
"templates",
2526
"README.md",
2627
"LICENSE"
2728
],
@@ -33,6 +34,7 @@
3334
"homepage": "https://github.com/The-Best-Codes/discraft-js#readme",
3435
"devDependencies": {
3536
"@types/bun": "^1.1.14",
37+
"@types/fs-extra": "^11.0.4",
3638
"bun": "^1.1.42"
3739
},
3840
"peerDependencies": {
@@ -43,7 +45,8 @@
4345
"consola": "^3.3.3",
4446
"esbuild": "^0.24.2",
4547
"esbuild-node-externals": "^1.16.0",
48+
"fs-extra": "^11.2.0",
4649
"glob": "^11.0.0",
4750
"inquirer": "^12.3.0"
4851
}
49-
}
52+
}

package/src/cli.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { program } from "commander";
44
import consola from "consola";
55
import { version } from "../../package.json";
66
import { build } from "./cli/build";
7+
import { init } from "./cli/init";
78
import { start } from "./cli/start";
89

910
program
@@ -38,4 +39,14 @@ program
3839
});
3940
});
4041

42+
program
43+
.command("init")
44+
.description("Initialize a new Discraft project")
45+
.action(() => {
46+
init().catch((error) => {
47+
consola.error("An error occurred during initialization:", error);
48+
process.exit(1);
49+
});
50+
});
51+
4152
program.parse(process.argv);

package/src/cli/init/index.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { execSync } from "child_process";
2+
import consola from "consola";
3+
import fs from "fs-extra";
4+
import inquirer from "inquirer";
5+
import path from "path";
6+
7+
async function detectPackageManager(): Promise<string | undefined> {
8+
const userAgent = process.env.npm_config_user_agent;
9+
if (userAgent) {
10+
if (userAgent.startsWith("bun")) {
11+
return "bun";
12+
}
13+
if (userAgent.startsWith("npm")) {
14+
return "npm";
15+
}
16+
if (userAgent.startsWith("yarn")) {
17+
return "yarn";
18+
}
19+
if (userAgent.startsWith("pnpm")) {
20+
return "pnpm";
21+
}
22+
}
23+
24+
try {
25+
execSync("bun --version", { stdio: "ignore" });
26+
return "bun";
27+
} catch (e) {}
28+
try {
29+
execSync("npm --version", { stdio: "ignore" });
30+
return "npm";
31+
} catch (e) {}
32+
try {
33+
execSync("yarn --version", { stdio: "ignore" });
34+
return "yarn";
35+
} catch (e) {}
36+
try {
37+
execSync("pnpm --version", { stdio: "ignore" });
38+
return "pnpm";
39+
} catch (e) {}
40+
41+
return undefined;
42+
}
43+
44+
async function copyTemplate(templatePath: string, projectPath: string) {
45+
const files = [
46+
"clients",
47+
"commands",
48+
"events",
49+
"utils",
50+
".env.example",
51+
".gitignore",
52+
"index.ts",
53+
"package.json",
54+
"tsconfig.json",
55+
];
56+
57+
for (const file of files) {
58+
const sourcePath = path.join(templatePath, file);
59+
const destPath = path.join(projectPath, file);
60+
61+
try {
62+
if (fs.lstatSync(sourcePath).isDirectory()) {
63+
await fs.copy(sourcePath, destPath);
64+
} else {
65+
await fs.copyFile(sourcePath, destPath);
66+
}
67+
} catch (e) {
68+
consola.error(`Failed to copy ${sourcePath} to ${destPath}`, e);
69+
throw new Error(`Failed to copy template files, ${e}`);
70+
}
71+
}
72+
}
73+
74+
async function init() {
75+
const currentWorkingDirectory = process.cwd();
76+
const packageRoot = path.join(__dirname, "..", "..", ".."); //navigate to package root from here
77+
const templatePath = path.join(packageRoot, "templates", "ts");
78+
79+
const useCurrentDir = await inquirer.prompt([
80+
{
81+
type: "confirm",
82+
name: "useCurrent",
83+
message: "Initialize a project in the current directory?",
84+
default: true,
85+
},
86+
]);
87+
88+
let projectDir = currentWorkingDirectory;
89+
90+
if (!useCurrentDir.useCurrent) {
91+
const projectDirResponse = await inquirer.prompt([
92+
{
93+
type: "input",
94+
name: "projectName",
95+
message: "What is your project directory?",
96+
default: "my-project",
97+
},
98+
]);
99+
projectDir = path.join(
100+
currentWorkingDirectory,
101+
projectDirResponse.projectName,
102+
);
103+
}
104+
const packageManager = await detectPackageManager();
105+
106+
const packageManagerResponse = await inquirer.prompt([
107+
{
108+
type: "list",
109+
name: "packageManager",
110+
message: "Select a package manager to install dependencies:",
111+
choices: [
112+
{ name: "npm", value: "npm" },
113+
{ name: "yarn", value: "yarn" },
114+
{ name: "pnpm", value: "pnpm" },
115+
{ name: "bun", value: "bun" },
116+
{ name: "Don't install dependencies", value: "none" },
117+
],
118+
default: packageManager ?? "none",
119+
},
120+
]);
121+
122+
consola.info(`Initializing project in ${projectDir}...`);
123+
124+
try {
125+
await fs.ensureDir(projectDir);
126+
await copyTemplate(templatePath, projectDir);
127+
consola.success("Copied template files successfully.");
128+
129+
if (packageManagerResponse.packageManager !== "none") {
130+
consola.info(
131+
`Installing dependencies with ${packageManagerResponse.packageManager}...`,
132+
);
133+
execSync(`${packageManagerResponse.packageManager} install`, {
134+
cwd: projectDir,
135+
stdio: "inherit",
136+
});
137+
consola.success("Installed dependencies.");
138+
}
139+
140+
consola.success("Project initialized successfully!");
141+
consola.info(
142+
`Go to ${path.relative(currentWorkingDirectory, projectDir)} and run "${packageManagerResponse.packageManager} run build" and "${packageManagerResponse.packageManager} run start" to get started.`,
143+
);
144+
} catch (error: any) {
145+
consola.error(`Failed to initialize project: ${error.message}`);
146+
}
147+
}
148+
149+
export { init };

templates/ts/.npmignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
dist
2+
.discraft
3+
node_modules
4+
.env
5+
bun.lockb
6+
bun.lock
7+
package-lock.json
8+
yarn.lock
9+
pnpm-lock.yaml

templates/ts/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"build": "discraft build"
1111
},
1212
"devDependencies": {
13-
"discraft": "1.6.2-beta.3"
13+
"discraft": "latest"
1414
},
1515
"dependencies": {
1616
"consola": "^3.3.3",
1717
"discord.js": "^14.17.2",
1818
"dotenv": "^16.4.7"
1919
}
20-
}
20+
}

0 commit comments

Comments
 (0)