@dolphjs/cli 1.1.8 → 1.1.10
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.
- package/README.md +2 -2
- package/dist/generators/generate_controller.js +23 -21
- package/dist/generators/generate_model.js +27 -21
- package/dist/generators/generate_service.js +28 -22
- package/dist/generators/spring/generate_spring_model.js +42 -0
- package/dist/generators/spring/generate_spring_service.js +42 -0
- package/dist/generators/templates/spring_controller_template.js +1 -1
- package/dist/generators/templates/spring_model_template.js +40 -0
- package/dist/generators/templates/spring_service_template.js +86 -0
- package/dist/registers/register_component.js +65 -0
- package/dist/utils/package_generator.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ to install all packages .
|
|
|
27
27
|
- models
|
|
28
28
|
- routes
|
|
29
29
|
|
|
30
|
-
It is recommended to make use of the `generate` command when one
|
|
30
|
+
It is recommended to make use of the `generate` command when one wants to create these files because it generates code the dolphjs way. In a scenario when you want to create files for all of the above, let's say you want to write code for user logic and functionalities, use the `-a` flag, as in:
|
|
31
31
|
|
|
32
32
|
```bash
|
|
33
33
|
dolph-cli g -a user
|
|
@@ -61,4 +61,4 @@ the command above creates a service, controller, model and routes file for `user
|
|
|
61
61
|
|
|
62
62
|
## Credit
|
|
63
63
|
|
|
64
|
-
dolphjs-cli is inspired by methane-cli [https://github.com/adedoyin/methane-cli]
|
|
64
|
+
dolphjs-cli is inspired by methane-cli [https://github.com/adedoyin-Emmanuel/methane-cli]
|
|
@@ -38,27 +38,29 @@ export const generateController = async (name) => {
|
|
|
38
38
|
if (readConfig().routing === "spring") {
|
|
39
39
|
generateSpringController(name);
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const controllerDirName = path.join(controllerDir, name);
|
|
49
|
-
const controllerFilePath = path.join(controllerDirName + `/${name}.controller.${readConfig().language}`);
|
|
50
|
-
try {
|
|
51
|
-
// Create the generate controller path
|
|
52
|
-
if (readConfig().generateFolder === "true" || true) {
|
|
53
|
-
mkdirSync(controllerDirName);
|
|
41
|
+
else {
|
|
42
|
+
if (!name)
|
|
43
|
+
chalk.bold(chalk.red("Controller extension or name is required! 🤨"));
|
|
44
|
+
let controllerDir = findControllerDirectory();
|
|
45
|
+
if (!controllerDir) {
|
|
46
|
+
createControllerDirectory();
|
|
47
|
+
controllerDir = findControllerDirectory();
|
|
54
48
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
49
|
+
const controllerDirName = path.join(controllerDir, name);
|
|
50
|
+
const controllerFilePath = path.join(controllerDirName + `/${name}.controller.${readConfig().language}`);
|
|
51
|
+
try {
|
|
52
|
+
// Create the generate controller path
|
|
53
|
+
if (readConfig().generateFolder === "true" || true) {
|
|
54
|
+
mkdirSync(controllerDirName);
|
|
55
|
+
}
|
|
56
|
+
//TODO: if no index.ts file, create one too
|
|
57
|
+
generateControllerFile(name, path.join(controllerFilePath), readConfig
|
|
58
|
+
// controllerDirName,
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
catch (e) {
|
|
62
|
+
console.log(chalk.bold(chalk.red(e)));
|
|
63
|
+
}
|
|
64
|
+
console.log(`${chalk.bold(chalk.green(`${name}.controller.${readConfig().language} generated successfully! 🙃`))}`);
|
|
62
65
|
}
|
|
63
|
-
console.log(`${chalk.bold(chalk.green(`${name}.controller.${readConfig().language} generated successfully! 🙃`))}`);
|
|
64
66
|
};
|
|
@@ -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 { resolveModelContent } from "./resolvers/resolve_model_content.js";
|
|
6
|
+
import { generateModel as generateSpringModel } from "./spring/generate_spring_model.js";
|
|
6
7
|
const createModelDirectory = () => {
|
|
7
8
|
const projectRoot = path.join(process.cwd());
|
|
8
9
|
const userModelFilePath = path.join(projectRoot, "/src/models");
|
|
@@ -34,28 +35,33 @@ export const generateModelFile = async (name, modelDir, readConfig) => {
|
|
|
34
35
|
});
|
|
35
36
|
};
|
|
36
37
|
export const generateModel = async (name) => {
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
let modelDir = findModelDirectory();
|
|
40
|
-
if (!modelDir) {
|
|
41
|
-
//TODO: create one if it doesn't exist
|
|
42
|
-
// console.log(chalk.bold(chalk.red("Model directory doesn't exist 🤨")));
|
|
43
|
-
// return;
|
|
44
|
-
createModelDirectory();
|
|
45
|
-
modelDir = findModelDirectory();
|
|
38
|
+
if (readConfig().routing === "spring") {
|
|
39
|
+
generateSpringModel(name, readConfig().database === "mongo" ? true : false, readConfig().database === "mysql" ? true : false);
|
|
46
40
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
41
|
+
else {
|
|
42
|
+
if (!name)
|
|
43
|
+
chalk.bold(chalk.red("Model extension or name is required! 🤨"));
|
|
44
|
+
let modelDir = findModelDirectory();
|
|
45
|
+
if (!modelDir) {
|
|
46
|
+
//TODO: create one if it doesn't exist
|
|
47
|
+
// console.log(chalk.bold(chalk.red("Model directory doesn't exist 🤨")));
|
|
48
|
+
// return;
|
|
49
|
+
createModelDirectory();
|
|
50
|
+
modelDir = findModelDirectory();
|
|
51
|
+
}
|
|
52
|
+
const modelDirName = path.join(modelDir, name);
|
|
53
|
+
const modelFilePath = path.join(modelDirName + `/${name}.model.${readConfig().language}`);
|
|
54
|
+
try {
|
|
55
|
+
// Create the generate controller path
|
|
56
|
+
mkdirSync(modelDirName);
|
|
57
|
+
//TODO: if no index.ts file, create one too
|
|
58
|
+
generateModelFile(name, path.join(modelFilePath), readConfig
|
|
59
|
+
// modelDirName,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
console.log(chalk.bold(chalk.red(e)));
|
|
64
|
+
}
|
|
59
65
|
}
|
|
60
66
|
console.log(`${chalk.bold(chalk.green(`${name}.model.${readConfig().language} generated successfully! 🙃`))}`);
|
|
61
67
|
};
|
|
@@ -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 { resolveServiceContent } from "./resolvers/resolve_service_content.js";
|
|
6
|
+
import { generateService as generateSpringService } from "./spring/generate_spring_service.js";
|
|
6
7
|
const createServiceDirectory = () => {
|
|
7
8
|
const projectRoot = path.join(process.cwd());
|
|
8
9
|
const userServiceFilePath = path.join(projectRoot, "/src/services");
|
|
@@ -34,30 +35,35 @@ export const generateServiceFile = async (name, serviceDir, readConfig) => {
|
|
|
34
35
|
});
|
|
35
36
|
};
|
|
36
37
|
export const generateService = async (name) => {
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
let serviceDir = findServiceDirectory();
|
|
40
|
-
if (!serviceDir) {
|
|
41
|
-
//TODO: create one if it doesn't exist
|
|
42
|
-
// console.log(chalk.bold(chalk.red("Service directory doesn't exist 🤨")));
|
|
43
|
-
// return;
|
|
44
|
-
createServiceDirectory();
|
|
45
|
-
serviceDir = findServiceDirectory();
|
|
38
|
+
if (readConfig().routing === "spring") {
|
|
39
|
+
generateSpringService(name, readConfig().database === "mongo" ? true : false, readConfig().database === "mysql" ? true : false);
|
|
46
40
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (
|
|
52
|
-
|
|
41
|
+
{
|
|
42
|
+
if (!name)
|
|
43
|
+
chalk.bold(chalk.red("Service extension or name is required! 🤨"));
|
|
44
|
+
let serviceDir = findServiceDirectory();
|
|
45
|
+
if (!serviceDir) {
|
|
46
|
+
//TODO: create one if it doesn't exist
|
|
47
|
+
// console.log(chalk.bold(chalk.red("Service directory doesn't exist 🤨")));
|
|
48
|
+
// return;
|
|
49
|
+
createServiceDirectory();
|
|
50
|
+
serviceDir = findServiceDirectory();
|
|
51
|
+
}
|
|
52
|
+
const serviceDirName = path.join(serviceDir, name);
|
|
53
|
+
const serviceFilePath = path.join(serviceDirName + `/${name}.service.${readConfig().language}`);
|
|
54
|
+
try {
|
|
55
|
+
// Create the generate controller path
|
|
56
|
+
if (readConfig().generateFolder === "true" || true) {
|
|
57
|
+
mkdirSync(serviceDirName);
|
|
58
|
+
}
|
|
59
|
+
//TODO: if no index.ts file, create one too
|
|
60
|
+
generateServiceFile(name, path.join(serviceFilePath), readConfig
|
|
61
|
+
// serviceDirName,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
console.log(chalk.bold(chalk.red(e)));
|
|
53
66
|
}
|
|
54
|
-
//TODO: if no index.ts file, create one too
|
|
55
|
-
generateServiceFile(name, path.join(serviceFilePath), readConfig
|
|
56
|
-
// serviceDirName,
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
catch (e) {
|
|
60
|
-
console.log(chalk.bold(chalk.red(e)));
|
|
61
67
|
}
|
|
62
68
|
console.log(`${chalk.bold(chalk.green(`${name}.service.${readConfig().language} generated successfully! 🙃`))}`);
|
|
63
69
|
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFile } from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import chalk from "chalk";
|
|
4
|
+
import { generateSpringModel } from "../../generators/templates/spring_model_template.js";
|
|
5
|
+
import { dolphMsg } from "../../helpers/messages.js";
|
|
6
|
+
const createModelDirectory = (componentName) => {
|
|
7
|
+
const projectRoot = path.join(process.cwd());
|
|
8
|
+
const userModelPath = path.join(projectRoot, `/src/${componentName}`);
|
|
9
|
+
if (!existsSync(userModelPath)) {
|
|
10
|
+
mkdirSync(userModelPath);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const findModelDirectory = (componentName) => {
|
|
14
|
+
const rootDir = process.cwd();
|
|
15
|
+
const possibleDirs = [`src/${componentName}`];
|
|
16
|
+
const modelDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
|
|
17
|
+
return modelDir ? path.join(rootDir, modelDir) : null;
|
|
18
|
+
};
|
|
19
|
+
export const generateSpringModelFile = async (componentName, modelDir, isMongo, isMySql) => {
|
|
20
|
+
await writeFile(modelDir, generateSpringModel(componentName, isMongo, isMySql), (error) => {
|
|
21
|
+
if (error) {
|
|
22
|
+
dolphMsg.errorRed(error.toString());
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
export const generateModel = async (name, isMongo, isMySql) => {
|
|
27
|
+
if (!name)
|
|
28
|
+
dolphMsg.errorRed("model extension or name is required! 🤨");
|
|
29
|
+
let modelDir = findModelDirectory(name);
|
|
30
|
+
if (!modelDir) {
|
|
31
|
+
createModelDirectory(name);
|
|
32
|
+
modelDir = findModelDirectory(name);
|
|
33
|
+
}
|
|
34
|
+
const modelFilePath = path.join(modelDir + `/${name}.model.ts`);
|
|
35
|
+
try {
|
|
36
|
+
generateSpringModelFile(name, path.join(modelFilePath), isMongo, isMySql);
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
dolphMsg.errorRed(e);
|
|
40
|
+
}
|
|
41
|
+
dolphMsg.info(`${chalk.blue(`${name}.model.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 { generateSpringService } from "../../generators/templates/spring_service_template.js";
|
|
5
|
+
import { dolphMsg } from "../../helpers/messages.js";
|
|
6
|
+
const createServiceDirectory = (componentName) => {
|
|
7
|
+
const projectRoot = path.join(process.cwd());
|
|
8
|
+
const userServicePath = path.join(projectRoot, `/src/${componentName}`);
|
|
9
|
+
if (!existsSync(userServicePath)) {
|
|
10
|
+
mkdirSync(userServicePath);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const findServiceDirectory = (componentName) => {
|
|
14
|
+
const rootDir = process.cwd();
|
|
15
|
+
const possibleDirs = [`src/${componentName}`];
|
|
16
|
+
const serviceDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
|
|
17
|
+
return serviceDir ? path.join(rootDir, serviceDir) : null;
|
|
18
|
+
};
|
|
19
|
+
export const generateSpringServiceFile = async (componentName, serviceDir, isMongo, isMySql) => {
|
|
20
|
+
await writeFile(serviceDir, generateSpringService(componentName, isMongo, isMySql), (error) => {
|
|
21
|
+
if (error) {
|
|
22
|
+
dolphMsg.errorRed(error.toString());
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
export const generateService = async (name, isMongo, isMySql) => {
|
|
27
|
+
if (!name)
|
|
28
|
+
dolphMsg.errorRed("service extension or name is required! 🤨");
|
|
29
|
+
let serviceDir = findServiceDirectory(name);
|
|
30
|
+
if (!serviceDir) {
|
|
31
|
+
createServiceDirectory(name);
|
|
32
|
+
serviceDir = findServiceDirectory(name);
|
|
33
|
+
}
|
|
34
|
+
const serviceFilePath = path.join(serviceDir + `/${name}.service.ts`);
|
|
35
|
+
try {
|
|
36
|
+
generateSpringServiceFile(name, path.join(serviceFilePath), isMongo, isMySql);
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
dolphMsg.errorRed(e);
|
|
40
|
+
}
|
|
41
|
+
dolphMsg.info(`${chalk.blue(`${name}.service.ts`)} generated successfully for ${chalk.blue(`${name}`)} component.`);
|
|
42
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const generateSpringModel = (componentName, isMongo, isMysql) => {
|
|
2
|
+
let template;
|
|
3
|
+
const capitalizeFirstLetter = (str) => {
|
|
4
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
5
|
+
};
|
|
6
|
+
if (isMongo) {
|
|
7
|
+
template = `import { Schema, Document, model } from "mongoose";
|
|
8
|
+
|
|
9
|
+
export interface I${capitalizeFirstLetter(componentName)} extends Document {
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const ${capitalizeFirstLetter(componentName)}Schema = new Schema(
|
|
14
|
+
{
|
|
15
|
+
|
|
16
|
+
}
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
export const ${capitalizeFirstLetter(componentName)}Model = model<I${capitalizeFirstLetter(componentName)}>("${componentName.charAt(0) !== "s" ? `${componentName}s` : componentName}", ${capitalizeFirstLetter(componentName)}Schema);
|
|
20
|
+
`;
|
|
21
|
+
}
|
|
22
|
+
if (isMysql) {
|
|
23
|
+
template = `import { sequelizeInstance } from "@/shared/configs/db.configs";
|
|
24
|
+
import { DataTypes } from "sequelize";
|
|
25
|
+
|
|
26
|
+
export const ${capitalizeFirstLetter(componentName)} = sequelizeInstance.define("${componentName}", {
|
|
27
|
+
id: {
|
|
28
|
+
type: DataTypes.INTEGER,
|
|
29
|
+
allowNull: false,
|
|
30
|
+
primaryKey: true,
|
|
31
|
+
autoIncrement: true,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
`;
|
|
35
|
+
}
|
|
36
|
+
if (!isMongo && !isMysql) {
|
|
37
|
+
template = ``;
|
|
38
|
+
}
|
|
39
|
+
return template;
|
|
40
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export const generateSpringService = (componentName, isMongo, isMySql) => {
|
|
2
|
+
let template;
|
|
3
|
+
const capitalizeFirstLetter = (str) => {
|
|
4
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
5
|
+
};
|
|
6
|
+
const className = capitalizeFirstLetter(componentName) + "Service";
|
|
7
|
+
if (isMongo && isMySql) {
|
|
8
|
+
template = `
|
|
9
|
+
import { DolphServiceHandler } from "@dolphjs/dolph/classes";
|
|
10
|
+
import { Dolph } from "@dolphjs/dolph/common";
|
|
11
|
+
import { InjectMongo, InjectMySQL } from "@dolphjs/dolph/decorators";
|
|
12
|
+
import { Model } from "mongoose";
|
|
13
|
+
import { ModelStatic, Model as SqlModel } from "sequelize";
|
|
14
|
+
import { ${capitalizeFirstLetter(componentName)}Model, I${capitalizeFirstLetter(componentName)} } from "./${componentName}.model";
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@InjectMongo("${componentName}Model", ${capitalizeFirstLetter(componentName)}Model)
|
|
18
|
+
@InjectMySQL("${componentName}SqlModel", )
|
|
19
|
+
export class ${className} extends DolphServiceHandler<Dolph> {
|
|
20
|
+
${componentName}Model!: Model<${capitalizeFirstLetter(componentName)}>;
|
|
21
|
+
${componentName}SqlModel!: ModelStatic<SqlModel<any, any>>;
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
constructor() {
|
|
25
|
+
super("${className.toLowerCase()}");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
`;
|
|
30
|
+
}
|
|
31
|
+
if (isMongo) {
|
|
32
|
+
template = `
|
|
33
|
+
import { DolphServiceHandler } from "@dolphjs/dolph/classes";
|
|
34
|
+
import { Dolph } from "@dolphjs/dolph/common";
|
|
35
|
+
import { InjectMongo} from "@dolphjs/dolph/decorators";
|
|
36
|
+
import { Model } from "mongoose";
|
|
37
|
+
import { ${capitalizeFirstLetter(componentName)}Model, I${capitalizeFirstLetter(componentName)} } from "./${componentName}.model";
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@InjectMongo("${componentName}Model", ${capitalizeFirstLetter(componentName)}Model)
|
|
41
|
+
export class ${className} extends DolphServiceHandler<Dolph> {
|
|
42
|
+
${componentName}Model!: Model<>;
|
|
43
|
+
|
|
44
|
+
constructor() {
|
|
45
|
+
super("${className.toLowerCase()}");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
`;
|
|
50
|
+
}
|
|
51
|
+
if (isMySql) {
|
|
52
|
+
template = `
|
|
53
|
+
import { DolphServiceHandler } from "@dolphjs/dolph/classes";
|
|
54
|
+
import { Dolph } from "@dolphjs/dolph/common";
|
|
55
|
+
import { InjectMySQL } from "@dolphjs/dolph/decorators";
|
|
56
|
+
import { ModelStatic, Model } from "sequelize";
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@InjectMySQL("${componentName}Model", )
|
|
60
|
+
export class ${className} extends DolphServiceHandler<Dolph> {
|
|
61
|
+
${componentName}Model!: ModelStatic<Model<any, any>>;
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
constructor() {
|
|
65
|
+
super("${className.toLowerCase()}");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
`;
|
|
70
|
+
}
|
|
71
|
+
if (!isMongo && !isMySql) {
|
|
72
|
+
template = `
|
|
73
|
+
import { DolphServiceHandler } from "@dolphjs/dolph/classes";
|
|
74
|
+
import { Dolph } from "@dolphjs/dolph/common";
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
export class ${className} extends DolphServiceHandler<Dolph> {
|
|
78
|
+
|
|
79
|
+
constructor() {
|
|
80
|
+
super("${className.toLowerCase()}");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
`;
|
|
84
|
+
}
|
|
85
|
+
return template;
|
|
86
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { dolphMsg } from "../helpers/messages.js";
|
|
3
|
+
import path from "path";
|
|
4
|
+
const createSrcDirectory = () => {
|
|
5
|
+
const projectRoot = path.join(process.cwd());
|
|
6
|
+
const userComponentPath = path.join(projectRoot, `/src`);
|
|
7
|
+
if (!existsSync(userComponentPath)) {
|
|
8
|
+
mkdirSync(userComponentPath);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const findSrcDirectory = () => {
|
|
12
|
+
const rootDir = process.cwd();
|
|
13
|
+
const possibleDirs = [`src`];
|
|
14
|
+
const srcDir = possibleDirs.find((dir) => existsSync(path.join(rootDir, dir)));
|
|
15
|
+
return srcDir ? path.join(rootDir, srcDir) : null;
|
|
16
|
+
};
|
|
17
|
+
export const addComponentToServerFile = (componentName) => {
|
|
18
|
+
if (!componentName) {
|
|
19
|
+
dolphMsg.errorRed("component extension or name is required! 🤨");
|
|
20
|
+
}
|
|
21
|
+
const serverDir = findSrcDirectory();
|
|
22
|
+
if (!serverDir) {
|
|
23
|
+
dolphMsg.errorRed("something went wrong, please make sure to use the `new` command when creating a dolph project.");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const serverPath = path.join(serverDir, "server.ts");
|
|
27
|
+
const capitalizedString = componentName.charAt(0).toUpperCase() + componentName.slice(1);
|
|
28
|
+
const importStatement = `import { ${capitalizedString}Component } from "./${componentName}/${componentName}.component";`;
|
|
29
|
+
try {
|
|
30
|
+
if (!existsSync(serverPath)) {
|
|
31
|
+
const fileContent = `import { DolphFactory } from "@dolphjs/dolph";
|
|
32
|
+
${importStatement}
|
|
33
|
+
const dolph = new DolphFactory([${capitalizedString}Component]);
|
|
34
|
+
dolph.start();
|
|
35
|
+
`;
|
|
36
|
+
writeFileSync(serverPath, fileContent);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const fileContent = readFileSync(serverPath, "utf8");
|
|
40
|
+
const newComponentInstance = `${capitalizedString}Component`;
|
|
41
|
+
if (!fileContent.includes(importStatement)) {
|
|
42
|
+
const updatedContent = `${importStatement}\n${fileContent}`;
|
|
43
|
+
writeFileSync(serverPath, updatedContent);
|
|
44
|
+
}
|
|
45
|
+
if (fileContent.includes(`const dolph`)) {
|
|
46
|
+
// add new controller class to the existing array
|
|
47
|
+
const routesArrayMatch = fileContent.match(/const dolph = new DolphFactory\(([^)]+)\);/);
|
|
48
|
+
if (routesArrayMatch) {
|
|
49
|
+
const existingCode = routesArrayMatch && routesArrayMatch[1]
|
|
50
|
+
? routesArrayMatch[1].trim()
|
|
51
|
+
: "";
|
|
52
|
+
let fileContents;
|
|
53
|
+
fileContents = fileContent.replace(existingCode, `${existingCode.length > 0 ? existingCode + ", " : ""}${newComponentInstance}`);
|
|
54
|
+
const updatedContents = `${importStatement}\n${fileContents}`;
|
|
55
|
+
writeFileSync(serverPath, updatedContents);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
dolphMsg.info(`added ${capitalizedString}Component to components array in server.ts file.`);
|
|
60
|
+
process.exit(0);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
dolphMsg.errorRed(e.toString());
|
|
64
|
+
}
|
|
65
|
+
};
|
|
@@ -10,6 +10,7 @@ import { addRoutesIndexFile } from "../registers/register_routes.js";
|
|
|
10
10
|
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
|
+
import { addComponentToServerFile } from "../registers/register_component.js";
|
|
13
14
|
export const packageGenerator = () => {
|
|
14
15
|
program
|
|
15
16
|
.command("generate")
|
|
@@ -65,6 +66,7 @@ export const packageGenerator = () => {
|
|
|
65
66
|
}
|
|
66
67
|
else {
|
|
67
68
|
generateComponent(value.toString());
|
|
69
|
+
addComponentToServerFile(value.toString());
|
|
68
70
|
}
|
|
69
71
|
}
|
|
70
72
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dolphjs/cli",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.10",
|
|
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",
|