speedrun-cli 2.6.5 → 2.6.7
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/package.json +1 -1
- package/src/moduleGenerator.js +46 -4
package/package.json
CHANGED
package/src/moduleGenerator.js
CHANGED
|
@@ -386,6 +386,42 @@ export class ${pascalName}Module {}
|
|
|
386
386
|
`;
|
|
387
387
|
}
|
|
388
388
|
|
|
389
|
+
async function registerInAppModule(targetDir, pascalName, kebabName) {
|
|
390
|
+
try {
|
|
391
|
+
const appModulePath = path.join(targetDir, 'src', 'app.module.ts');
|
|
392
|
+
|
|
393
|
+
if (!(await fs.pathExists(appModulePath))) {
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
let content = await fs.readFile(appModulePath, 'utf8');
|
|
398
|
+
const moduleImport = `import { ${pascalName}Module } from './modules/${kebabName}/${kebabName}.module';`;
|
|
399
|
+
|
|
400
|
+
// Cegah duplikasi import
|
|
401
|
+
if (content.includes(moduleImport) || content.includes(`${pascalName}Module`)) {
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// 1. Tambahkan baris import di paling atas file
|
|
406
|
+
content = `${moduleImport}\n` + content;
|
|
407
|
+
|
|
408
|
+
// 2. Inject ${pascalName}Module tepat di dalam decorator @Module({ imports: [ ... ] })
|
|
409
|
+
const importsKeywordIndex = content.indexOf('imports: [');
|
|
410
|
+
|
|
411
|
+
if (importsKeywordIndex !== -1) {
|
|
412
|
+
const insertPosition = importsKeywordIndex + 'imports: ['.length;
|
|
413
|
+
content = content.slice(0, insertPosition) + `\n ${pascalName}Module,` + content.slice(insertPosition);
|
|
414
|
+
|
|
415
|
+
await fs.writeFile(appModulePath, content, 'utf8');
|
|
416
|
+
console.log(chalk.green(`✨ Automatically registered ${pascalName}Module in src/app.module.ts`));
|
|
417
|
+
return true;
|
|
418
|
+
}
|
|
419
|
+
} catch (error) {
|
|
420
|
+
console.warn(chalk.yellow(`⚠️ Could not auto-register ${pascalName}Module in app.module.ts: ${error.message}`));
|
|
421
|
+
}
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
|
|
389
425
|
async function generateModule(providedModuleName, targetDir = process.cwd(), specifiedOrm = null) {
|
|
390
426
|
try {
|
|
391
427
|
const options = await promptForModuleOptions(providedModuleName);
|
|
@@ -396,7 +432,7 @@ async function generateModule(providedModuleName, targetDir = process.cwd(), spe
|
|
|
396
432
|
// Detect ORM
|
|
397
433
|
const orm = specifiedOrm || await detectOrm(targetDir);
|
|
398
434
|
|
|
399
|
-
// Ensure
|
|
435
|
+
// Ensure inside a NestJS project structure
|
|
400
436
|
const srcDir = path.join(targetDir, 'src');
|
|
401
437
|
if (!(await fs.pathExists(srcDir))) {
|
|
402
438
|
console.warn(chalk.yellow('⚠️ Could not find "src" directory. Generating at current directory.'));
|
|
@@ -481,7 +517,7 @@ export class ${responseDtoName} {
|
|
|
481
517
|
const serviceContent = getServiceContent(orm, pascalName, camelName, kebabName, createDtoName, updateDtoName, ops);
|
|
482
518
|
await fs.writeFile(path.join(moduleDir, `${kebabName}.service.ts`), serviceContent);
|
|
483
519
|
|
|
484
|
-
// 3. Generate Controller
|
|
520
|
+
// 3. Generate Controller
|
|
485
521
|
const controllerContent = `import { Controller${ops.findAll ? ', Query' : ''}${ops.findOne || ops.update || ops.remove ? ', Param, ParseUUIDPipe, HttpStatus' : ''}${ops.create ? ', Post, Body' : ''}${ops.findAll || ops.findOne ? ', Get' : ''}${ops.update ? ', Put' : ''}${ops.remove ? ', Delete' : ''}, Type } from '@nestjs/common';
|
|
486
522
|
import { ApiTags, ApiBearerAuth, ApiExtraModels, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
|
|
487
523
|
import { BaseController } from '../../common/base/base.controller';
|
|
@@ -557,10 +593,15 @@ ${ops.create ? `
|
|
|
557
593
|
const moduleContent = getModuleContent(orm, pascalName, kebabName);
|
|
558
594
|
await fs.writeFile(path.join(moduleDir, `${kebabName}.module.ts`), moduleContent);
|
|
559
595
|
|
|
596
|
+
// 5. Auto-register in src/app.module.ts
|
|
597
|
+
const isAutoRegistered = await registerInAppModule(targetDir, pascalName, kebabName);
|
|
598
|
+
|
|
560
599
|
console.log(chalk.green(`\n✅ Module "${kebabName}" successfully generated in ${path.relative(process.cwd(), moduleDir)}`));
|
|
561
600
|
console.log(chalk.gray(` Detected ORM: ${orm}`));
|
|
562
|
-
|
|
563
|
-
|
|
601
|
+
|
|
602
|
+
if (!isAutoRegistered) {
|
|
603
|
+
console.log(chalk.yellow(`\n⚠️ Please manually register ${pascalName}Module in src/app.module.ts:`));
|
|
604
|
+
console.log(chalk.cyan(`
|
|
564
605
|
import { ${pascalName}Module } from './modules/${kebabName}/${kebabName}.module';
|
|
565
606
|
|
|
566
607
|
@Module({
|
|
@@ -571,6 +612,7 @@ import { ${pascalName}Module } from './modules/${kebabName}/${kebabName}.module'
|
|
|
571
612
|
})
|
|
572
613
|
export class AppModule {}
|
|
573
614
|
`));
|
|
615
|
+
}
|
|
574
616
|
|
|
575
617
|
return true;
|
|
576
618
|
} catch (error) {
|