@expressots/cli 1.1.1 → 1.2.0
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/bin/generate/cli.js +3 -3
- package/bin/generate/form.js +26 -15
- package/bin/new/cli.js +17 -4
- package/bin/new/form.d.ts +10 -1
- package/bin/new/form.js +48 -32
- package/bin/utils/add-controller-to-module.d.ts +2 -0
- package/bin/utils/add-controller-to-module.js +40 -0
- package/bin/utils/verify-file-exists.d.ts +2 -0
- package/bin/utils/verify-file-exists.js +27 -0
- package/package.json +1 -1
package/bin/generate/cli.js
CHANGED
|
@@ -8,7 +8,7 @@ const form_1 = require("./form");
|
|
|
8
8
|
const chalk_1 = __importDefault(require("chalk"));
|
|
9
9
|
const generateProject = () => {
|
|
10
10
|
return {
|
|
11
|
-
command: "generate [schematic] [path
|
|
11
|
+
command: "generate [schematic] [path]",
|
|
12
12
|
describe: "Generate a schematic",
|
|
13
13
|
aliases: ["g"],
|
|
14
14
|
builder: (yargs) => {
|
|
@@ -24,14 +24,14 @@ const generateProject = () => {
|
|
|
24
24
|
type: "string",
|
|
25
25
|
coerce: coerceSchematicAliases,
|
|
26
26
|
});
|
|
27
|
-
yargs.positional("path
|
|
27
|
+
yargs.positional("path", {
|
|
28
28
|
describe: "The path to generate the schematic",
|
|
29
29
|
type: "string",
|
|
30
30
|
});
|
|
31
31
|
return yargs;
|
|
32
32
|
},
|
|
33
33
|
handler: async ({ schematic, path }) => {
|
|
34
|
-
const file = await (0, form_1.createTemplate)({ schematic, path
|
|
34
|
+
const file = await (0, form_1.createTemplate)({ schematic, path });
|
|
35
35
|
console.log(chalk_1.default.green(`> ${file.split(".")[0]} ${schematic} created! 🚀`));
|
|
36
36
|
},
|
|
37
37
|
};
|
package/bin/generate/form.js
CHANGED
|
@@ -41,6 +41,8 @@ const fs_1 = require("fs");
|
|
|
41
41
|
const chalk_1 = __importDefault(require("chalk"));
|
|
42
42
|
const boost_ts_1 = require("@expressots/boost-ts");
|
|
43
43
|
const compiler_1 = __importDefault(require("../utils/compiler"));
|
|
44
|
+
const add_controller_to_module_1 = require("../utils/add-controller-to-module");
|
|
45
|
+
const verify_file_exists_1 = require("../utils/verify-file-exists");
|
|
44
46
|
function getFileNameWithoutExtension(filePath) {
|
|
45
47
|
return filePath.split('.')[0];
|
|
46
48
|
}
|
|
@@ -60,6 +62,7 @@ const createTemplate = async ({ schematic, path: target, }) => {
|
|
|
60
62
|
const { path, file, className } = await splitTarget({ target, schematic });
|
|
61
63
|
const { sourceRoot } = await compiler_1.default.loadConfig();
|
|
62
64
|
const usecaseDir = `${sourceRoot}/${withinSource}`;
|
|
65
|
+
await (0, verify_file_exists_1.verifyIfFileExists)(`${usecaseDir}/${path}${file}`);
|
|
63
66
|
(0, node_fs_1.mkdirSync)(`${usecaseDir}/${path}`, { recursive: true });
|
|
64
67
|
if (schematic !== "service") {
|
|
65
68
|
console.log(messageColors[schematic](`> [${schematic}] Creating ${file}...`));
|
|
@@ -81,11 +84,12 @@ const createTemplate = async ({ schematic, path: target, }) => {
|
|
|
81
84
|
_c = _f.value;
|
|
82
85
|
_d = false;
|
|
83
86
|
try {
|
|
84
|
-
const
|
|
87
|
+
const resource = _c;
|
|
88
|
+
const currentSchematic = resource.replace("controller-service", "controller");
|
|
85
89
|
const schematicFile = file.replace(`controller.ts`, `${currentSchematic}.ts`);
|
|
86
|
-
console.log(messageColors[currentSchematic](`> [${currentSchematic}] Creating ${schematicFile
|
|
90
|
+
console.log(messageColors[currentSchematic](`> [${currentSchematic}] Creating ${schematicFile}...`));
|
|
87
91
|
writeTemplate({
|
|
88
|
-
outputPath: `${usecaseDir}/${path}${schematicFile
|
|
92
|
+
outputPath: `${usecaseDir}/${path}${schematicFile}`,
|
|
89
93
|
template: {
|
|
90
94
|
path: `./templates/${currentSchematic}.tpl`,
|
|
91
95
|
data: {
|
|
@@ -112,19 +116,26 @@ const createTemplate = async ({ schematic, path: target, }) => {
|
|
|
112
116
|
}
|
|
113
117
|
}
|
|
114
118
|
const moduleName = path.split("/")[0];
|
|
115
|
-
if (["controller", "service"].includes(schematic)
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
if (["controller", "service"].includes(schematic)) {
|
|
120
|
+
if ((0, fs_1.existsSync)(`${usecaseDir}/${moduleName}/${moduleName}.module.ts`)) {
|
|
121
|
+
console.log(messageColors.module(`> [module] Adding controller to ${moduleName}.module.ts...`));
|
|
122
|
+
const controllerPath = `./${path.split("/")[1]}/${file.slice(0, file.lastIndexOf('.'))}`;
|
|
123
|
+
await (0, add_controller_to_module_1.addControllerToModule)(`${usecaseDir}/${moduleName}/${moduleName}.module.ts`, `${className}Controller`, controllerPath);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
console.log(messageColors.module(`> [module] Creating ${moduleName}.module.ts...`));
|
|
127
|
+
writeTemplate({
|
|
128
|
+
outputPath: `${usecaseDir}/${moduleName}/${moduleName}.module.ts`,
|
|
129
|
+
template: {
|
|
130
|
+
path: `./templates/module.tpl`,
|
|
131
|
+
data: {
|
|
132
|
+
moduleName: moduleName[0].toUpperCase() + moduleName.slice(1),
|
|
133
|
+
className,
|
|
134
|
+
path: `${path.split("/")[1]}/${file.slice(0, file.lastIndexOf('.'))}`
|
|
135
|
+
},
|
|
125
136
|
},
|
|
126
|
-
}
|
|
127
|
-
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
128
139
|
}
|
|
129
140
|
return file;
|
|
130
141
|
};
|
package/bin/new/cli.js
CHANGED
|
@@ -4,17 +4,30 @@ exports.createProject = void 0;
|
|
|
4
4
|
const form_1 = require("./form");
|
|
5
5
|
const createProject = () => {
|
|
6
6
|
return {
|
|
7
|
-
command: "new
|
|
7
|
+
command: "new <project-name> [package-manager] [template]",
|
|
8
8
|
describe: "Create a new Expresso TS project",
|
|
9
9
|
builder: (yargs) => {
|
|
10
|
-
yargs
|
|
10
|
+
yargs
|
|
11
|
+
.positional("project-name", {
|
|
11
12
|
describe: "The name of the project",
|
|
12
13
|
type: "string",
|
|
14
|
+
})
|
|
15
|
+
.option("template", {
|
|
16
|
+
describe: "The project template to use",
|
|
17
|
+
type: "string",
|
|
18
|
+
choices: ["non-opinionated", "opinionated"],
|
|
19
|
+
alias: "t",
|
|
20
|
+
})
|
|
21
|
+
.option("package-manager", {
|
|
22
|
+
describe: "The package manager to use",
|
|
23
|
+
type: "string",
|
|
24
|
+
choices: ["npm", "yarn", "pnpm"],
|
|
25
|
+
alias: "p",
|
|
13
26
|
});
|
|
14
27
|
return yargs;
|
|
15
28
|
},
|
|
16
|
-
handler: async ({ projectName }) => {
|
|
17
|
-
return await (0, form_1.projectForm)(projectName);
|
|
29
|
+
handler: async ({ projectName, packageManager, template }) => {
|
|
30
|
+
return await (0, form_1.projectForm)(projectName, packageManager, template);
|
|
18
31
|
},
|
|
19
32
|
};
|
|
20
33
|
};
|
package/bin/new/form.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
declare
|
|
1
|
+
declare enum Template {
|
|
2
|
+
"non-opinionated" = "Non-Opinionated :: A simple ExpressoTS project.",
|
|
3
|
+
opinionated = "Opinionated :: A complete ExpressoTS project with an opinionated structure and features."
|
|
4
|
+
}
|
|
5
|
+
declare const enum PackageManager {
|
|
6
|
+
npm = 0,
|
|
7
|
+
yarn = 1,
|
|
8
|
+
pnpm = 2
|
|
9
|
+
}
|
|
10
|
+
declare const projectForm: (projectName: string, packageManager: PackageManager, template: keyof typeof Template) => Promise<void>;
|
|
2
11
|
export { projectForm };
|
package/bin/new/form.js
CHANGED
|
@@ -57,39 +57,55 @@ function changePackageName({ directory, name, }) {
|
|
|
57
57
|
// Save the package.json file
|
|
58
58
|
node_fs_1.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
59
59
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
var Template;
|
|
61
|
+
(function (Template) {
|
|
62
|
+
Template["non-opinionated"] = "Non-Opinionated :: A simple ExpressoTS project.";
|
|
63
|
+
Template["opinionated"] = "Opinionated :: A complete ExpressoTS project with an opinionated structure and features.";
|
|
64
|
+
})(Template || (Template = {}));
|
|
65
|
+
const projectForm = async (projectName, packageManager, template) => {
|
|
66
|
+
let answer;
|
|
67
|
+
if (packageManager && template) {
|
|
68
|
+
answer = {
|
|
69
|
+
name: projectName,
|
|
70
|
+
packageManager: packageManager,
|
|
71
|
+
template: Template[template],
|
|
72
|
+
confirm: true,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
answer = await inquirer_1.default.prompt([
|
|
77
|
+
{
|
|
78
|
+
type: "input",
|
|
79
|
+
name: "name",
|
|
80
|
+
message: "Project name",
|
|
81
|
+
default: projectName,
|
|
82
|
+
transformer: (input) => {
|
|
83
|
+
return chalk_1.default.yellow(chalk_1.default.bold(input));
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
type: "list",
|
|
88
|
+
name: "packageManager",
|
|
89
|
+
message: "Package manager",
|
|
90
|
+
choices: ["npm", "yarn", "pnpm"],
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
type: "list",
|
|
94
|
+
name: "template",
|
|
95
|
+
message: "Select a template",
|
|
96
|
+
choices: [
|
|
97
|
+
"Non-Opinionated :: A simple ExpressoTS project.",
|
|
98
|
+
"Opinionated :: A complete ExpressoTS project with an opinionated structure and features.",
|
|
99
|
+
],
|
|
69
100
|
},
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
type: "list",
|
|
79
|
-
name: "template",
|
|
80
|
-
message: "Select a template",
|
|
81
|
-
choices: [
|
|
82
|
-
"Non-Opinionated :: A simple ExpressoTS project.",
|
|
83
|
-
"Opinionated :: A complete ExpressoTS project with an opinionated structure and features.",
|
|
84
|
-
],
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
type: "confirm",
|
|
88
|
-
name: "confirm",
|
|
89
|
-
message: "Do you want to create this project?",
|
|
90
|
-
default: true,
|
|
91
|
-
},
|
|
92
|
-
]);
|
|
101
|
+
{
|
|
102
|
+
type: "confirm",
|
|
103
|
+
name: "confirm",
|
|
104
|
+
message: "Do you want to create this project?",
|
|
105
|
+
default: true,
|
|
106
|
+
},
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
93
109
|
// Hashmap of templates and their directories
|
|
94
110
|
const templates = {
|
|
95
111
|
"Non-Opinionated": "non_opinionated",
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.addControllerToModule = void 0;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
async function addControllerToModule(filePath, controllerName, controllerPath) {
|
|
9
|
+
const fileContent = await node_fs_1.default.promises.readFile(filePath, 'utf8');
|
|
10
|
+
const imports = [];
|
|
11
|
+
const notImports = [];
|
|
12
|
+
fileContent.split('\n').forEach((line) => {
|
|
13
|
+
if (line.startsWith('import')) {
|
|
14
|
+
imports.push(line);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
notImports.push(line);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const newImport = `import { ${controllerName} } from "${controllerPath}";`;
|
|
21
|
+
if (imports.includes(newImport)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
imports.push(newImport);
|
|
25
|
+
const moduleDeclarationRegex = /CreateModule\(\[(.*)\]\);/s;
|
|
26
|
+
const moduleDeclarationMatch = fileContent.match(moduleDeclarationRegex);
|
|
27
|
+
if (!moduleDeclarationMatch) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const controllers = moduleDeclarationMatch[1].trim().split(',').map((c) => c.trim());
|
|
31
|
+
if (controllers.includes(controllerName)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
controllers.push(controllerName);
|
|
35
|
+
const newControllers = controllers.join(', ');
|
|
36
|
+
const newModuleDeclaration = `CreateModule([${newControllers}]);`;
|
|
37
|
+
const newFileContent = [...imports, ...notImports].join('\n').replace(moduleDeclarationRegex, newModuleDeclaration);
|
|
38
|
+
await node_fs_1.default.promises.writeFile(filePath, newFileContent, 'utf8');
|
|
39
|
+
}
|
|
40
|
+
exports.addControllerToModule = addControllerToModule;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.verifyIfFileExists = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const inquirer_1 = __importDefault(require("inquirer"));
|
|
9
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
10
|
+
async function verifyIfFileExists(path) {
|
|
11
|
+
const fileExists = node_fs_1.default.existsSync(path);
|
|
12
|
+
if (fileExists) {
|
|
13
|
+
const answer = await inquirer_1.default.prompt([
|
|
14
|
+
{
|
|
15
|
+
type: "confirm",
|
|
16
|
+
name: "confirm",
|
|
17
|
+
message: "File with this path already exists. Do you want to create it anyway?",
|
|
18
|
+
default: true,
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
if (!answer.confirm) {
|
|
22
|
+
console.log(chalk_1.default.green('> File not created!'));
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.verifyIfFileExists = verifyIfFileExists;
|