-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
ยท97 lines (82 loc) ยท 2.99 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
#!/usr/bin/env node
import fs from "fs"; // Import fs to interact with the filesystem
import { Command } from "commander"; // Import Commander.js for CLI functionality
import path from "path";
import { getPrompts } from "./lib/prompts.js";
import { generateFiles } from "./lib/fileGenerator.js";
import { initializeGit } from "./lib/gitInitializer.js";
import { installPackages } from "./lib/packageInstaller.js";
import { generateCommand } from "./lib/generateCommand.js"; // Function for command scaffolding
const program = new Command();
// Display the Yocode banner
console.log(`
๐ Yocode - Your VS Code Extension Generator ๐
`);
// `yocode init` command to initialize a new project
program
.command("init")
.description("Initialize a new VS Code extension project")
.action(async () => {
const answers = await getPrompts();
const targetDir = path.join(process.cwd(), answers.identifier);
// Step 1: Generate files
await generateFiles(targetDir, answers);
// Step 2: Initialize Git
if (answers.gitInit) {
try {
await initializeGit(targetDir);
} catch (error) {
console.error("Failed to initialize Git repository:", error);
}
}
// Step 3: Install dependencies
try {
await installPackages(targetDir, answers.packageManager);
} catch (error) {
console.error(
`Failed to install dependencies with ${answers.packageManager}:`,
error
);
}
// Success message
console.log(
`๐ Congratulations! Your project has been set up successfully in ${targetDir} ๐`
);
console.log("๐ Time to start building your awesome extension! ๐");
});
// `yocode generate:command <commandName>` to scaffold a new command
program
.command("generate:command <commandName>")
.description("Generate a new command in the current project")
.action(async (commandName) => {
const targetDir = process.cwd(); // Use the current working directory
const packageJsonPath = path.join(targetDir, "package.json");
// Check if the command is being run inside a valid Yocode project
if (!fs.existsSync(packageJsonPath)) {
console.error(
"โ Error: package.json not found. Please run this command inside a Yocode project."
);
return;
}
// Detect the language type from package.json
let languageType = "JavaScript"; // Default to JavaScript
try {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
if (packageJson.devDependencies?.typescript) {
languageType = "TypeScript";
}
} catch (error) {
console.error("โ Error reading package.json:", error.message);
return;
}
try {
await generateCommand(commandName, languageType, targetDir);
} catch (error) {
console.error(
`โ Failed to generate the command "${commandName}": ${error.message}`
);
}
});
// Parse CLI arguments
program.parse(process.argv);
// spinner.succeed('Files generated successfully!');