-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
178 lines (153 loc) · 7.4 KB
/
cli.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
#!/usr/bin/env node
import { program } from "commander";
import fs from "fs";
import { readFile } from "fs/promises";
import path from "path";
import readline from "readline";
import * as rimraf from "rimraf"; // Import the 'rimraf' package
import contentfile from './content.js'
program.command("init").description("Initialize the CLI configuration file").action(() => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const filePath = "rapidjet.config.json";
fs.writeFileSync(filePath, `${JSON.stringify(contentfile.config)}`);
rl.close();
console.log(`CLI has been initialized , A rapidjet.config.json file has been created`);
});
let content
program.command("generate").description("Generate model-related folders and files").action(async () => {
try {
const configpath = path.join(process.cwd(), "rapidjet.config.json")
// const config = await import(`file://${configpath}.json`)
const jsonData = await readFile(`${configpath}`)
const config = JSON.parse(jsonData)
content = contentfile[config.orm];
let length, sep, location;
let extenstion = config.typescript ? ".ts" : ".js"
if (!config.apiPath || config.apiPath.trim() === "") {
console.log("apiPath not found in rapidjet.config.json")
return { error: "apiPath not found in rapidjet.config.json", message: "kindly initiaize the cli using init command" }
}
if (config.apiPath.trim() === "") {
length = 2
sep = "../";
location = sep.repeat(length) + config.databasePath
} else {
length = 2 + config.apiPath.split("/").filter(item => item.trim() !== "").length
sep = "../"
location = sep.repeat(length) + config.databasePath
}
const { modelName, fields } = await promptForModelInfo();
generateModelFiles({ modelName, fields, config, location, extenstion });
generateControllerFile({ modelName, fields, config, extenstion });
generateMiddlewareFile({ modelName, fields, config, extenstion });
generateRouteFile({ modelName, fields, config, extenstion });
} catch (error) {
if (error.code === "ENOENT") {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
rl.question("CLI configuration not found. Initialize now? (y/n)", async (anwer) => {
rl.close();
if (anwer.toLocaleLowerCase() === "y") {
const filePath = "rapidjet.config.json";
fs.writeFileSync(filePath, `${JSON.stringify(contentfile.config)}\n`);
console.log(`CLI configuration initialized.\n A new file ${path.join(process.cwd(), filePath)} \n Please setup the config file and run 'generate' again.`);
}
})
} else {
console.log(error)
}
// console.log( ? "Please initialize the cli using cmd - 'node cli init' before running 'generate'" : error)
// throw error("Please initialize the cli using cmd - 'node cli init' before running 'generate' ")
}
});
// to remove api
program.command("remove <modelName>").description("Remove model-related folders and files")
.action((modelName) => {
removeModelFiles(modelName);
});
program.parse(process.argv);
async function promptForModelInfo() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const modelName = await new Promise((resolve) => {
rl.question("Enter the model name: ", (answer) => {
resolve(answer);
});
});
const fields = await new Promise((resolve) => {
rl.question("Enter model fields (key:value format, e.g., name:string): ", (answer) => {
const fieldArray = answer.split(",").map((field) => {
const [key, value] = field.trim().split(":");
return { key, value };
});
resolve(fieldArray);
});
});
rl.close();
return { modelName, fields };
}
function generateModelFiles({ modelName, fields, config, location, extenstion }) {
// const apiPath = path.join(process.cwd(), "src", "api", modelName);
const apiDirectory = path.join(process.cwd(), config.apiPath, modelName);
const modelsDirectory = path.join(apiDirectory, "models");
fs.mkdirSync(modelsDirectory, { recursive: true });
// Create model file
const modelFileContent = content.model({ modelName, fields, config, location });
// const modelFileContent = generateModelFileContent(modelName, fields);
const modelFilePath = path.join(modelsDirectory, `${modelName}${extenstion}`);
fs.writeFileSync(modelFilePath, modelFileContent);
}
function generateControllerFile({ modelName, fields, config, extenstion }) {
// const apiDirectory = path.join(process.cwd(), "src", "api", modelName);
const apiDirectory = path.join(process.cwd(), config.apiPath, modelName);
const controllersDirectory = path.join(apiDirectory, "controllers");
fs.mkdirSync(controllersDirectory, { recursive: true });
// Create controller file
const controllerFileContent = content.controller({ modelName });
// const controllerFileContent = generateControllerFileContent(modelName);
const controllerFilePath = path.join(controllersDirectory, `${modelName}${extenstion}`);
fs.writeFileSync(controllerFilePath, controllerFileContent);
}
function generateMiddlewareFile({ modelName, fields, config, extenstion }) {
const apiDirectory = path.join(process.cwd(), config.apiPath, modelName);
const middlewaresDirectory = path.join(apiDirectory, "middlewares");
fs.mkdirSync(middlewaresDirectory, { recursive: true });
// Create middleware file
const middlewareFileContent = content.middleware({ fields });
// const middlewareFileContent = generateMiddlewareFileContent(modelName, fields);
const middlewareFilePath = path.join(middlewaresDirectory, `${modelName}${extenstion}`);
fs.writeFileSync(middlewareFilePath, middlewareFileContent);
}
function generateRouteFile({ modelName, fields, config, extenstion }) {
const apiDirectory = path.join(process.cwd(), config.apiPath, modelName);
const routesDirectory = path.join(apiDirectory, "routes");
fs.mkdirSync(routesDirectory, { recursive: true });
// Create route file
const routeFileContent = content.routes({ modelName });
// const routeFileContent = generateRouteFileContent(modelName);
const routeFilePath = path.join(routesDirectory, `${modelName}${extenstion}`);
fs.writeFileSync(routeFilePath, routeFileContent);
}
// remove files functinality
async function removeModelFiles(modelName) {
const configpath = path.join(process.cwd(), "rapidjet.config.json")
// const config = await import(`file://${configpath}`)
const jsonData = await readFile(`${configpath}`)
const config = JSON.parse(jsonData)
const apiDirectory = path.join(process.cwd(), config.apiPath, modelName);
// Check if the API directory exists
if (fs.existsSync(apiDirectory)) {
// Use the 'rimraf' package to remove the entire directory and its contents
rimraf.sync(apiDirectory);
console.log(`Model "${modelName}" and its associated files have been removed.`);
} else {
console.log(`Model "${modelName}" does not exist.`);
}
}