@azure-net/cli 1.0.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/azure-net.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
import generateRepository from '../src/commands/generateCrudRepo.js';
|
|
5
|
+
import generateService from '../src/commands/generateCrudService.js';
|
|
6
|
+
|
|
7
|
+
const program = new Command();
|
|
8
|
+
|
|
9
|
+
program
|
|
10
|
+
.name('azure-net')
|
|
11
|
+
.description('Universal code generator')
|
|
12
|
+
.version('1.0.0');
|
|
13
|
+
|
|
14
|
+
program.command('generate:crud-repository')
|
|
15
|
+
.description('Generate a CRUD repository')
|
|
16
|
+
.action(generateRepository);
|
|
17
|
+
|
|
18
|
+
program.command('generate:crud-service')
|
|
19
|
+
.description('Generate a service class')
|
|
20
|
+
.action(generateService);
|
|
21
|
+
|
|
22
|
+
program.parse();
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@azure-net/cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"bin": {
|
|
6
|
+
"azure-net": "bin/azure-net.js"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@azure-net/kit": "^0.1.3",
|
|
10
|
+
"commander": "^11",
|
|
11
|
+
"ejs": "^3",
|
|
12
|
+
"prompts": "^2"
|
|
13
|
+
},
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node": "^24.0.3",
|
|
16
|
+
"typescript": "^5"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import prompts from 'prompts';
|
|
2
|
+
import ejs from 'ejs';
|
|
3
|
+
import fs from 'fs/promises';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { fileURLToPath } from 'url';
|
|
6
|
+
import { loadUserConfig } from "../utils/loadConfig.js";
|
|
7
|
+
|
|
8
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
|
|
10
|
+
export default async function generateCrudRepository() {
|
|
11
|
+
const config = loadUserConfig();
|
|
12
|
+
const answers = await prompts([
|
|
13
|
+
{ type: 'text', name: 'className', message: 'Class name:' },
|
|
14
|
+
{ type: 'text', name: 'endpoint', message: 'API endpoint:' },
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
const templatePath = path.join(__dirname, '../templates/generateCrudRepo.ejs');
|
|
18
|
+
const template = await fs.readFile(templatePath, 'utf-8');
|
|
19
|
+
const result = ejs.render(template, answers);
|
|
20
|
+
|
|
21
|
+
const outputDir = config.output.repository;
|
|
22
|
+
const outFile = path.join(process.cwd(), outputDir, `${answers.className}.ts`);
|
|
23
|
+
await fs.mkdir(path.dirname(outFile), { recursive: true });
|
|
24
|
+
await fs.writeFile(outFile, result, 'utf-8');
|
|
25
|
+
|
|
26
|
+
console.log(`✅ Created: ${outFile}`);
|
|
27
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BaseDatasource } from '@azure-net/kit';
|
|
2
|
+
|
|
3
|
+
export class <%= className %>Repo {
|
|
4
|
+
private endpoint = '<%= endpoint %>';
|
|
5
|
+
constructor(private source: BaseDatasource) {}
|
|
6
|
+
|
|
7
|
+
public async collection() {
|
|
8
|
+
return await this.source.createRequest<unknown[]>(({ http }) => http.get(this.endpoint));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public async resource(id: number) {
|
|
12
|
+
return await this.source.createRequest<unknown>(({ http }) => http.get(`${this.endpoint}/${id}`));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async create(data: unknown) {
|
|
16
|
+
return await this.source.createRequest<unknown>(({ http }) => http.post(this.endpoint, { json: data }));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public async update(id: number, data: unknown) {
|
|
20
|
+
return await this.source.createRequest<unknown>(({ http }) => http.put(`${this.endpoint}/${id}`, { json: data }));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async delete(id: number) {
|
|
24
|
+
return await this.source.createRequest<unknown>(({ http }) => http.delete(`${this.endpoint}/${id}`));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { pathToFileURL } from 'url';
|
|
3
|
+
|
|
4
|
+
export async function loadUserConfig() {
|
|
5
|
+
try {
|
|
6
|
+
const configPath = path.resolve(process.cwd(), 'azure-net.config.js');
|
|
7
|
+
const userConfig = await import(pathToFileURL(configPath));
|
|
8
|
+
return userConfig.default || {};
|
|
9
|
+
} catch {
|
|
10
|
+
return {
|
|
11
|
+
output: {
|
|
12
|
+
repository: 'src/repositories',
|
|
13
|
+
service: 'src/services'
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|