-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·78 lines (67 loc) · 2.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
import fs from "node:fs/promises";
import path from "node:path";
import { input } from "@inquirer/prompts";
import { buildBase, buildExt } from "./template.js";
import chalk from "chalk";
const exists = (path) =>
fs
.stat(path)
.then(() => true)
.catch(() => false);
const moonEmoji = "🌙";
async function write(dir, tmpl) {
for (const [filePath, fileContents] of Object.entries(tmpl)) {
const fullPath = path.join(dir, filePath);
const fullPathDir = path.dirname(fullPath);
if (!(await exists(fullPathDir))) await fs.mkdir(fullPathDir, { recursive: true });
await fs.writeFile(fullPath, fileContents);
}
}
const packageJson = path.resolve("./package.json");
if (await exists(packageJson)) {
console.log(`${moonEmoji} Using existing project directory.`);
let extId;
while (true) {
extId = await input({ message: "Choose an extension ID:", default: "sampleExtension" });
const extDir = path.join("./src", extId);
if ((await exists(extDir)) && (await fs.readdir(extDir)).length !== 0) {
console.log(
`${chalk.bgRed("[error]")} Extension already exists ${chalk.grey("-")} please choose another directory.`
);
} else {
break;
}
}
let tmpl = {};
tmpl = { ...tmpl, ...(await buildExt(extId, path.resolve("./env.d.ts"))) };
await write(path.resolve("."), tmpl);
console.log("Done.");
} else {
console.log(`${moonEmoji} Creating new extension.`);
let dir;
while (true) {
dir = await input({ message: "Choose a directory:", default: "./my-moonlight-extensions" });
if ((await exists(dir)) && (await fs.readdir(dir)).length !== 0) {
console.log(
`${chalk.bgRed("[error]")} Directory already exists ${chalk.grey("-")} please choose another directory.`
);
} else {
break;
}
}
dir = path.resolve(dir);
const packageName = path.basename(dir);
const extId = await input({ message: "Choose an extension ID:", default: "sampleExtension" });
let tmpl = {};
tmpl = { ...tmpl, ...(await buildBase(packageName)) };
tmpl = { ...tmpl, ...(await buildExt(extId)) };
await write(dir, tmpl);
console.log(`${chalk.green("Done!")} ${chalk.grey("Now run:")}\n`);
console.log(`${chalk.blue("$")} cd ${dir}`);
console.log(`${chalk.blue("$")} pnpm i`);
console.log(`${chalk.blue("$")} git init`);
console.log("\nGood luck! Feel free to reach out:");
console.log(`${chalk.blueBright("Docs:")} ${chalk.grey("https://moonlight-mod.github.io/ext-dev/getting-started")}`);
console.log(`${chalk.blueBright("Discord:")} ${chalk.grey("https://discord.gg/FdZBTFCP6F")}`);
}