@dolphjs/cli 1.1.17 โ†’ 1.1.19

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.
@@ -14,7 +14,7 @@ const findSrcDirectory = () => {
14
14
  const srcDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
15
15
  return srcDir ? path.join(rootDir, srcDir) : null;
16
16
  };
17
- export const addComponentToServerFile = (componentName) => {
17
+ export const addComponentToServerFile = async (componentName) => {
18
18
  if (!componentName) {
19
19
  dolphMsg.errorRed("component extension or name is required! ๐Ÿคจ");
20
20
  }
@@ -33,19 +33,12 @@ export class ${capitalizedString}Component {}
33
33
  const fileContent = readFileSync(componentPath, "utf8");
34
34
  const newComponentInstance = `${capitalizedString}Controller`;
35
35
  if (!fileContent.includes(importStatement)) {
36
- const controllersArrayMatch = fileContent.match(/@Component\(\s*{\s*controllers:\s*\[([^\]]*)\]\s*}\s*\)/);
37
- if (controllersArrayMatch) {
38
- const existingCode = controllersArrayMatch[1].trim();
39
- let fileContents;
40
- fileContents = fileContent.replace(existingCode, `${existingCode.length > 0
41
- ? existingCode + `, ${newComponentInstance}`
42
- : newComponentInstance}`);
43
- const updatedContents = `${importStatement}\n${fileContents}`;
44
- writeFileSync(componentPath, updatedContents);
45
- }
36
+ // If import statement is not present, add it along with the new controller
37
+ const updatedContents = `${importStatement}\n${fileContent.replace(/@Component\(\s*{\s*controllers:\s*\[([^\]]*)\]\s*}\s*\)/, `@Component({ controllers: [${newComponentInstance}] })`)}`;
38
+ writeFileSync(componentPath, updatedContents);
46
39
  }
47
- if (fileContent.includes(`const dolph`)) {
48
- // add new controller class to the existing array
40
+ else if (fileContent.includes(`const dolph`)) {
41
+ // If import statement is present and contains `const dolph`
49
42
  const controllersArrayMatch = fileContent.match(/@Component\(\s*{\s*controllers:\s*\[([^\]]*)\]\s*}\s*\)/);
50
43
  if (controllersArrayMatch) {
51
44
  const existingCode = controllersArrayMatch[1].trim();
@@ -58,10 +51,11 @@ export class ${capitalizedString}Component {}
58
51
  }
59
52
  }
60
53
  }
61
- dolphMsg.info(`registerd ${capitalizedString}Controller in ${capitalizedString}Component`);
54
+ dolphMsg.info(`registered ${capitalizedString}Controller in ${capitalizedString}Component`);
62
55
  process.exit(0);
63
56
  }
64
- catch (e) {
65
- dolphMsg.errorRed(e.toString());
57
+ catch (error) {
58
+ console.error(error);
59
+ process.exit(1);
66
60
  }
67
61
  };
@@ -0,0 +1,72 @@
1
+ import chalk from "chalk";
2
+ import { existsSync, writeFileSync } from "fs";
3
+ import path from "path";
4
+ const findBaseDirectory = () => {
5
+ const rootDir = process.cwd();
6
+ const possibleDirs = ["/src"];
7
+ const routesDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
8
+ return routesDir ? path.join(rootDir, routesDir) : null;
9
+ };
10
+ export const addServerFile = async (database, name) => {
11
+ const baseDirectory = findBaseDirectory();
12
+ const indexPath = path.join(baseDirectory, `server.ts`);
13
+ let importStatement = ``;
14
+ let otherFileContent = "";
15
+ const capitalizedString = name.charAt(0).toUpperCase() + name.slice(1);
16
+ if (database === "mysql" && name.length) {
17
+ importStatement = `import { DolphFactory } from "@dolphjs/dolph";
18
+ import { ${capitalizedString}Component } from "./components/${name}/${name}.component.ts";
19
+ import { sequelizeInstance } from "@/shared/configs/db.configs";
20
+ import { autoInitMySql } from "@dolphjs/dolph/packages";
21
+ `;
22
+ }
23
+ else if (database === "mysql") {
24
+ importStatement = `import { DolphFactory } from "@dolphjs/dolph";
25
+ import { sequelizeInstance } from "@/shared/configs/db.configs";
26
+ import { autoInitMySql } from "@dolphjs/dolph/packages";`;
27
+ }
28
+ else if (database === "mongo" && name.length) {
29
+ importStatement = `import { DolphFactory } from "@dolphjs/dolph";
30
+ import { ${capitalizedString}Component } from "./components/${name}/${name}.component.ts";
31
+ import { autoInitMySql } from "@dolphjs/dolph/packages";`;
32
+ }
33
+ else if (database === "mongo") {
34
+ importStatement = `import { DolphFactory } from "@dolphjs/dolph";
35
+ import { autoInitMySql } from "@dolphjs/dolph/packages";`;
36
+ }
37
+ if (database === "mysql" && name) {
38
+ otherFileContent = `const dolph = new DolphFactory([${capitalizedString}Component]);
39
+ autoInitMySql(sequelizeInstance);
40
+
41
+ dolph.start();`;
42
+ }
43
+ else if (database === "mysql") {
44
+ otherFileContent = `const dolph = new DolphFactory([]);
45
+ autoInitMySql(sequelizeInstance);
46
+
47
+ dolph.start();`;
48
+ }
49
+ else if (database === "mongo" && name) {
50
+ otherFileContent = `const dolph = new DolphFactory([${capitalizedString}Component]);
51
+ dolph.start();`;
52
+ }
53
+ else if (database === "mongo") {
54
+ otherFileContent = `const dolph = new DolphFactory([]);
55
+ dolph.start();`;
56
+ }
57
+ try {
58
+ // check if file exists
59
+ if (!existsSync(indexPath)) {
60
+ // if it doesn't exist, create file and add import statement
61
+ const fileContent = `${importStatement}\n\n${otherFileContent}\n`;
62
+ writeFileSync(indexPath, fileContent);
63
+ }
64
+ else {
65
+ const fileContent = `${importStatement}\n\n${otherFileContent}\n`;
66
+ writeFileSync(indexPath, fileContent);
67
+ }
68
+ }
69
+ catch (e) {
70
+ console.log(chalk.bold(chalk.red(e)));
71
+ }
72
+ };
@@ -8,6 +8,7 @@ import { writeDolphConfig } from "./write_dolph_config.js";
8
8
  import { writePackageJsonFile } from "./write_package_json.js";
9
9
  import { writeGitignore } from "./write_gitignore.js";
10
10
  import { dolphMsg } from "../helpers/messages.js";
11
+ import { addServerFile as addSpringServerFile } from "../registers/server_file_spring.js";
11
12
  export const initDolphCli = (appname) => {
12
13
  let projectName = appname;
13
14
  if (appname.length === 0) {
@@ -99,6 +100,7 @@ export const initDolphCli = (appname) => {
99
100
  if (!existsSync(sharedDirPath)) {
100
101
  mkdirSync(sharedDirPath);
101
102
  }
103
+ addSpringServerFile(userConfig.database, "");
102
104
  }
103
105
  dolphMsg.infoBlue("dolph configurations have been initialized successfully โœจ.");
104
106
  dolphMsg.infoYellow("run `yarn install` to install dependencies in project directory and start building ๐Ÿš€.");
@@ -7,7 +7,7 @@ import * as modelGen from "../generators/generate_model.js";
7
7
  import * as mysqlGen from "../generators/generate_mysql.js";
8
8
  import { readConfig } from "./read_user_config_path.js";
9
9
  import { addRoutesIndexFile } from "../registers/register_routes.js";
10
- import { addServerFile } from "../registers/server_file_routes.js";
10
+ import { addServerFile } from "../registers/server_file_express.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";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dolphjs/cli",
3
- "version": "1.1.17",
3
+ "version": "1.1.19",
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",