@dolphjs/cli 1.1.6 → 1.1.8

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.
@@ -4,7 +4,7 @@ import chalk from "chalk";
4
4
  import { getRootDirectory } from "../utils/get_root_dir_path.js";
5
5
  import { readConfig } from "../utils/read_user_config_path.js";
6
6
  import { join } from "path";
7
- import { debounce } from "lodash";
7
+ import _ from "lodash";
8
8
  let fileExtension = "";
9
9
  let indexFilePath = "";
10
10
  export const startApp = () => {
@@ -105,7 +105,7 @@ const watcher = chokidar.watch([], {
105
105
  ignoreInitial: false,
106
106
  });
107
107
  let isWatcherActive = false;
108
- const debouncedStartApp = debounce(startApp, 1000);
108
+ const debouncedStartApp = _.debounce(startApp, 1000);
109
109
  export const watchFile = () => {
110
110
  if (!isWatcherActive) {
111
111
  console.log(`${chalk.bold(chalk.green("[DOLPH INFO]: "))} ${chalk.greenBright("watching files for changes ...")}`);
@@ -3,6 +3,7 @@ import path from "path";
3
3
  import chalk from "chalk";
4
4
  import { readConfig } from "../utils/read_user_config_path.js";
5
5
  import { resolveControllerContent } from "./resolvers/resolve_controller_content.js";
6
+ import { generateController as generateSpringController } from "./spring/generate_spring_controller.js";
6
7
  const createControllerDirectory = () => {
7
8
  const projectRoot = path.join(process.cwd());
8
9
  const userControllerFilePath = path.join(projectRoot, "/src/controllers");
@@ -34,15 +35,13 @@ export const generateControllerFile = async (name, controllerDir, readConfig) =>
34
35
  });
35
36
  };
36
37
  export const generateController = async (name) => {
38
+ if (readConfig().routing === "spring") {
39
+ generateSpringController(name);
40
+ }
37
41
  if (!name)
38
42
  chalk.bold(chalk.red("Controller extension or name is required! 🤨"));
39
43
  let controllerDir = findControllerDirectory();
40
44
  if (!controllerDir) {
41
- //TODO: create one if it doesn't exist
42
- // console.log(
43
- // chalk.bold(chalk.red("controller directory doesn't exist 🤨"))
44
- // );
45
- // return;
46
45
  createControllerDirectory();
47
46
  controllerDir = findControllerDirectory();
48
47
  }
@@ -0,0 +1,42 @@
1
+ import { existsSync, mkdirSync, writeFile } from "fs";
2
+ import path from "path";
3
+ import chalk from "chalk";
4
+ import { generateSpringComponent } from "../../generators/templates/spring_component_template.js";
5
+ import { dolphMsg } from "../../helpers/messages.js";
6
+ const createComponentDirectory = (componentName) => {
7
+ const projectRoot = path.join(process.cwd());
8
+ const userComponentPath = path.join(projectRoot, `/src/${componentName}`);
9
+ if (!existsSync(userComponentPath)) {
10
+ mkdirSync(userComponentPath);
11
+ }
12
+ };
13
+ const findComponentDirectory = (componentName) => {
14
+ const rootDir = process.cwd();
15
+ const possibleDirs = [`src/${componentName}`];
16
+ const componentDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
17
+ return componentDir ? path.join(rootDir, componentDir) : null;
18
+ };
19
+ export const generateSpringComponentFile = async (componentName, componentDir) => {
20
+ await writeFile(componentDir, generateSpringComponent(componentName), (error) => {
21
+ if (error) {
22
+ dolphMsg.errorRed(error.toString());
23
+ }
24
+ });
25
+ };
26
+ export const generateComponent = async (name) => {
27
+ if (!name)
28
+ dolphMsg.errorRed("component extension or name is required! 🤨");
29
+ let componentDir = findComponentDirectory(name);
30
+ if (!componentDir) {
31
+ createComponentDirectory(name);
32
+ componentDir = findComponentDirectory(name);
33
+ }
34
+ const componentFilePath = path.join(componentDir + `/${name}.component.ts`);
35
+ try {
36
+ generateSpringComponentFile(name, path.join(componentFilePath));
37
+ }
38
+ catch (e) {
39
+ dolphMsg.errorRed(e);
40
+ }
41
+ dolphMsg.info(`${chalk.blue(`${name}.component.ts`)} generated successfully for ${chalk.blue(`${name}`)} component.`);
42
+ };
@@ -0,0 +1,42 @@
1
+ import { existsSync, mkdirSync, writeFile } from "fs";
2
+ import path from "path";
3
+ import chalk from "chalk";
4
+ import { generateSpringController } from "../../generators/templates/spring_controller_template.js";
5
+ import { dolphMsg } from "../../helpers/messages.js";
6
+ const createControllerDirectory = (componentName) => {
7
+ const projectRoot = path.join(process.cwd());
8
+ const userCompontrollerPath = path.join(projectRoot, `/src/${componentName}`);
9
+ if (!existsSync(userCompontrollerPath)) {
10
+ mkdirSync(userCompontrollerPath);
11
+ }
12
+ };
13
+ const findControllerDirectory = (componentName) => {
14
+ const rootDir = process.cwd();
15
+ const possibleDirs = [`src/${componentName}`];
16
+ const controllerDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
17
+ return controllerDir ? path.join(rootDir, controllerDir) : null;
18
+ };
19
+ export const generateSpringControllerFile = async (componentName, controllerDir) => {
20
+ await writeFile(controllerDir, generateSpringController(componentName), (error) => {
21
+ if (error) {
22
+ dolphMsg.errorRed(error.toString());
23
+ }
24
+ });
25
+ };
26
+ export const generateController = async (name) => {
27
+ if (!name)
28
+ dolphMsg.errorRed("controller extension or name is required! 🤨");
29
+ let controllerDir = findControllerDirectory(name);
30
+ if (!controllerDir) {
31
+ createControllerDirectory(name);
32
+ controllerDir = findControllerDirectory(name);
33
+ }
34
+ const controllerFilePath = path.join(controllerDir + `/${name}.controller.ts`);
35
+ try {
36
+ generateSpringControllerFile(name, path.join(controllerFilePath));
37
+ }
38
+ catch (e) {
39
+ dolphMsg.errorRed(e);
40
+ }
41
+ dolphMsg.info(`${chalk.blue(`${name}.controller.ts`)} generated successfully for ${chalk.blue(`${name}`)} component.`);
42
+ };
@@ -0,0 +1,12 @@
1
+ export const generateSpringComponent = (componentName) => {
2
+ const capitalizeFirstLetter = (str) => {
3
+ return str.charAt(0).toUpperCase() + str.slice(1);
4
+ };
5
+ const className = capitalizeFirstLetter(componentName) + "Component";
6
+ const template = `import { Component } from "@dolphjs/dolph/decorators";
7
+
8
+ @Component({ controllers: [] })
9
+ export class ${className} {}
10
+ `;
11
+ return template;
12
+ };
@@ -0,0 +1,28 @@
1
+ export const generateSpringController = (componentName) => {
2
+ const capitalizeFirstLetter = (str) => {
3
+ return str.charAt(0).toUpperCase() + str.slice(1);
4
+ };
5
+ const className = capitalizeFirstLetter(componentName) + "Controller";
6
+ const template = `import { DolphControllerHandler } from "@dolphjs/dolph/classes";
7
+ import {
8
+ Dolph,
9
+ SuccessResponse,
10
+ DRequest,
11
+ DResponse
12
+ } from "@dolphjs/dolph/common
13
+ import { Get, Route } from "@dolphjs/dolph/decorators";
14
+
15
+ @Route('${componentName}')
16
+ export class ${className} extends DolphControllerHandler<Dolph> {
17
+ constructor() {
18
+ super();
19
+ }
20
+
21
+ @Get("greet")
22
+ async greet (req: DRequest, res: DResponse) {
23
+ SuccessResponse({ res, body: { message: "you've reached the ${componentName} endpoint." } });
24
+ }
25
+ }
26
+ `;
27
+ return template;
28
+ };
@@ -0,0 +1,9 @@
1
+ import chalk from "chalk";
2
+ export const dolphMsg = {
3
+ infoGreen: (message) => console.log(chalk.bold(chalk.green("[DOLPH INFO]: ")) + chalk.greenBright(message)),
4
+ infoYellow: (message) => console.log(chalk.bold(chalk.yellow("[DOLPH INFO]: ")) + chalk.yellowBright(message)),
5
+ infoBlue: (message) => console.log(chalk.bold(chalk.blue("[DOLPH INFO]: ")) + chalk.blueBright(message)),
6
+ info: (message) => console.log(chalk.bold(chalk.blue("[DOLPH INFO]: " + chalk.white(message)))),
7
+ errorRed: (message) => console.log(chalk.bold(chalk.red("[DOLPH ERROR]: ")) + chalk.redBright(message)),
8
+ errorGray: (message) => console.log(chalk.bold(chalk.bgGrey("[DOLPH ERROR]: ")) + chalk.grey(message)),
9
+ };
@@ -1,7 +1,6 @@
1
1
  import chalk from "chalk";
2
2
  import { existsSync, readFileSync, writeFileSync } from "fs";
3
3
  import path from "path";
4
- // removed other possible dirs to enforce dolphjs style guide
5
4
  const findRoutesDirectory = () => {
6
5
  const rootDir = process.cwd();
7
6
  const possibleDirs = [
@@ -2,16 +2,16 @@ import { existsSync, mkdirSync, writeFileSync } from "fs";
2
2
  import inquirer from "inquirer";
3
3
  import path from "path";
4
4
  import yaml from "js-yaml";
5
- import chalk from "chalk";
6
5
  import { writeSwcrc } from "./write_swrcc.js";
7
6
  import { writeTsConfigFile } from "./write_tsconfig.js";
8
7
  import { writeDolphConfig } from "./write_dolph_config.js";
9
8
  import { writePackageJsonFile } from "./write_package_json.js";
10
9
  import { writeGitignore } from "./write_gitignore.js";
10
+ import { dolphMsg } from "../helpers/messages.js";
11
11
  export const initDolphCli = (appname) => {
12
12
  let projectName = appname;
13
13
  if (appname.length === 0) {
14
- console.log(chalk.red(chalk.bold("Provide a name for you project or indicate with a '.' to use current directory ")));
14
+ dolphMsg.errorGray("provide a name for you project or indicate with a '.' to use current directory.");
15
15
  return;
16
16
  }
17
17
  if (appname !== ".") {
@@ -35,6 +35,7 @@ export const initDolphCli = (appname) => {
35
35
  paradigm: "oop",
36
36
  database: "mongo",
37
37
  generateFolder: true,
38
+ routing: "express",
38
39
  };
39
40
  if (!existsSync(userConfigFilePath)) {
40
41
  inquirer
@@ -60,20 +61,23 @@ export const initDolphCli = (appname) => {
60
61
  choices: ["mongo", "mysql", "postgresql", "other"],
61
62
  default: "mongo",
62
63
  },
63
- // {
64
- // type: "list",
65
- // name: "generateFolder",
66
- // message:
67
- // "Generate folder and files for the Services, Models, Controllers and Routes",
68
- // choices: ["true", "false"],
69
- // default: "true",
70
- // },
64
+ {
65
+ type: "list",
66
+ name: "routing",
67
+ message: "Which dolph routing architecture would you be using? (choosing 'spring' means that you are making use of both 'oop' & 'typescript')",
68
+ choices: ["express", "spring"],
69
+ default: "express",
70
+ },
71
71
  ])
72
72
  .then((replies) => {
73
73
  // TODO: add this to the config file:
74
- // # this is an auto generate file, please do not edit manually
74
+ const comment = "# this is an auto-generated file, please do not edit manually\n";
75
+ if ((replies.routing = "spring")) {
76
+ replies.paradigm = "oop";
77
+ replies.language = "ts";
78
+ }
75
79
  const userConfig = { ...defaultConfig, ...replies };
76
- const yamlString = yaml.dump(userConfig);
80
+ const yamlString = comment + yaml.dump(userConfig);
77
81
  writeFileSync(userConfigFilePath, yamlString, "utf8");
78
82
  if (userConfig.language === "ts") {
79
83
  writeSwcrc();
@@ -82,12 +86,12 @@ export const initDolphCli = (appname) => {
82
86
  writeDolphConfig();
83
87
  writePackageJsonFile(projectName, userConfig.language);
84
88
  writeGitignore();
85
- console.log(chalk.green(chalk.bold("dolph cli configurations have been initialized successfully 😎.")));
86
- console.log(chalk.green(chalk.bold("run yarn install to install dependencies in project directory and start coding 😉.")));
89
+ dolphMsg.infoBlue("dolph configurations have been initialized successfully ✨.");
90
+ dolphMsg.infoYellow("run `yarn install` to install dependencies in project directory and start building 🚀.");
87
91
  process.exit(0);
88
92
  });
89
93
  }
90
94
  else {
91
- console.log(chalk.bold(chalk.cyan("I see you already have your dolph cli configurations present 😉.")));
95
+ dolphMsg.info("I see you already have your dolph configurations present, so I'll abort gently ... 😉.");
92
96
  }
93
97
  };
@@ -3,5 +3,5 @@ import { program } from "commander";
3
3
  export const packageDescription = () => {
4
4
  program
5
5
  .version("1.1.0")
6
- .description(chalk.cyan("The dolphjs-cli is a tool for efficient intialization and management of a dolphjs project"));
6
+ .description(chalk.cyan("The dolph-cli is a tool for efficient intialization and management of a dolphjs project"));
7
7
  };
@@ -8,23 +8,18 @@ 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
10
  import { addServerFile } from "../registers/server_file_routes.js";
11
+ import { dolphMsg } from "../helpers/messages.js";
12
+ import { generateComponent } from "../generators/spring/generate_spring_component.js";
11
13
  export const packageGenerator = () => {
12
14
  program
13
15
  .command("generate")
14
16
  .alias("g")
15
17
  .option("-s, --service" + chalk.bold(chalk.blue("<name>")), "Generates a dolphjs service file.")
16
18
  .option("-c, --controller" + chalk.bold(chalk.blue("<name>")), "Generates a dolphjs controller file.")
17
- .option("-r, --routes" + chalk.bold(chalk.blue("<name>")), "Generates a dolphjs routes file.")
18
- .option("-m, --models" + chalk.bold(chalk.blue("<name>")), "Generates a dolphjs models file.")
19
- .option("-a, --all" + chalk.bold(chalk.blue("<name>")), "Generates dolphjs ocntrollers, routes, models and services files for the name paramter.")
20
- // .option(
21
- // "-in, --interfaces" + chalk.bold(chalk.blue("<name>")),
22
- // "Generates a dolphjs interfaces file."
23
- // )
24
- // .option(
25
- // "-hp, --helper" + chalk.bold(chalk.blue("<name>")),
26
- // "Generates a dolphjs helper file."
27
- // )
19
+ .option("-r, --route" + chalk.bold(chalk.blue("<name>")), "Generates a dolphjs routes file.")
20
+ .option("-m, --model" + chalk.bold(chalk.blue("<name>")), "Generates a dolphjs models file.")
21
+ .option("-com, --component" + chalk.bold(chalk.blue("<name>")), "Generates a dolphjs spring component file.")
22
+ .option("-a, --all" + chalk.bold(chalk.blue(" <name> ")), "Generates dolphjs controllers, routes, models and services files for the name paramter.")
28
23
  .action(async (name, _options) => {
29
24
  Object.entries(name).forEach(([key, value]) => {
30
25
  // generates controller files for the paramter name
@@ -36,17 +31,25 @@ export const packageGenerator = () => {
36
31
  serviceGen.generateService(value.toString());
37
32
  }
38
33
  // generatess routes files for the paramter name
39
- if (key && key.toLowerCase().includes("routes") && value) {
40
- routesGen.generateRouter(value.toString());
41
- addRoutesIndexFile(value.toString(), readConfig);
34
+ if (key && key.toLowerCase().includes("route") && value) {
35
+ if (readConfig().routing === "spring") {
36
+ dolphMsg.errorGray("cannot create routes file for spring routing.");
37
+ }
38
+ else {
39
+ routesGen.generateRouter(value.toString());
40
+ addRoutesIndexFile(value.toString(), readConfig);
41
+ }
42
42
  }
43
43
  // generatess model fles for the paramter name
44
- if (key && key.toLowerCase().includes("models") && value) {
44
+ if (key && key.toLowerCase().includes("model") && value) {
45
45
  modelGen.generateModel(value.toString());
46
46
  if (readConfig().database === "mysql") {
47
47
  mysqlGen.generateConfig(value.toString());
48
48
  }
49
49
  }
50
+ if (key && key.toLowerCase().includes("component") && value) {
51
+ generateComponent(value.toString());
52
+ }
50
53
  // generates files for all the above options
51
54
  if (key && key.toLowerCase().includes("all") && value) {
52
55
  modelGen.generateModel(value.toString());
@@ -55,9 +58,14 @@ export const packageGenerator = () => {
55
58
  }
56
59
  serviceGen.generateService(value.toString());
57
60
  controllerGen.generateController(value.toString());
58
- routesGen.generateRouter(value.toString());
59
- addRoutesIndexFile(value.toString(), readConfig);
60
- addServerFile(readConfig);
61
+ if (readConfig().routing === "express") {
62
+ routesGen.generateRouter(value.toString());
63
+ addRoutesIndexFile(value.toString(), readConfig);
64
+ addServerFile(readConfig);
65
+ }
66
+ else {
67
+ generateComponent(value.toString());
68
+ }
61
69
  }
62
70
  });
63
71
  // const controllerName = controller ? `${controller}` : undefined;
@@ -2,6 +2,7 @@ import { program } from "commander";
2
2
  import chalk from "chalk";
3
3
  import { readConfig } from "./read_user_config_path.js";
4
4
  import { writeConfig } from "./write_user_config.js";
5
+ import { dolphMsg } from "../helpers/messages.js";
5
6
  const dbOptions = ["mongo", "postgresql", "mysql", "other"];
6
7
  export const configurePackage = () => {
7
8
  program
@@ -18,63 +19,37 @@ export const configurePackage = () => {
18
19
  if (key && key.toLocaleLowerCase().includes("language") && value) {
19
20
  console.log(value.toString(), value);
20
21
  if (value.toString() !== "js" && value.toString() !== "ts") {
21
- console.log(chalk.red(`Invalid project language, format must be either ${chalk.bold("js")} or ${chalk.bold("ts")}`));
22
+ dolphMsg.errorRed(`invalid project language, format must be either ${chalk.bold("js")} or ${chalk.bold("ts")}`);
22
23
  return;
23
24
  }
24
25
  config.language = value;
25
26
  writeConfig(config);
26
- console.log(chalk.cyan(`Default project language is now ${chalk.bold(config.language)}`));
27
+ dolphMsg.info(`default project language is now ${chalk.bold(config.language)}`);
27
28
  return;
28
29
  }
29
30
  if (key && key.toLocaleLowerCase().includes("paradigm") && value) {
30
31
  if (value.toString() !== "oop" && value.toString() !== "functional") {
31
- console.log(chalk.red(`Invalid programming paradigm, must be either ${chalk.bold("oop")} or ${chalk.bold("functional")}`));
32
+ dolphMsg.errorRed(`invalid programming paradigm, must be either ${chalk.bold("oop")} or ${chalk.bold("functional")}`);
32
33
  return;
33
34
  }
34
35
  config.paradigm = value;
35
36
  writeConfig(config);
36
- console.log(chalk.cyan(`Default programming paradigm is now ${chalk.bold(config.paradigm)}`));
37
+ dolphMsg.info(`default programming paradigm is now ${chalk.bold(config.paradigm)}`);
37
38
  return;
38
39
  }
39
40
  if (key && key.toLocaleLowerCase().includes("database") && value) {
40
41
  if (!dbOptions.includes(value.toString())) {
41
- console.log(chalk.red(`Invalid database value, must be either of these: ${dbOptions.join(", ")} `));
42
+ dolphMsg.errorRed(`invalid database value, must be either of these: ${dbOptions.join(", ")} `);
42
43
  return;
43
44
  }
44
45
  config.database = value;
45
46
  writeConfig(config);
46
- console.log(chalk.cyan(`Default project database is now ${chalk.bold(config.database)}`));
47
+ dolphMsg.info(`default project database is now ${chalk.bold(config.database)}`);
47
48
  return;
48
49
  }
49
50
  });
50
- // validate generateFolder
51
- // if (options.generateFolder) {
52
- // if (
53
- // options.generateFolder !== "true" &&
54
- // options.generateFolder !== "false"
55
- // ) {
56
- // console.log(
57
- // chalk.red(
58
- // `Invalid folder generation value, value must be ${chalk.bold(
59
- // "true"
60
- // )} or ${chalk.bold("false")}`
61
- // )
62
- // );
63
- // return;
64
- // }
65
- // config.generateFolder = options.generateFolder;
66
- // writeConfig(config);
67
- // console.log(
68
- // chalk.cyan(
69
- // `Generation of folder files for the Services, Models, Controllers and Routes set to ${chalk.bold(
70
- // config.generateFolder
71
- // )}`
72
- // )
73
- // );
74
- // return;
75
- // }
76
51
  if (Object.keys(options).length === 0) {
77
- console.log(chalk.yellow(chalk.bold(`No configurations applied, run ${chalk.white("dolph config --help")} to see available commands`)));
52
+ dolphMsg.errorGray(`no configurations applied, run ${chalk.green("dolph config --help")} to see available commands`);
78
53
  }
79
54
  process.exit(0);
80
55
  });
@@ -27,7 +27,6 @@ export function writePackageJsonFile(name, extension) {
27
27
  "devDependencies": {
28
28
  "@swc/cli": "^0.1.62",
29
29
  "@swc/core": "^1.3.91",
30
- "@types/express": "^4.17.18",
31
30
  "@types/node": "^20.8.2",
32
31
  "ts-node": "^10.9.1",
33
32
  "tsc-alias": "^1.8.8",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dolphjs/cli",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
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",