@dolphjs/cli 1.1.13 → 1.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,6 +5,7 @@ import { promisify } from "util";
|
|
|
5
5
|
import { readConfig } from "../utils/read_user_config_path.js";
|
|
6
6
|
import { resolveControllerContent } from "./resolvers/resolve_controller_content.js";
|
|
7
7
|
import { generateController as generateSpringController } from "./spring/generate_spring_controller.js";
|
|
8
|
+
import { addControllerInComponentFIle } from "../registers/register_controllers_in_components.js";
|
|
8
9
|
const writeFileAsync = promisify(writeFile);
|
|
9
10
|
const createControllerDirectory = () => {
|
|
10
11
|
const projectRoot = path.join(process.cwd());
|
|
@@ -31,6 +32,7 @@ export const generateControllerFile = async (name, controllerDir, readConfig) =>
|
|
|
31
32
|
export const generateController = async (name) => {
|
|
32
33
|
if (readConfig().routing === "spring") {
|
|
33
34
|
await generateSpringController(name);
|
|
35
|
+
addControllerInComponentFIle(name);
|
|
34
36
|
}
|
|
35
37
|
else {
|
|
36
38
|
if (!name) {
|
|
@@ -25,7 +25,7 @@ export const addComponentToServerFile = (componentName) => {
|
|
|
25
25
|
}
|
|
26
26
|
const serverPath = path.join(serverDir, "server.ts");
|
|
27
27
|
const capitalizedString = componentName.charAt(0).toUpperCase() + componentName.slice(1);
|
|
28
|
-
const importStatement = `import { ${capitalizedString}Component } from "
|
|
28
|
+
const importStatement = `import { ${capitalizedString}Component } from "./components/${componentName}/${componentName}.component";`;
|
|
29
29
|
try {
|
|
30
30
|
if (!existsSync(serverPath)) {
|
|
31
31
|
const fileContent = `import { DolphFactory } from "@dolphjs/dolph";
|
|
@@ -48,7 +48,7 @@ dolph.start();
|
|
|
48
48
|
if (routesArrayMatch) {
|
|
49
49
|
const existingCode = routesArrayMatch[1].trim();
|
|
50
50
|
let fileContents;
|
|
51
|
-
fileContents = fileContent.replace(existingCode,
|
|
51
|
+
fileContents = fileContent.replace(existingCode, `${existingCode.length > 0
|
|
52
52
|
? existingCode.slice(0, -1) + `, ${newComponentInstance}]`
|
|
53
53
|
: newComponentInstance}`);
|
|
54
54
|
const updatedContents = `${importStatement}\n${fileContents}`;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { dolphMsg } from "../helpers/messages.js";
|
|
4
|
+
const findComponentsDirectory = (componentName) => {
|
|
5
|
+
const rootDir = process.cwd();
|
|
6
|
+
const possibleDirs = [`src/components/${componentName}`];
|
|
7
|
+
const srcDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
|
|
8
|
+
return srcDir ? path.join(rootDir, srcDir) : null;
|
|
9
|
+
};
|
|
10
|
+
export const addControllerInComponentFIle = (componentName) => {
|
|
11
|
+
if (!componentName) {
|
|
12
|
+
dolphMsg.errorRed("component extension or name is required! 🤨");
|
|
13
|
+
}
|
|
14
|
+
const componentDir = findComponentsDirectory(componentName);
|
|
15
|
+
if (!componentDir) {
|
|
16
|
+
dolphMsg.errorRed("something went wrong, please make sure to use the `new` command when creating a dolph project.");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const componentPath = path.join(componentDir, `${componentName}.component.ts`);
|
|
20
|
+
const capitalizedString = componentName.charAt(0).toUpperCase() + componentName.slice(1);
|
|
21
|
+
const importStatement = `import { ${capitalizedString}Controller } from "./${componentName}.controller";`;
|
|
22
|
+
try {
|
|
23
|
+
if (!existsSync(componentPath)) {
|
|
24
|
+
const fileContent = `import { Component } from "@dolphjs/dolph/decorators";
|
|
25
|
+
@Component({ controllers: [${capitalizedString}Controller] })
|
|
26
|
+
export class ${capitalizedString}Component {}
|
|
27
|
+
`;
|
|
28
|
+
writeFileSync(componentPath, fileContent);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
const fileContent = readFileSync(componentPath, "utf8");
|
|
32
|
+
const newComponentInstance = `${capitalizedString}Controller`;
|
|
33
|
+
if (!fileContent.includes(importStatement)) {
|
|
34
|
+
const updatedContent = `${importStatement}\n\n${fileContent}`;
|
|
35
|
+
writeFileSync(componentPath, updatedContent);
|
|
36
|
+
}
|
|
37
|
+
if (fileContent.includes(`const dolph`)) {
|
|
38
|
+
// add new controller class to the existing array
|
|
39
|
+
const controllersArrayMatch = fileContent.match(/@Component\(\s*{\s*controllers:\s*\[([^\]]*)\]\s*}\s*\)/);
|
|
40
|
+
if (controllersArrayMatch) {
|
|
41
|
+
const existingCode = controllersArrayMatch[1].trim();
|
|
42
|
+
let fileContents;
|
|
43
|
+
fileContents = fileContent.replace(existingCode, `${existingCode.length > 0
|
|
44
|
+
? existingCode.slice(0, -1) + `, ${newComponentInstance}`
|
|
45
|
+
: newComponentInstance}`);
|
|
46
|
+
const updatedContents = `${importStatement}\n${fileContents}`;
|
|
47
|
+
writeFileSync(componentPath, updatedContents);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
dolphMsg.info(`registerd ${capitalizedString}Controller in ${capitalizedString}Component`);
|
|
52
|
+
process.exit(0);
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
dolphMsg.errorRed(e.toString());
|
|
56
|
+
}
|
|
57
|
+
};
|
|
@@ -11,6 +11,7 @@ import { addServerFile } from "../registers/server_file_routes.js";
|
|
|
11
11
|
import { dolphMsg } from "../helpers/messages.js";
|
|
12
12
|
import { generateComponent } from "../generators/spring/generate_spring_component.js";
|
|
13
13
|
import { addComponentToServerFile } from "../registers/register_component.js";
|
|
14
|
+
import { addControllerInComponentFIle } from "../registers/register_controllers_in_components.js";
|
|
14
15
|
export const packageGenerator = () => {
|
|
15
16
|
program
|
|
16
17
|
.command("generate")
|
|
@@ -66,6 +67,7 @@ export const packageGenerator = () => {
|
|
|
66
67
|
}
|
|
67
68
|
else {
|
|
68
69
|
generateComponent(value.toString());
|
|
70
|
+
addControllerInComponentFIle(value.toString());
|
|
69
71
|
addComponentToServerFile(value.toString());
|
|
70
72
|
}
|
|
71
73
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dolphjs/cli",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.15",
|
|
4
4
|
"description": "The dolphjs-cli is a tool for efficient intialization and management of a dolphjs project",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"repository": "https://github.com/dolphjs/dolph-cli.git",
|