@avleon/cli 0.0.4 → 0.0.5
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/dist/av-cli.js +60 -0
- package/dist/cli.interface.js +2 -0
- package/dist/cli.js +240 -0
- package/dist/commands.js +54 -0
- package/dist/controller.js +116 -0
- package/dist/create.js +140 -0
- package/dist/index.js +17 -0
- package/dist/paths.js +22 -0
- package/package.json +1 -1
- package/src/create.ts +21 -12
package/dist/av-cli.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.PathExtractor = PathExtractor;
|
|
5
|
+
function formatFileName(name) {
|
|
6
|
+
return name
|
|
7
|
+
.replace(/[_\s]+/g, "-")
|
|
8
|
+
.replace(/([a-z])([A-Z])/g, "$1-$2")
|
|
9
|
+
.replace(/((-|_|\.)+(Controller|Service|Model))|(Controller|Service|Model)/gi, "")
|
|
10
|
+
.toLowerCase();
|
|
11
|
+
}
|
|
12
|
+
function formatClassName(name) {
|
|
13
|
+
return name
|
|
14
|
+
.replace(/[_\s-]+/g, " ")
|
|
15
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
16
|
+
.toLowerCase()
|
|
17
|
+
.replace(/\b\w/g, (char) => char.toUpperCase())
|
|
18
|
+
.replace(/\s+/g, "");
|
|
19
|
+
}
|
|
20
|
+
function PathExtractor(givenPath, options) {
|
|
21
|
+
let pathSplits = givenPath.split("/");
|
|
22
|
+
let splitSize = pathSplits.length;
|
|
23
|
+
let hasSubDirectory = splitSize > 0;
|
|
24
|
+
let name = hasSubDirectory ? pathSplits[splitSize - 1] : pathSplits[0];
|
|
25
|
+
let className = formatClassName(name);
|
|
26
|
+
let fileType = "any";
|
|
27
|
+
let isRest = false;
|
|
28
|
+
let isForced = false;
|
|
29
|
+
if (options.includes("make:controller") || options.includes("m:c")) {
|
|
30
|
+
fileType = "controller";
|
|
31
|
+
className = name.toLowerCase().replace(/controller|Controller/g, "");
|
|
32
|
+
}
|
|
33
|
+
if (options.includes("make:model") || options.includes("m:m")) {
|
|
34
|
+
fileType = "model";
|
|
35
|
+
className = name.toLowerCase().replace(/controller|Controller/g, "");
|
|
36
|
+
}
|
|
37
|
+
if (options.includes("make:service") || options.includes("m:s")) {
|
|
38
|
+
fileType = "service";
|
|
39
|
+
className = name.toLowerCase().replace(/controller|Controller/g, "");
|
|
40
|
+
}
|
|
41
|
+
if (options.includes("make:util") || options.includes("m:u")) {
|
|
42
|
+
fileType = "util";
|
|
43
|
+
className = name.toLowerCase().replace(/controller|Controller/g, "");
|
|
44
|
+
}
|
|
45
|
+
if (options.includes("make:config")) {
|
|
46
|
+
fileType = "config";
|
|
47
|
+
className = name.toLowerCase().replace(/controller|Controller/g, "");
|
|
48
|
+
}
|
|
49
|
+
if (options.includes("--rest") || options.includes("-r")) {
|
|
50
|
+
isRest = true;
|
|
51
|
+
className = name.toLowerCase().replace(/controller|Controller/g, "");
|
|
52
|
+
}
|
|
53
|
+
if (options.includes("--force") || options.includes("-f")) {
|
|
54
|
+
isForced = true;
|
|
55
|
+
className = name.toLowerCase().replace(/controller|Controller/g, "");
|
|
56
|
+
}
|
|
57
|
+
let outputFileName = formatFileName(name);
|
|
58
|
+
let outputPath = givenPath + "." + fileType + ".ts";
|
|
59
|
+
return {};
|
|
60
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.iqraCli = iqraCli;
|
|
41
|
+
exports.run = run;
|
|
42
|
+
const cli_table3_1 = __importDefault(require("cli-table3"));
|
|
43
|
+
const path_1 = __importDefault(require("path"));
|
|
44
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
45
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
46
|
+
const fs_1 = require("fs");
|
|
47
|
+
const prettier = __importStar(require("prettier"));
|
|
48
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
49
|
+
const controller_1 = require("./controller");
|
|
50
|
+
const commands_1 = require("./commands");
|
|
51
|
+
const child_process_1 = require("child_process");
|
|
52
|
+
const create_1 = require("./create");
|
|
53
|
+
function iqraCli() { }
|
|
54
|
+
const listedCommands = commands_1.commandList.flatMap(x => x.Command).concat(commands_1.commandList.flatMap(x => x.Alias.split(","))).flatMap(a => a.trim());
|
|
55
|
+
/**
|
|
56
|
+
* @description Displaying basic info
|
|
57
|
+
* @since v0.0.2
|
|
58
|
+
* */
|
|
59
|
+
async function printLogo() {
|
|
60
|
+
try {
|
|
61
|
+
const packageInfo = await promises_1.default.readFile(path_1.default.join(__dirname, "../package.json"), "utf8");
|
|
62
|
+
const parsedPackInfo = JSON.parse(packageInfo);
|
|
63
|
+
console.log(`Name: Avleon CLI`, "\r");
|
|
64
|
+
console.log(`Version: ${parsedPackInfo.version}`, "\r");
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw new Error("Can't read packag info");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function displayCommandList(info = { logo: true, all: false }) {
|
|
71
|
+
if (info.logo) {
|
|
72
|
+
await printLogo();
|
|
73
|
+
}
|
|
74
|
+
console.log("Available commandList:");
|
|
75
|
+
if (info.all) {
|
|
76
|
+
const table = new cli_table3_1.default({
|
|
77
|
+
head: ["Command", "Alias", "Available Options", "Description"],
|
|
78
|
+
colWidths: [20, 10, 30, 50],
|
|
79
|
+
wrapOnWordBoundary: true,
|
|
80
|
+
wordWrap: true,
|
|
81
|
+
});
|
|
82
|
+
commands_1.commandList.forEach((c) => {
|
|
83
|
+
table.push([c.Command, c.Alias, c.Options, c.Description]);
|
|
84
|
+
});
|
|
85
|
+
console.log(table.toString());
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
const table = new cli_table3_1.default({
|
|
89
|
+
head: ["Command", "Alias", "Available Options"],
|
|
90
|
+
colWidths: [20, 10, 50],
|
|
91
|
+
wrapOnWordBoundary: true,
|
|
92
|
+
wordWrap: true,
|
|
93
|
+
});
|
|
94
|
+
commands_1.commandList.forEach((c) => {
|
|
95
|
+
table.push([c.Command, c.Alias, c.Options]);
|
|
96
|
+
});
|
|
97
|
+
console.log(table.toString());
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function createModel(name, options = { force: false }) {
|
|
101
|
+
if (!name) {
|
|
102
|
+
throw new Error("Model name not found . use --name or node artisan make:model ModelName");
|
|
103
|
+
}
|
|
104
|
+
const modelFolderExists = (0, fs_1.existsSync)(path_1.default.join(process.cwd(), "./src/models"));
|
|
105
|
+
if (!modelFolderExists) {
|
|
106
|
+
await promises_1.default.mkdir(path_1.default.join(process.cwd(), "./src/models"), {
|
|
107
|
+
recursive: true,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
const modelName = name.at(0).toUpperCase() + name.slice(1);
|
|
111
|
+
const modelExists = (0, fs_1.existsSync)(path_1.default.join(process.cwd(), `./src/models/${modelName.toLowerCase()}.ts`));
|
|
112
|
+
if (modelExists && !options.force) {
|
|
113
|
+
console.log(chalk_1.default.bold(chalk_1.default.red("Model already exists!")), chalk_1.default.greenBright("Use -f or --force to overwrite.\r"));
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const model = `
|
|
117
|
+
import {Entity, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
|
118
|
+
\t
|
|
119
|
+
@Entity({name:'${modelName}'})
|
|
120
|
+
export class ${modelName}{
|
|
121
|
+
|
|
122
|
+
@PrimaryGeneratedColumn()
|
|
123
|
+
id:number;
|
|
124
|
+
|
|
125
|
+
@CreateDateColumn()
|
|
126
|
+
createdAt: Date;
|
|
127
|
+
|
|
128
|
+
@UpdateDateColumn()
|
|
129
|
+
updatedAt: Date;
|
|
130
|
+
}
|
|
131
|
+
`;
|
|
132
|
+
const formatModel = await prettier.format(model, {
|
|
133
|
+
singleQuote: true,
|
|
134
|
+
singleAttributePerLine: true,
|
|
135
|
+
parser: "typescript",
|
|
136
|
+
});
|
|
137
|
+
await promises_1.default.writeFile(path_1.default.join(process.cwd(), "./src/models/" + modelName.toLowerCase() + ".ts"), formatModel);
|
|
138
|
+
console.log("Model created succssessfully!");
|
|
139
|
+
}
|
|
140
|
+
async function createXApplication(fname) {
|
|
141
|
+
if ((0, fs_1.existsSync)(process.cwd() + fname)) {
|
|
142
|
+
throw new Error("Application already exists");
|
|
143
|
+
}
|
|
144
|
+
const projectPath = path_1.default.join(process.cwd(), fname);
|
|
145
|
+
const templateDir = path_1.default.resolve("D:\\projects\\node\\iqra\\packages\\cli\\src\\stubs");
|
|
146
|
+
//await fs.mkdir(projectPath);
|
|
147
|
+
//await fsExtra.copy(templateDir, projectPath + '/');
|
|
148
|
+
// Define project path
|
|
149
|
+
// const projectPath = path.resolve('new_test');
|
|
150
|
+
// Clone the repository and move the required folder
|
|
151
|
+
const cloneCommand = `
|
|
152
|
+
git clone -n --depth=1 --filter=tree:0 https://github.com/xtareq/iqra temp_repo &&
|
|
153
|
+
cd temp_repo &&
|
|
154
|
+
git sparse-checkout init --no-cone &&
|
|
155
|
+
git sparse-checkout set examples/iqra_test &&
|
|
156
|
+
git checkout &&
|
|
157
|
+
mv examples/iqra_test ${projectPath} &&
|
|
158
|
+
cd ..
|
|
159
|
+
`;
|
|
160
|
+
try {
|
|
161
|
+
// Execute the cloning and moving process
|
|
162
|
+
console.log('Cloning and setting up the repository...');
|
|
163
|
+
(0, child_process_1.exec)(cloneCommand, {});
|
|
164
|
+
// Check if the projectPath exists before proceeding
|
|
165
|
+
if (!fs_extra_1.default.existsSync(projectPath)) {
|
|
166
|
+
throw new Error(`Project path "${projectPath}" was not created.`);
|
|
167
|
+
}
|
|
168
|
+
// Remove the Git origin from the new project
|
|
169
|
+
console.log('Removing Git origin...');
|
|
170
|
+
(0, child_process_1.execSync)(`cd ${projectPath} && git remote remove origin`, { stdio: 'inherit' });
|
|
171
|
+
// Clean up the temporary repository
|
|
172
|
+
console.log('Cleaning up temporary files...');
|
|
173
|
+
fs_extra_1.default.removeSync('./temp_repo');
|
|
174
|
+
console.log(`Project created successfully in ${projectPath}`);
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
console.error('An error occurred:', error);
|
|
178
|
+
// Cleanup in case of failure
|
|
179
|
+
if (fs_extra_1.default.existsSync('./temp_repo')) {
|
|
180
|
+
console.log('Cleaning up temporary repository...');
|
|
181
|
+
fs_extra_1.default.removeSync('./temp_repo');
|
|
182
|
+
}
|
|
183
|
+
if (fs_extra_1.default.existsSync(projectPath)) {
|
|
184
|
+
console.log('Cleaning up project path...');
|
|
185
|
+
fs_extra_1.default.removeSync(projectPath);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
async function run() {
|
|
190
|
+
try {
|
|
191
|
+
const args = process.argv.slice(2);
|
|
192
|
+
if (args.length < 1) {
|
|
193
|
+
await displayCommandList({ logo: true, all: false });
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
const cmdList = commands_1.commandList
|
|
197
|
+
.flatMap((x) => x.Command)
|
|
198
|
+
.concat(...commands_1.commandList.flatMap((f) => f.Alias.split(",").map((s) => s.trim())));
|
|
199
|
+
if (!listedCommands.includes(args[0].trim())) {
|
|
200
|
+
console.error("Invalid command");
|
|
201
|
+
await displayCommandList({ logo: false, all: false });
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
let force = false;
|
|
205
|
+
if (args.some((x) => x == "-f" || x == "--force")) {
|
|
206
|
+
force = true;
|
|
207
|
+
}
|
|
208
|
+
if (args[0] == "new") {
|
|
209
|
+
const fname = args[1];
|
|
210
|
+
await (0, create_1.createApplication)(fname);
|
|
211
|
+
}
|
|
212
|
+
if (args[0] == "make:model") {
|
|
213
|
+
await createModel(args[1], { force });
|
|
214
|
+
}
|
|
215
|
+
if (args[0] == "make:controller" || args[0] == "m:c") {
|
|
216
|
+
let resource = false;
|
|
217
|
+
if (args.some((x) => x == "-r" || x == "--resource")) {
|
|
218
|
+
resource = true;
|
|
219
|
+
}
|
|
220
|
+
let model = null;
|
|
221
|
+
if (args.some((x) => x == "-m" || x == "--model")) {
|
|
222
|
+
const index = args.includes("-m")
|
|
223
|
+
? args.indexOf("-m")
|
|
224
|
+
: args.indexOf("--model");
|
|
225
|
+
if (args[index + 1] == undefined) {
|
|
226
|
+
throw new Error("Model not defined");
|
|
227
|
+
}
|
|
228
|
+
const modelName = args[index + 1];
|
|
229
|
+
model = modelName;
|
|
230
|
+
}
|
|
231
|
+
await (0, controller_1.createController)(args[1], { force, resource, model });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
console.log("CLI ERROR:", error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
run();
|
package/dist/commands.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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.commandList = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
exports.commandList = [
|
|
9
|
+
{
|
|
10
|
+
Command: "new",
|
|
11
|
+
Alias: "",
|
|
12
|
+
Options: "--minimal -m, -d --database",
|
|
13
|
+
Description: "Create a new application"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
Command: "make:model",
|
|
17
|
+
Alias: "m:m, mm",
|
|
18
|
+
Options: "-f --force",
|
|
19
|
+
Description: "Create a new model. Use -f or --froce to overwrite if any model exists with name",
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
Command: "make:controller",
|
|
23
|
+
Alias: "m:c",
|
|
24
|
+
Options: `-f, --force for force create\n -r or --resource for resource controller`,
|
|
25
|
+
Description: `Create a new controller. Use -f or --force to overwrite existing controller. If you want to create a resource controller pass options -r or --resource.
|
|
26
|
+
You may also pass model by -m or --model.\n
|
|
27
|
+
${chalk_1.default.blueBright("Note:")} when using a resource contorller iqra will automaticall check for dtos related to model name. If -m is user then
|
|
28
|
+
it will check for dtos like UserCreateDto and UserUpdateDto. If dtos not found it will create automatically using
|
|
29
|
+
model properties.
|
|
30
|
+
`,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
Command: "make:service",
|
|
34
|
+
Alias: "m:s",
|
|
35
|
+
Options: `-f, --force for force create\n -r or --resource for resource controller`,
|
|
36
|
+
Description: `Create a new controller. Use -f or --force to overwrite existing controller. If you want to create a resource controller pass options -r or --resource.
|
|
37
|
+
You may also pass model by -m or --model.\n
|
|
38
|
+
${chalk_1.default.blueBright("Note:")} when using a resource contorller iqra will automaticall check for dtos related to model name. If -m is user then
|
|
39
|
+
it will check for dtos like UserCreateDto and UserUpdateDto. If dtos not found it will create automatically using
|
|
40
|
+
model properties.
|
|
41
|
+
`,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
Command: "make:resource",
|
|
45
|
+
Alias: "m:r",
|
|
46
|
+
Options: `-f, --force for force create\n -r or --resource for resource`,
|
|
47
|
+
Description: `Create a new resource. Use -f or --force to overwrite existing controller. If you want to create a resource controller pass options -r or --resource.
|
|
48
|
+
You may also pass model by -m or --model.\n
|
|
49
|
+
${chalk_1.default.blueBright("Note:")} when using a resource contorller iqra will automaticall check for dtos related to model name. If -m is user then
|
|
50
|
+
it will check for dtos like UserCreateDto and UserUpdateDto. If dtos not found it will create automatically using
|
|
51
|
+
model properties.
|
|
52
|
+
`,
|
|
53
|
+
},
|
|
54
|
+
];
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.createController = createController;
|
|
40
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
41
|
+
const fs_1 = require("fs");
|
|
42
|
+
const promises_1 = require("fs/promises");
|
|
43
|
+
const path_1 = __importDefault(require("path"));
|
|
44
|
+
const prettier_1 = __importDefault(require("prettier"));
|
|
45
|
+
async function createController(name, options = { force: false, resource: false, model: null }, p) {
|
|
46
|
+
if (!name) {
|
|
47
|
+
throw new Error("Controller name not found. use --name or node artisan make:controller HomeController");
|
|
48
|
+
}
|
|
49
|
+
const controllerPath = path_1.default.join(process.cwd(), "./src/controllers");
|
|
50
|
+
const controllerFolderExists = (0, fs_1.existsSync)(controllerPath);
|
|
51
|
+
if (!controllerFolderExists) {
|
|
52
|
+
await (0, promises_1.mkdir)(controllerPath, {
|
|
53
|
+
recursive: true,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const controllerName = name.at(0).toUpperCase() + name.slice(1);
|
|
57
|
+
const controllerNameLowerCase = name.replace(/Controller/ig, '').match(/[A-Z][a-z]+|[0-9]+/g).join("-").toLowerCase();
|
|
58
|
+
const controllerNameCap = controllerName.replace(/Controller/ig, '');
|
|
59
|
+
const controllerExists = (0, fs_1.existsSync)(`${controllerPath}/${name.toLowerCase()}.controller.ts`);
|
|
60
|
+
if (controllerExists && !options.force) {
|
|
61
|
+
console.log(chalk_1.default.bold(chalk_1.default.red("controller already exists!")), chalk_1.default.greenBright("Use -f or --force to overwrite.\r"));
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (options.model) {
|
|
65
|
+
const file = await Promise.resolve(`${path_1.default.join(process.cwd(), "./src/models/" + options.model.toUpperCase() + ".ts")}`).then(s => __importStar(require(s)));
|
|
66
|
+
if (file) {
|
|
67
|
+
Object.getOwnPropertyNames(file);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const restController = `
|
|
71
|
+
import { ApiController, Get, Post, Put, Delete, Param, Body } from '@avleon/core';
|
|
72
|
+
\t
|
|
73
|
+
@ApiController("/${controllerNameLowerCase}")
|
|
74
|
+
export class ${controllerName}{\n
|
|
75
|
+
@Get()
|
|
76
|
+
async findAll(){
|
|
77
|
+
return "okay";
|
|
78
|
+
}\n
|
|
79
|
+
@Get('/:id')
|
|
80
|
+
async findOne(@Param('id') id:number){
|
|
81
|
+
return "okay";
|
|
82
|
+
}\n
|
|
83
|
+
@Post()
|
|
84
|
+
async create(@Body() body:any){
|
|
85
|
+
return true;
|
|
86
|
+
}\n
|
|
87
|
+
@Put('/:id')
|
|
88
|
+
async update(@Param('id') id:number, @Body() body:any){
|
|
89
|
+
return true;
|
|
90
|
+
}\n
|
|
91
|
+
@Delete('/:id')
|
|
92
|
+
async delete(@Param('id') id:number){
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
}
|
|
97
|
+
`;
|
|
98
|
+
const basicContorller = `import {ApiController, Get} from '@avleon/core'
|
|
99
|
+
\n
|
|
100
|
+
@ApiController("/${controllerNameLowerCase}")
|
|
101
|
+
export class ${controllerName}{
|
|
102
|
+
\n
|
|
103
|
+
@Get()
|
|
104
|
+
index(){
|
|
105
|
+
return []
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
`;
|
|
109
|
+
const formatcontroller = await prettier_1.default.format(options.resource ? restController : basicContorller, {
|
|
110
|
+
singleQuote: true,
|
|
111
|
+
singleAttributePerLine: true,
|
|
112
|
+
parser: "typescript",
|
|
113
|
+
});
|
|
114
|
+
await (0, promises_1.writeFile)(path_1.default.join(process.cwd(), "./src/controllers/" + controllerName.toLowerCase().replace("controller", "") + ".controller." + ".ts"), formatcontroller);
|
|
115
|
+
console.log("Controller created succssessfully!");
|
|
116
|
+
}
|
package/dist/create.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.createApplication = createApplication;
|
|
40
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
41
|
+
const paths_1 = require("./paths");
|
|
42
|
+
const prettier = __importStar(require("prettier"));
|
|
43
|
+
const win32_1 = __importDefault(require("path/win32"));
|
|
44
|
+
const child_process_1 = require("child_process");
|
|
45
|
+
async function createApplication(fname) {
|
|
46
|
+
const appPaths = (0, paths_1.appPaths)(fname);
|
|
47
|
+
const dirs = Object.values(appPaths);
|
|
48
|
+
for (let i = 0; i < dirs.length; i++) {
|
|
49
|
+
await fs_extra_1.default.ensureDir(dirs[i]);
|
|
50
|
+
}
|
|
51
|
+
await fs_extra_1.default.ensureDir(appPaths.root + "/public");
|
|
52
|
+
// copy
|
|
53
|
+
//
|
|
54
|
+
const mainFunc = `
|
|
55
|
+
import {
|
|
56
|
+
Builder,
|
|
57
|
+
env
|
|
58
|
+
} from "@avleon/core";
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
// create builder
|
|
62
|
+
const builder = Builder.createAppBuilder();
|
|
63
|
+
|
|
64
|
+
// registering all services and dependencies here
|
|
65
|
+
// builder.addDataSource({
|
|
66
|
+
// type: "better-sqlite3",
|
|
67
|
+
// database: path.join(process.cwd(), "mydb.db"),
|
|
68
|
+
// entities: [Category, Product],
|
|
69
|
+
// synchronize: true,
|
|
70
|
+
// });
|
|
71
|
+
|
|
72
|
+
// build app
|
|
73
|
+
const app = builder.build();
|
|
74
|
+
app.mapGet('/', ()=> "Welcome");
|
|
75
|
+
|
|
76
|
+
app.listen(env.PORT);
|
|
77
|
+
|
|
78
|
+
`;
|
|
79
|
+
const formatMainFunc = await prettier.format(mainFunc, {
|
|
80
|
+
singleQuote: true,
|
|
81
|
+
singleAttributePerLine: true,
|
|
82
|
+
parser: "typescript",
|
|
83
|
+
});
|
|
84
|
+
await fs_extra_1.default.writeFile(win32_1.default.join(appPaths.src, 'app.ts'), formatMainFunc);
|
|
85
|
+
// create package.json
|
|
86
|
+
const packageJson = `
|
|
87
|
+
{
|
|
88
|
+
"name": "${fname}",
|
|
89
|
+
"version": "1.0.0",
|
|
90
|
+
"description": "",
|
|
91
|
+
"main": "index.js",
|
|
92
|
+
"scripts": {
|
|
93
|
+
"test": "echo \\"Error: no test specified\\" && exit 1",
|
|
94
|
+
"dev": "nodemon src/app.ts",
|
|
95
|
+
"start": "node dist/app.js",
|
|
96
|
+
"build": "rimraf dist && tsc"
|
|
97
|
+
},
|
|
98
|
+
"keywords": [],
|
|
99
|
+
"author": "",
|
|
100
|
+
"license": "ISC",
|
|
101
|
+
"dependencies": {
|
|
102
|
+
"class-transformer": "^0.5.1",
|
|
103
|
+
"class-validator": "^0.14.1",
|
|
104
|
+
"reflect-metadata": "^0.2.2"
|
|
105
|
+
},
|
|
106
|
+
"devDependencies": {
|
|
107
|
+
"nodemon": "^3.1.7",
|
|
108
|
+
"ts-node": "^10.9.2",
|
|
109
|
+
"typescript": "^5.7.2"
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
const formattedPackageJson = await prettier.format(packageJson, {
|
|
115
|
+
parser: "json",
|
|
116
|
+
});
|
|
117
|
+
await fs_extra_1.default.writeFile(win32_1.default.join(appPaths.root, 'package.json'), formattedPackageJson);
|
|
118
|
+
const tsconfigJson = await prettier.format(`{
|
|
119
|
+
"compilerOptions": {
|
|
120
|
+
"target": "ES2017",
|
|
121
|
+
"module": "CommonJS",
|
|
122
|
+
"moduleResolution": "node",
|
|
123
|
+
"outDir": "./dist",
|
|
124
|
+
"rootDir": "./src",
|
|
125
|
+
"esModuleInterop": true,
|
|
126
|
+
"allowSyntheticDefaultImports": true,
|
|
127
|
+
"forceConsistentCasingInFileNames": true,
|
|
128
|
+
"skipLibCheck": true
|
|
129
|
+
},
|
|
130
|
+
"include": ["src/**/*"],
|
|
131
|
+
"exclude": ["node_modules", "dist"]
|
|
132
|
+
}`, { parser: 'json' });
|
|
133
|
+
await fs_extra_1.default.writeFile(win32_1.default.join(appPaths.root, 'tsconfig.json'), tsconfigJson);
|
|
134
|
+
await fs_extra_1.default.writeFile(win32_1.default.join(appPaths.root, '.gitignore'), `node_modules/\ndist/\n.env`);
|
|
135
|
+
// create basic service
|
|
136
|
+
// create basic controller
|
|
137
|
+
console.info("Running pnpm install");
|
|
138
|
+
(0, child_process_1.execSync)(`cd ${fname}&& pnpm install && pnpm link @avleon/core`, { stdio: 'inherit' });
|
|
139
|
+
console.info("App create successfully.");
|
|
140
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./cli"), exports);
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
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.appPaths = void 0;
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const appPaths = (rootPath) => {
|
|
9
|
+
const root = rootPath ? rootPath : node_path_1.default.join(process.cwd(), "./");
|
|
10
|
+
return {
|
|
11
|
+
root: root,
|
|
12
|
+
src: node_path_1.default.join(root, "src"),
|
|
13
|
+
test: node_path_1.default.join(root, "test"),
|
|
14
|
+
controllers: node_path_1.default.join(root, "src/controllers"),
|
|
15
|
+
models: node_path_1.default.join(root, "src/models"),
|
|
16
|
+
services: node_path_1.default.join(root, "src/services"),
|
|
17
|
+
requests: node_path_1.default.join(root, "src/requests"),
|
|
18
|
+
responses: node_path_1.default.join(root, "src/responses"),
|
|
19
|
+
configs: node_path_1.default.join(root, "src/configs")
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
exports.appPaths = appPaths;
|
package/package.json
CHANGED
package/src/create.ts
CHANGED
|
@@ -19,20 +19,29 @@ async function createApplication(fname: any) {
|
|
|
19
19
|
// copy
|
|
20
20
|
//
|
|
21
21
|
const mainFunc = `
|
|
22
|
-
import
|
|
23
|
-
|
|
22
|
+
import {
|
|
23
|
+
Builder,
|
|
24
|
+
env
|
|
25
|
+
} from "@avleon/core";
|
|
24
26
|
|
|
25
|
-
async function main() {
|
|
26
|
-
const builder = AppBuilder.createBuilder();
|
|
27
|
-
// builder.useLogger();
|
|
28
|
-
builder.useDatabase();
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
// create builder
|
|
29
|
+
const builder = Builder.createAppBuilder();
|
|
30
|
+
|
|
31
|
+
// registering all services and dependencies here
|
|
32
|
+
// builder.addDataSource({
|
|
33
|
+
// type: "better-sqlite3",
|
|
34
|
+
// database: path.join(process.cwd(), "mydb.db"),
|
|
35
|
+
// entities: [Category, Product],
|
|
36
|
+
// synchronize: true,
|
|
37
|
+
// });
|
|
38
|
+
|
|
39
|
+
// build app
|
|
40
|
+
const app = builder.build();
|
|
41
|
+
app.mapGet('/', ()=> "Welcome");
|
|
42
|
+
|
|
43
|
+
app.listen(env.PORT);
|
|
33
44
|
|
|
34
|
-
}
|
|
35
|
-
main().catch(err=> console.log(err));
|
|
36
45
|
`
|
|
37
46
|
|
|
38
47
|
const formatMainFunc = await prettier.format(
|
|
@@ -105,7 +114,7 @@ main().catch(err=> console.log(err));
|
|
|
105
114
|
|
|
106
115
|
|
|
107
116
|
console.info("Running pnpm install");
|
|
108
|
-
execSync(`cd ${fname}&& pnpm install && pnpm link @avleon/core
|
|
117
|
+
execSync(`cd ${fname}&& pnpm install && pnpm link @avleon/core`, { stdio: 'inherit' })
|
|
109
118
|
|
|
110
119
|
console.info("App create successfully.");
|
|
111
120
|
|