-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
executable file
·309 lines (250 loc) · 8.53 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env node
import { program } from "commander";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { log, outro, text, select } from "@clack/prompts";
import { default as fs } from "fs-extra";
import { isCancel } from "@clack/prompts";
import { detect } from "detect-package-manager";
import pc from "picocolors";
const __dirname = dirname(fileURLToPath(import.meta.url));
// Read package.json to get the version
const packageJson = JSON.parse(
fs.readFileSync(resolve(__dirname, "package.json"), "utf8"),
);
const version = packageJson.version;
program
.name("message-kit")
.description("CLI to initialize projects")
.action(async () => {
try {
// Add Yarn 4 check at the start of the action
const pkgManager = await detectPackageManager();
log.info(pc.cyan(`pkgManager detected: ${pkgManager}`));
log.info(pc.cyan(`Welcome to MessageKit CLI v${version}!`));
const coolLogo = `
███╗ ███╗███████╗███████╗███████╗ █████╗ ██████╗ ███████╗██╗ ██╗██╗████████╗
████╗ ████║██╔════╝██╔════╝██╔════╝██╔══██╗██╔════╝ ██╔════╝██║ ██╔╝██║╚══██╔══╝
██╔████╔██║█████╗ ███████╗███████╗███████║██║ ███╗█████╗ █████╔╝ ██║ ██║
██║╚██╔╝██║██╔══╝ ╚════██║╚════██║██╔══██║██║ ██║██╔══╝ ██╔═██╗ ██║ ██║
██║ ╚═╝ ██║███████╗███████║███████║██║ ██║╚██████╔╝███████╗██║ ██╗██║ ██║
╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
Powered by XMTP`;
log.info(pc.red(coolLogo));
const { templateType, displayName, destDir } = await gatherProjectInfo();
// Create .gitignore
createGitignore(destDir);
// Create .data directory
createDataDir(destDir);
// Create .env file
createEnvFile(destDir);
// Create tsconfig.json file
createTsconfig(destDir);
// Wrap up
log.success(`Project launched in ${pc.red(destDir)}!`);
// Add package.json
updatePackagejson(destDir, templateType);
// Create README.md file
createReadme(destDir, templateType, displayName, pkgManager);
// Log next steps
logNextSteps(displayName);
outro(pc.red("Made with ❤️ by Ephemera"));
} catch (error) {
log.error(pc.red("An error occurred while creating your project:"));
log.error(error.message);
process.exit(1);
}
});
program.parse(process.argv);
async function createDataDir(destDir) {
if (!fs.existsSync(resolve(destDir, ".data"))) {
fs.mkdirSync(resolve(destDir, ".data"));
}
}
async function updatePackagejson(destDir, templateType) {
// Remove 'templates/' prefix if it exists in templateType
const cleanTemplatePath = templateType.replace("templates/", "");
const templateDir = resolve(__dirname, `templates/${cleanTemplatePath}`);
const packageTemplate = fs.readJsonSync(`${templateDir}/package.json`);
packageTemplate.dependencies["@xmtp/message-kit"] = "latest";
//Add for yarn in general
packageTemplate.scripts.postinstall = "tsc";
if (packageTemplate?.packageManager?.startsWith("yarn")) {
packageTemplate.packageManager = "yarn@4.5.1";
}
fs.writeJsonSync(resolve(destDir, "package.json"), packageTemplate, {
spaces: 2,
});
}
async function gatherProjectInfo() {
const templateOptions = [
{
value: "templates/simple",
label: "Simple - Basic template with minimal setup",
},
{
value: "templates/ens",
label: "ENS - Template with ENS integration",
},
{
value: "templates/paymentagent",
label: "Payment Agent - Template for funding an agent wallet",
},
];
const templateType = await select({
message: "Select the type of template to initialize:",
options: templateOptions,
});
if (isCancel(templateType)) {
process.exit(0);
}
// Clean up template path and ensure correct directory structure
const cleanTemplatePath = templateType.replace("templates/", "");
const templateDir = resolve(__dirname, `templates/${cleanTemplatePath}`);
// Ensure the template directory exists
if (!fs.existsSync(templateDir)) {
log.error(`Template directory not found: ${templateDir}`);
process.exit(1);
}
const displayName = await text({
message: "What is the name of the project to initialize?",
validate(value) {
if (!value) return "Please enter a name.";
return;
},
});
if (isCancel(displayName)) {
process.exit(0);
}
const name = kebabcase(displayName);
const destDir = resolve(process.cwd(), name);
// Remove existing directory if it exists
if (fs.existsSync(destDir)) {
fs.removeSync(destDir);
}
// Copy template files
fs.copySync(templateDir, destDir);
return { templateType: templateType, displayName, destDir, templateDir };
}
function createTsconfig(destDir) {
const tsconfigContent = {
include: ["src/**/*"],
compilerOptions: {
outDir: "./dist",
esModuleInterop: true,
declaration: true,
declarationMap: true,
forceConsistentCasingInFileNames: true,
isolatedModules: true,
lib: ["ESNext"],
module: "ESNext",
moduleResolution: "node",
resolveJsonModule: true,
skipLibCheck: true,
sourceMap: true,
strict: true,
target: "ESNext",
},
};
fs.writeJsonSync(resolve(destDir, "tsconfig.json"), tsconfigContent, {
spaces: 2,
});
}
function logNextSteps(name) {
log.message("Next steps:");
log.step(`1. ${pc.red(`cd ./${name}`)} - Navigate to project`);
log.step(`2. ${pc.red(`code .`)} - Open with your favorite editor`);
log.step(`3. ${pc.green("Start building!")}`);
}
function createGitignore(destDir) {
const gitignoreContent = `
# Main
.env
node_modules/
.data/
dist/
.DS_Store
# Logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# IDE files
.vscode/
.idea/
# packages
.yarn/
.pnpm/
.pnpm-workspace/`;
fs.writeFileSync(resolve(destDir, ".gitignore"), gitignoreContent.trim());
}
async function detectPackageManager() {
try {
// Check if running through bun create
if (process.env.BUN_CREATE === "true" || process.env._?.includes("bun")) {
return "bun";
}
const userAgent = process.env.npm_config_user_agent;
if (userAgent?.startsWith("npm") || userAgent?.startsWith("npx")) {
return "npm";
}
// Check for Bun in process.argv
if (process.argv.some((arg) => arg.includes("bun"))) {
return "bun";
}
// Fallback to detect for other cases
const pkgManager = await detect();
let version = "";
if (userAgent && pkgManager === "yarn") {
const parts = userAgent.split(" ")[0]?.split("/");
if (parts && parts.length > 1) {
version = `@${parts[1]}`;
}
}
return pkgManager + version;
} catch (error) {
return "npm";
}
}
function kebabcase(str) {
return str
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/[\s_]+/g, "-")
.toLowerCase();
}
function createEnvFile(destDir) {
fs.writeFileSync(resolve(destDir, ".env"), "");
}
// Create README.md file
function createReadme(destDir, templateType, projectName, packageManager) {
const envExampleContent = fs.readFileSync(
resolve(destDir, ".env.example"),
"utf8",
);
const readmeContent = `# ${projectName}
This project is powered by [MessageKit](https://message-kit.org/)
## Setup
Follow these steps to set up and run the project:
1. **Navigate to the project directory:**
\`\`\`sh
cd ./${projectName}
\`\`\`
2. **Set up your environment variables:**
\`\`\`sh
${envExampleContent}
\`\`\`
3. **Install dependencies:**
\`\`\`sh
${packageManager.split("@")[0]} install
\`\`\`
4. **Run the project:**
\`\`\`sh
${packageManager.split("@")[0]} run dev
\`\`\`
5. Enjoy!
---
Made with ❤️ by [Ephemera](https://ephemerahq.com)
`;
fs.writeFileSync(resolve(destDir, "README.md"), readmeContent.trim());
}