@alevnyacow/nzmt 0.0.32 → 0.0.34
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/cli.js +64 -16
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -6,6 +6,11 @@ var args = process.argv.slice(2);
|
|
|
6
6
|
|
|
7
7
|
var [command, entityName, options = ''] = args;
|
|
8
8
|
|
|
9
|
+
function getImportName(str) {
|
|
10
|
+
const match = str.match(/import\s*{\s*([^}]+)\s*}/);
|
|
11
|
+
return match ? match[1].trim() : null;
|
|
12
|
+
}
|
|
13
|
+
|
|
9
14
|
function camelizeVariants(str) {
|
|
10
15
|
if (!str.includes('-')) {
|
|
11
16
|
return [str, str.substring(0, 1).toUpperCase() + str.substring(1)]
|
|
@@ -308,14 +313,16 @@ if (command === 'entity') {
|
|
|
308
313
|
}
|
|
309
314
|
|
|
310
315
|
function generateService(lowerCase, upperCase, withCrud) {
|
|
311
|
-
|
|
316
|
+
|
|
317
|
+
const serviceName = withCrud ? entityName + 's' : entityName
|
|
318
|
+
const folder = config?.paths?.services ? path.resolve(process.cwd(), config?.paths?.services, serviceName) : path.resolve(process.cwd(), serviceName);
|
|
312
319
|
|
|
313
320
|
fs.mkdirSync(folder, { recursive: true })
|
|
314
321
|
|
|
315
322
|
// Metadata
|
|
316
323
|
|
|
317
324
|
if (withCrud) {
|
|
318
|
-
fs.writeFileSync(path.resolve(folder, `${
|
|
325
|
+
fs.writeFileSync(path.resolve(folder, `${serviceName}.service.metadata.ts`), [
|
|
319
326
|
"import { type Module, Entities } from '@alevnyacow/nzmt'",
|
|
320
327
|
"import z from 'zod'",
|
|
321
328
|
`import { ${lowerCase}StoreMetadata } from '${config?.paths?.stores?.replace('./src', '@')}/${entityName}'`,
|
|
@@ -350,7 +357,7 @@ function generateService(lowerCase, upperCase, withCrud) {
|
|
|
350
357
|
`\t\tcreate: {`,
|
|
351
358
|
`\t\t\tpayload: z.object({`,
|
|
352
359
|
`\t\t\t\tpayload: ${lowerCase}StoreMetadata.actionsPayload.create`,
|
|
353
|
-
`\t\t\t},`,
|
|
360
|
+
`\t\t\t}),`,
|
|
354
361
|
`\t\t\tresponse: z.object({`,
|
|
355
362
|
`\t\t\t\tid: Entities.Identifier.schema`,
|
|
356
363
|
`\t\t\t}),`,
|
|
@@ -367,7 +374,7 @@ function generateService(lowerCase, upperCase, withCrud) {
|
|
|
367
374
|
`export type ${upperCase}sServiceDTOs = Module.DTOs<typeof ${lowerCase}sServiceMetadata>"`
|
|
368
375
|
].filter(x => typeof x === 'string').join('\n'))
|
|
369
376
|
} else {
|
|
370
|
-
fs.writeFileSync(path.resolve(folder, `${
|
|
377
|
+
fs.writeFileSync(path.resolve(folder, `${serviceName}.service.metadata.ts`), [
|
|
371
378
|
"import type { Module } from '@alevnyacow/nzmt'",
|
|
372
379
|
"",
|
|
373
380
|
`export const ${lowerCase}ServiceMetadata = {`,
|
|
@@ -381,22 +388,63 @@ function generateService(lowerCase, upperCase, withCrud) {
|
|
|
381
388
|
|
|
382
389
|
// Service body
|
|
383
390
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
391
|
+
if (withCrud) {
|
|
392
|
+
fs.writeFileSync(path.resolve(folder, `${serviceName}.service.ts`), [
|
|
393
|
+
config?.dependencyInjection?.inversifyjs ? "import { injectable, inject } from 'inversify'" : undefined,
|
|
394
|
+
config?.dependencyInjection?.storeTokensPrefix ?? undefined,
|
|
395
|
+
`import type { ${upperCase}Store } from '${config?.paths?.stores?.replace('./src', '@')}/${entityName}'`,
|
|
396
|
+
`import { ${lowerCase}ServiceMetadata } from './${entityName}.service.metadata'`,
|
|
397
|
+
"import { Module } from '@alevnyacow/nzmt'",
|
|
398
|
+
config?.dependencyInjection === 'inversifyjs' ? "@injectable()" : undefined,
|
|
399
|
+
`export class ${upperCase}Service {`,
|
|
400
|
+
`\tconstructor(`,
|
|
401
|
+
config?.dependencyInjection?.storeTokensPrefix ? `\t\t@inject(${getImportName(config?.dependencyInjection?.storeTokensPrefix)}.${lowerCase}s)` : undefined,
|
|
402
|
+
`\t\tprivate readonly ${lowerCase}s: ${upperCase}Store))`,
|
|
403
|
+
'\t) { }',
|
|
404
|
+
'\t',
|
|
405
|
+
`\tprivate method = Module.methods(${lowerCase}ServiceMetadata)`,
|
|
406
|
+
'\t',
|
|
407
|
+
`\tcreate = this.method('create', this.${lowerCase}s.create);`,
|
|
408
|
+
'\t',
|
|
409
|
+
`\tgetSpecific = this.method('getSpecific', async (x) => {`,
|
|
410
|
+
`\t\tconst item = await this.${lowerCase}s.details(x);`,
|
|
411
|
+
`\t\treturn { item };`,
|
|
412
|
+
`\t})`,
|
|
413
|
+
`\t`,
|
|
414
|
+
`\tgetList = this.method('getList', async (x) => {`,
|
|
415
|
+
`\t\tconst items = await this.${lowerCase}s.list(x);`,
|
|
416
|
+
`\t\treturn { items };`,
|
|
417
|
+
`\t})`,
|
|
418
|
+
`\t`,
|
|
419
|
+
`\tupdateOne = this.method('updateOne', async (x) => {`,
|
|
420
|
+
`\t\tawait this.${lowerCase}s.updateOne(x);`,
|
|
421
|
+
`\t\treturn {};`,
|
|
422
|
+
`\t})`,
|
|
423
|
+
`\t`,
|
|
424
|
+
`\tdeleteOne = this.method('deleteOne', async (x) => {`,
|
|
425
|
+
`\t\tawait this.${lowerCase}s.deleteOne(x);`,
|
|
426
|
+
`\t\treturn {};`,
|
|
427
|
+
`\t})`,
|
|
428
|
+
"}"
|
|
429
|
+
].filter(x => typeof x === 'string').join('\n'))
|
|
430
|
+
} else {
|
|
431
|
+
fs.writeFileSync(path.resolve(folder, `${serviceName}.service.ts`), [
|
|
432
|
+
config?.dependencyInjection?.inversifyjs ? "import { injectable } from 'inversify'" : undefined,
|
|
433
|
+
`import { ${lowerCase}ServiceMetadata } from './${entityName}.service.metadata'`,
|
|
434
|
+
"import { Module } from '@alevnyacow/nzmt'",
|
|
435
|
+
"",
|
|
436
|
+
config?.dependencyInjection === 'inversifyjs' ? "@injectable()" : undefined,
|
|
437
|
+
`export class ${upperCase}Service {`,
|
|
438
|
+
`\tprivate methods = Module.methods(${lowerCase}ServiceMetadata)`,
|
|
439
|
+
"}"
|
|
440
|
+
].filter(x => typeof x === 'string').join('\n'))
|
|
441
|
+
}
|
|
394
442
|
|
|
395
443
|
// Barrel
|
|
396
444
|
|
|
397
445
|
fs.writeFileSync(path.resolve(folder, 'index.ts'), [
|
|
398
|
-
`export * from ${
|
|
399
|
-
`export * from ${
|
|
446
|
+
`export * from ${serviceName}.service.metadata`,
|
|
447
|
+
`export * from ${serviceName}.service`
|
|
400
448
|
].join('\n'))
|
|
401
449
|
}
|
|
402
450
|
|