@alevnyacow/nzmt 0.0.33 → 0.0.35
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 +61 -15
- 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)]
|
|
@@ -103,7 +108,6 @@ function generateStores(lowerCase, upperCase) {
|
|
|
103
108
|
// Contract
|
|
104
109
|
|
|
105
110
|
fs.writeFileSync(path.resolve(folder, `${entityName}.store.ts`), [
|
|
106
|
-
"import z from 'zod'",
|
|
107
111
|
"import type { Store } from '@alevnyacow/nzmt'",
|
|
108
112
|
config?.paths?.entities ? `import { ${upperCase} } from '${config?.paths?.entities.replace('./src', '@')}/${entityName}'` : undefined,
|
|
109
113
|
"",
|
|
@@ -362,11 +366,11 @@ function generateService(lowerCase, upperCase, withCrud) {
|
|
|
362
366
|
`\t\t\t\tfilter: ${lowerCase}StoreMetadata.searchPayload.specific`,
|
|
363
367
|
`\t\t\t}),`,
|
|
364
368
|
`\t\t\tresponse: z.object({})`,
|
|
365
|
-
`\t\t}
|
|
369
|
+
`\t\t},`,
|
|
366
370
|
"\t}",
|
|
367
371
|
"} satisfies Module.Metadata",
|
|
368
372
|
"",
|
|
369
|
-
`export type ${upperCase}sServiceDTOs = Module.DTOs<typeof ${lowerCase}sServiceMetadata
|
|
373
|
+
`export type ${upperCase}sServiceDTOs = Module.DTOs<typeof ${lowerCase}sServiceMetadata>`
|
|
370
374
|
].filter(x => typeof x === 'string').join('\n'))
|
|
371
375
|
} else {
|
|
372
376
|
fs.writeFileSync(path.resolve(folder, `${serviceName}.service.metadata.ts`), [
|
|
@@ -383,22 +387,64 @@ function generateService(lowerCase, upperCase, withCrud) {
|
|
|
383
387
|
|
|
384
388
|
// Service body
|
|
385
389
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
390
|
+
if (withCrud) {
|
|
391
|
+
const [upperCaseService, lowerCaseService] = camelizeVariants(serviceName)
|
|
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 { ${lowerCaseService}ServiceMetadata } from './${serviceName}.service.metadata'`,
|
|
397
|
+
"import { Module } from '@alevnyacow/nzmt'",
|
|
398
|
+
config?.dependencyInjection?.inversifyjs ? "@injectable()" : undefined,
|
|
399
|
+
`export class ${upperCaseService}Service {`,
|
|
400
|
+
`\tconstructor(`,
|
|
401
|
+
config?.dependencyInjection?.inversifyjs?.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
|
+
}
|
|
396
442
|
|
|
397
443
|
// Barrel
|
|
398
444
|
|
|
399
445
|
fs.writeFileSync(path.resolve(folder, 'index.ts'), [
|
|
400
|
-
`export * from
|
|
401
|
-
`export * from
|
|
446
|
+
`export * from './${serviceName}.service.metadata'`,
|
|
447
|
+
`export * from './${serviceName}.service'`
|
|
402
448
|
].join('\n'))
|
|
403
449
|
}
|
|
404
450
|
|