add-nest-auth 1.0.5 → 1.0.6

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/cli.js CHANGED
@@ -489,13 +489,157 @@ var init_ast_updater = __esm({
489
489
  }
490
490
  });
491
491
 
492
+ // src/installer/main-ts-updater.ts
493
+ var import_ts_morph2, fs5, MainTsUpdater;
494
+ var init_main_ts_updater = __esm({
495
+ "src/installer/main-ts-updater.ts"() {
496
+ "use strict";
497
+ init_cjs_shims();
498
+ import_ts_morph2 = require("ts-morph");
499
+ fs5 = __toESM(require("fs-extra"));
500
+ MainTsUpdater = class {
501
+ constructor(mainTsPath) {
502
+ this.mainTsPath = mainTsPath;
503
+ this.project = new import_ts_morph2.Project({
504
+ skipAddingFilesFromTsConfig: true
505
+ });
506
+ }
507
+ project;
508
+ sourceFile;
509
+ backupPath = null;
510
+ /**
511
+ * Update main.ts with global guards and validation pipe
512
+ */
513
+ async update() {
514
+ if (!await fs5.pathExists(this.mainTsPath)) {
515
+ throw new Error(`main.ts not found at ${this.mainTsPath}`);
516
+ }
517
+ await this.createBackup();
518
+ try {
519
+ this.sourceFile = this.project.addSourceFileAtPath(this.mainTsPath);
520
+ this.addImports();
521
+ this.addGlobalGuardsAndPipes();
522
+ await this.sourceFile.save();
523
+ } catch (error) {
524
+ await this.restoreBackup();
525
+ throw error;
526
+ }
527
+ }
528
+ /**
529
+ * Add necessary imports
530
+ */
531
+ addImports() {
532
+ if (!this.sourceFile) {
533
+ throw new Error("Source file not loaded");
534
+ }
535
+ this.addImport("@nestjs/core", ["Reflector"]);
536
+ this.addImport("@nestjs/common", ["ValidationPipe"]);
537
+ this.addImport("./auth/guards/jwt-auth.guard", ["JwtAuthGuard"]);
538
+ }
539
+ /**
540
+ * Add an import statement if it doesn't exist
541
+ */
542
+ addImport(moduleSpecifier, namedImports) {
543
+ if (!this.sourceFile) return;
544
+ const existingImport = this.sourceFile.getImportDeclarations().find((imp) => imp.getModuleSpecifierValue() === moduleSpecifier);
545
+ if (existingImport) {
546
+ const existingNames = existingImport.getNamedImports().map((ni) => ni.getName());
547
+ const missingImports = namedImports.filter(
548
+ (name) => !existingNames.includes(name)
549
+ );
550
+ if (missingImports.length > 0) {
551
+ existingImport.addNamedImports(missingImports);
552
+ }
553
+ } else {
554
+ this.sourceFile.addImportDeclaration({
555
+ moduleSpecifier,
556
+ namedImports
557
+ });
558
+ }
559
+ }
560
+ /**
561
+ * Add global guards and validation pipe to bootstrap function
562
+ */
563
+ addGlobalGuardsAndPipes() {
564
+ if (!this.sourceFile) return;
565
+ const bootstrapFunc = this.sourceFile.getFunction("bootstrap");
566
+ if (!bootstrapFunc) {
567
+ throw new Error("bootstrap function not found in main.ts");
568
+ }
569
+ const body = bootstrapFunc.getBody();
570
+ if (!body || !import_ts_morph2.Node.isBlock(body)) {
571
+ throw new Error("bootstrap function has no body");
572
+ }
573
+ const bodyText = body.getText();
574
+ if (bodyText.includes("useGlobalGuards") || bodyText.includes("JwtAuthGuard")) {
575
+ return;
576
+ }
577
+ const statements = body.getStatements();
578
+ let listenIndex = -1;
579
+ for (let i = 0; i < statements.length; i++) {
580
+ const text = statements[i].getText();
581
+ if (text.includes(".listen(") || text.includes(".listen (")) {
582
+ listenIndex = i;
583
+ break;
584
+ }
585
+ }
586
+ if (listenIndex === -1) {
587
+ listenIndex = statements.length;
588
+ }
589
+ const codeToInsert = [
590
+ "",
591
+ " // Enable global validation pipe",
592
+ " app.useGlobalPipes(",
593
+ " new ValidationPipe({",
594
+ " whitelist: true,",
595
+ " forbidNonWhitelisted: true,",
596
+ " transform: true,",
597
+ " }),",
598
+ " );",
599
+ "",
600
+ " // Enable global JWT guard (all routes protected by default)",
601
+ " // Use @Public() decorator on routes that should be accessible without auth",
602
+ " const reflector = app.get(Reflector);",
603
+ " app.useGlobalGuards(new JwtAuthGuard(reflector));",
604
+ ""
605
+ ].join("\n");
606
+ body.insertStatements(listenIndex, codeToInsert);
607
+ }
608
+ /**
609
+ * Create backup of main.ts
610
+ */
611
+ async createBackup() {
612
+ this.backupPath = `${this.mainTsPath}.backup`;
613
+ await fs5.copy(this.mainTsPath, this.backupPath);
614
+ }
615
+ /**
616
+ * Restore backup
617
+ */
618
+ async restoreBackup() {
619
+ if (this.backupPath && await fs5.pathExists(this.backupPath)) {
620
+ await fs5.copy(this.backupPath, this.mainTsPath, { overwrite: true });
621
+ await fs5.remove(this.backupPath);
622
+ }
623
+ }
624
+ /**
625
+ * Clean up backup
626
+ */
627
+ async cleanupBackup() {
628
+ if (this.backupPath && await fs5.pathExists(this.backupPath)) {
629
+ await fs5.remove(this.backupPath);
630
+ }
631
+ }
632
+ };
633
+ }
634
+ });
635
+
492
636
  // src/installer/package-updater.ts
493
- var fs5, PackageUpdater;
637
+ var fs6, PackageUpdater;
494
638
  var init_package_updater = __esm({
495
639
  "src/installer/package-updater.ts"() {
496
640
  "use strict";
497
641
  init_cjs_shims();
498
- fs5 = __toESM(require("fs-extra"));
642
+ fs6 = __toESM(require("fs-extra"));
499
643
  PackageUpdater = class {
500
644
  constructor(packageJsonPath) {
501
645
  this.packageJsonPath = packageJsonPath;
@@ -507,7 +651,7 @@ var init_package_updater = __esm({
507
651
  async update(config) {
508
652
  await this.createBackup();
509
653
  try {
510
- const packageJson = await fs5.readJSON(this.packageJsonPath);
654
+ const packageJson = await fs6.readJSON(this.packageJsonPath);
511
655
  const deps = this.getDependencies(config);
512
656
  packageJson.dependencies = {
513
657
  ...packageJson.dependencies,
@@ -521,7 +665,7 @@ var init_package_updater = __esm({
521
665
  packageJson.devDependencies = this.sortObject(
522
666
  packageJson.devDependencies
523
667
  );
524
- await fs5.writeJSON(this.packageJsonPath, packageJson, { spaces: 2 });
668
+ await fs6.writeJSON(this.packageJsonPath, packageJson, { spaces: 2 });
525
669
  } catch (error) {
526
670
  await this.restoreBackup();
527
671
  throw error;
@@ -581,23 +725,23 @@ var init_package_updater = __esm({
581
725
  */
582
726
  async createBackup() {
583
727
  this.backupPath = `${this.packageJsonPath}.backup`;
584
- await fs5.copy(this.packageJsonPath, this.backupPath);
728
+ await fs6.copy(this.packageJsonPath, this.backupPath);
585
729
  }
586
730
  /**
587
731
  * Restore backup
588
732
  */
589
733
  async restoreBackup() {
590
- if (this.backupPath && await fs5.pathExists(this.backupPath)) {
591
- await fs5.copy(this.backupPath, this.packageJsonPath, { overwrite: true });
592
- await fs5.remove(this.backupPath);
734
+ if (this.backupPath && await fs6.pathExists(this.backupPath)) {
735
+ await fs6.copy(this.backupPath, this.packageJsonPath, { overwrite: true });
736
+ await fs6.remove(this.backupPath);
593
737
  }
594
738
  }
595
739
  /**
596
740
  * Clean up backup
597
741
  */
598
742
  async cleanupBackup() {
599
- if (this.backupPath && await fs5.pathExists(this.backupPath)) {
600
- await fs5.remove(this.backupPath);
743
+ if (this.backupPath && await fs6.pathExists(this.backupPath)) {
744
+ await fs6.remove(this.backupPath);
601
745
  }
602
746
  }
603
747
  };
@@ -650,6 +794,7 @@ var installer_exports = {};
650
794
  __export(installer_exports, {
651
795
  AppModuleUpdater: () => AppModuleUpdater,
652
796
  DependencyInstaller: () => DependencyInstaller,
797
+ MainTsUpdater: () => MainTsUpdater,
653
798
  PackageUpdater: () => PackageUpdater
654
799
  });
655
800
  var init_installer = __esm({
@@ -657,6 +802,7 @@ var init_installer = __esm({
657
802
  "use strict";
658
803
  init_cjs_shims();
659
804
  init_ast_updater();
805
+ init_main_ts_updater();
660
806
  init_package_updater();
661
807
  init_dependency_installer();
662
808
  }
@@ -730,6 +876,7 @@ var ProjectDetector = class {
730
876
  errors.push(`app.module.ts not found at ${sourceRoot}/app.module.ts`);
731
877
  return this.createInvalidProject(root, errors);
732
878
  }
879
+ const mainTsPath = path.join(root, sourceRoot, "main.ts");
733
880
  const orm = await detectORM(packageJson);
734
881
  const authModulePath = path.join(root, sourceRoot, "auth");
735
882
  if (await fs.pathExists(authModulePath)) {
@@ -739,6 +886,7 @@ var ProjectDetector = class {
739
886
  root,
740
887
  sourceRoot,
741
888
  appModulePath,
889
+ mainTsPath,
742
890
  packageJsonPath,
743
891
  nestCliConfigPath,
744
892
  orm,
@@ -781,6 +929,7 @@ var ProjectDetector = class {
781
929
  root,
782
930
  sourceRoot: "src",
783
931
  appModulePath: path.join(root, "src", "app.module.ts"),
932
+ mainTsPath: path.join(root, "src", "main.ts"),
784
933
  packageJsonPath: path.join(root, "package.json"),
785
934
  nestCliConfigPath: path.join(root, "nest-cli.json"),
786
935
  orm: "none",
@@ -1091,6 +1240,16 @@ async function run(cwd = process.cwd()) {
1091
1240
  );
1092
1241
  process.exit(1);
1093
1242
  }
1243
+ const mainSpinner = createSpinner("Updating main.ts with global guards...").start();
1244
+ try {
1245
+ const { MainTsUpdater: MainTsUpdater2 } = await Promise.resolve().then(() => (init_installer(), installer_exports));
1246
+ const mainUpdater = new MainTsUpdater2(projectInfo.mainTsPath);
1247
+ await mainUpdater.update();
1248
+ await mainUpdater.cleanupBackup();
1249
+ mainSpinner.succeed("Updated main.ts with global JWT guard");
1250
+ } catch (error) {
1251
+ mainSpinner.warn("Could not auto-update main.ts (see main.ts.example for manual setup)");
1252
+ }
1094
1253
  const pkgSpinner = createSpinner("Updating package.json...").start();
1095
1254
  try {
1096
1255
  const { PackageUpdater: PackageUpdater2 } = await Promise.resolve().then(() => (init_installer(), installer_exports));
@@ -1128,8 +1287,8 @@ async function run(cwd = process.cwd()) {
1128
1287
  refreshExpiration: config.features.refreshTokens ? config.jwt.refreshExpiration : void 0
1129
1288
  }
1130
1289
  });
1131
- console.log("\u{1F41B} Issues? https://github.com/yourusername/add-nest-auth/issues");
1132
- console.log("\u2B50 Like it? https://github.com/yourusername/add-nest-auth");
1290
+ console.log("\u{1F41B} Issues? https://github.com/Islamawad132/add-nest-auth/issues");
1291
+ console.log("\u2B50 Like it? https://github.com/Islamawad132/add-nest-auth");
1133
1292
  console.log();
1134
1293
  }
1135
1294