nest-authme 1.2.3 → 1.3.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/dist/index.js CHANGED
@@ -343,8 +343,7 @@ var init_generator = __esm({
343
343
  } else if (config.orm === "prisma") {
344
344
  plan.push(
345
345
  { template: "prisma/prisma.service.ts.hbs", output: `${config.sourceRoot}/prisma/prisma.service.ts` },
346
- { template: "prisma/prisma.module.ts.hbs", output: `${config.sourceRoot}/prisma/prisma.module.ts` },
347
- { template: "prisma/schema.prisma.additions.hbs", output: "prisma-schema-additions.prisma" }
346
+ { template: "prisma/prisma.module.ts.hbs", output: `${config.sourceRoot}/prisma/prisma.module.ts` }
348
347
  );
349
348
  }
350
349
  if (config.features.emailService) {
@@ -777,13 +776,139 @@ var init_main_ts_updater = __esm({
777
776
  }
778
777
  });
779
778
 
779
+ // src/installer/prisma-schema-updater.ts
780
+ var fs6, import_execa, PrismaSchemaUpdater;
781
+ var init_prisma_schema_updater = __esm({
782
+ "src/installer/prisma-schema-updater.ts"() {
783
+ "use strict";
784
+ init_cjs_shims();
785
+ fs6 = __toESM(require("fs-extra"));
786
+ import_execa = require("execa");
787
+ init_template_engine();
788
+ init_config_builder();
789
+ PrismaSchemaUpdater = class {
790
+ constructor(schemaPath) {
791
+ this.schemaPath = schemaPath;
792
+ }
793
+ backupPath = null;
794
+ /**
795
+ * Update prisma/schema.prisma with auth models
796
+ */
797
+ async update(config) {
798
+ const exists = await fs6.pathExists(this.schemaPath);
799
+ if (!exists) {
800
+ return {
801
+ updated: false,
802
+ message: 'prisma/schema.prisma not found. Run "npx prisma init" first, then re-run nest-authme.',
803
+ skippedModels: []
804
+ };
805
+ }
806
+ const existingContent = await fs6.readFile(this.schemaPath, "utf-8");
807
+ const skippedModels = [];
808
+ const hasUser = /^\s*model\s+User\s*\{/m.test(existingContent);
809
+ const hasRefreshToken = /^\s*model\s+RefreshToken\s*\{/m.test(existingContent);
810
+ if (hasUser) {
811
+ skippedModels.push("User");
812
+ }
813
+ if (hasRefreshToken && config.features.refreshTokens) {
814
+ skippedModels.push("RefreshToken");
815
+ }
816
+ const needsUser = !hasUser;
817
+ const needsRefreshToken = config.features.refreshTokens && !hasRefreshToken;
818
+ if (!needsUser && !needsRefreshToken) {
819
+ return {
820
+ updated: false,
821
+ message: `Models already exist in schema.prisma: ${skippedModels.join(", ")}. Please check that your existing models have all required auth fields.`,
822
+ skippedModels
823
+ };
824
+ }
825
+ const templateEngine = new TemplateEngine();
826
+ const context = buildTemplateContext(config);
827
+ let modelsContent = await templateEngine.render("prisma/schema.prisma.models.hbs", context);
828
+ if (hasUser) {
829
+ modelsContent = modelsContent.replace(/model\s+User\s*\{[^}]*\}\n?/s, "");
830
+ }
831
+ if (hasRefreshToken) {
832
+ modelsContent = modelsContent.replace(/model\s+RefreshToken\s*\{[^}]*\}\n?/s, "");
833
+ }
834
+ modelsContent = modelsContent.replace(/\n{3,}/g, "\n\n").trim();
835
+ if (!modelsContent) {
836
+ return {
837
+ updated: false,
838
+ message: "No new models to add.",
839
+ skippedModels
840
+ };
841
+ }
842
+ await this.createBackup();
843
+ try {
844
+ const separator = "\n\n// === Auth models (added by nest-authme) ===\n\n";
845
+ const updatedContent = existingContent.trimEnd() + separator + modelsContent + "\n";
846
+ await fs6.writeFile(this.schemaPath, updatedContent, "utf-8");
847
+ await this.tryPrismaFormat();
848
+ const addedModels = [];
849
+ if (needsUser) addedModels.push("User");
850
+ if (needsRefreshToken) addedModels.push("RefreshToken");
851
+ let message = `Added ${addedModels.join(", ")} model(s) to prisma/schema.prisma`;
852
+ if (skippedModels.length > 0) {
853
+ message += `. Skipped existing: ${skippedModels.join(", ")} (check for missing auth fields)`;
854
+ }
855
+ return {
856
+ updated: true,
857
+ message,
858
+ skippedModels
859
+ };
860
+ } catch (error) {
861
+ await this.restoreBackup();
862
+ throw error;
863
+ }
864
+ }
865
+ /**
866
+ * Try to run prisma format for consistent indentation
867
+ */
868
+ async tryPrismaFormat() {
869
+ try {
870
+ await (0, import_execa.execa)("npx", ["prisma", "format"], {
871
+ cwd: this.schemaPath.replace(/[/\\]prisma[/\\]schema\.prisma$/, ""),
872
+ timeout: 15e3
873
+ });
874
+ } catch {
875
+ }
876
+ }
877
+ /**
878
+ * Create backup of schema.prisma
879
+ */
880
+ async createBackup() {
881
+ this.backupPath = `${this.schemaPath}.backup`;
882
+ await fs6.copy(this.schemaPath, this.backupPath);
883
+ }
884
+ /**
885
+ * Restore backup on failure
886
+ */
887
+ async restoreBackup() {
888
+ if (this.backupPath && await fs6.pathExists(this.backupPath)) {
889
+ await fs6.copy(this.backupPath, this.schemaPath, { overwrite: true });
890
+ await fs6.remove(this.backupPath);
891
+ }
892
+ }
893
+ /**
894
+ * Clean up backup after success
895
+ */
896
+ async cleanupBackup() {
897
+ if (this.backupPath && await fs6.pathExists(this.backupPath)) {
898
+ await fs6.remove(this.backupPath);
899
+ }
900
+ }
901
+ };
902
+ }
903
+ });
904
+
780
905
  // src/installer/package-updater.ts
781
- var fs6, PackageUpdater;
906
+ var fs7, PackageUpdater;
782
907
  var init_package_updater = __esm({
783
908
  "src/installer/package-updater.ts"() {
784
909
  "use strict";
785
910
  init_cjs_shims();
786
- fs6 = __toESM(require("fs-extra"));
911
+ fs7 = __toESM(require("fs-extra"));
787
912
  PackageUpdater = class {
788
913
  constructor(packageJsonPath) {
789
914
  this.packageJsonPath = packageJsonPath;
@@ -795,7 +920,7 @@ var init_package_updater = __esm({
795
920
  async update(config) {
796
921
  await this.createBackup();
797
922
  try {
798
- const packageJson = await fs6.readJSON(this.packageJsonPath);
923
+ const packageJson = await fs7.readJSON(this.packageJsonPath);
799
924
  const deps = this.getDependencies(config);
800
925
  packageJson.dependencies = {
801
926
  ...packageJson.dependencies,
@@ -809,7 +934,7 @@ var init_package_updater = __esm({
809
934
  packageJson.devDependencies = this.sortObject(
810
935
  packageJson.devDependencies
811
936
  );
812
- await fs6.writeJSON(this.packageJsonPath, packageJson, { spaces: 2 });
937
+ await fs7.writeJSON(this.packageJsonPath, packageJson, { spaces: 2 });
813
938
  } catch (error) {
814
939
  await this.restoreBackup();
815
940
  throw error;
@@ -891,23 +1016,23 @@ var init_package_updater = __esm({
891
1016
  */
892
1017
  async createBackup() {
893
1018
  this.backupPath = `${this.packageJsonPath}.backup`;
894
- await fs6.copy(this.packageJsonPath, this.backupPath);
1019
+ await fs7.copy(this.packageJsonPath, this.backupPath);
895
1020
  }
896
1021
  /**
897
1022
  * Restore backup
898
1023
  */
899
1024
  async restoreBackup() {
900
- if (this.backupPath && await fs6.pathExists(this.backupPath)) {
901
- await fs6.copy(this.backupPath, this.packageJsonPath, { overwrite: true });
902
- await fs6.remove(this.backupPath);
1025
+ if (this.backupPath && await fs7.pathExists(this.backupPath)) {
1026
+ await fs7.copy(this.backupPath, this.packageJsonPath, { overwrite: true });
1027
+ await fs7.remove(this.backupPath);
903
1028
  }
904
1029
  }
905
1030
  /**
906
1031
  * Clean up backup
907
1032
  */
908
1033
  async cleanupBackup() {
909
- if (this.backupPath && await fs6.pathExists(this.backupPath)) {
910
- await fs6.remove(this.backupPath);
1034
+ if (this.backupPath && await fs7.pathExists(this.backupPath)) {
1035
+ await fs7.remove(this.backupPath);
911
1036
  }
912
1037
  }
913
1038
  };
@@ -915,12 +1040,12 @@ var init_package_updater = __esm({
915
1040
  });
916
1041
 
917
1042
  // src/installer/dependency-installer.ts
918
- var import_execa, import_detect_package_manager, DependencyInstaller;
1043
+ var import_execa2, import_detect_package_manager, DependencyInstaller;
919
1044
  var init_dependency_installer = __esm({
920
1045
  "src/installer/dependency-installer.ts"() {
921
1046
  "use strict";
922
1047
  init_cjs_shims();
923
- import_execa = require("execa");
1048
+ import_execa2 = require("execa");
924
1049
  import_detect_package_manager = require("detect-package-manager");
925
1050
  DependencyInstaller = class {
926
1051
  /**
@@ -930,7 +1055,7 @@ var init_dependency_installer = __esm({
930
1055
  const packageManager = await this.detectPackageManager(cwd);
931
1056
  console.log(`\u{1F4E6} Installing dependencies with ${packageManager}...`);
932
1057
  try {
933
- await (0, import_execa.execa)(packageManager, ["install"], {
1058
+ await (0, import_execa2.execa)(packageManager, ["install"], {
934
1059
  cwd,
935
1060
  stdio: "inherit"
936
1061
  });
@@ -947,7 +1072,7 @@ var init_dependency_installer = __esm({
947
1072
  async installCapture(cwd) {
948
1073
  const packageManager = await this.detectPackageManager(cwd);
949
1074
  try {
950
- const result = await (0, import_execa.execa)(packageManager, ["install"], {
1075
+ const result = await (0, import_execa2.execa)(packageManager, ["install"], {
951
1076
  cwd,
952
1077
  stdio: "pipe"
953
1078
  });
@@ -978,7 +1103,8 @@ __export(installer_exports, {
978
1103
  AppModuleUpdater: () => AppModuleUpdater,
979
1104
  DependencyInstaller: () => DependencyInstaller,
980
1105
  MainTsUpdater: () => MainTsUpdater,
981
- PackageUpdater: () => PackageUpdater
1106
+ PackageUpdater: () => PackageUpdater,
1107
+ PrismaSchemaUpdater: () => PrismaSchemaUpdater
982
1108
  });
983
1109
  var init_installer = __esm({
984
1110
  "src/installer/index.ts"() {
@@ -986,6 +1112,7 @@ var init_installer = __esm({
986
1112
  init_cjs_shims();
987
1113
  init_ast_updater();
988
1114
  init_main_ts_updater();
1115
+ init_prisma_schema_updater();
989
1116
  init_package_updater();
990
1117
  init_dependency_installer();
991
1118
  }
@@ -998,6 +1125,7 @@ __export(index_exports, {
998
1125
  });
999
1126
  module.exports = __toCommonJS(index_exports);
1000
1127
  init_cjs_shims();
1128
+ var path5 = __toESM(require("path"));
1001
1129
 
1002
1130
  // src/analyzer/index.ts
1003
1131
  init_cjs_shims();
@@ -1476,10 +1604,16 @@ function showSuccess(stats) {
1476
1604
  console.log(import_chalk.default.gray(" # .env.example is also provided as a git-safe reference"));
1477
1605
  console.log();
1478
1606
  if (stats.orm === "prisma") {
1479
- console.log(import_chalk.default.cyan(" 2. Add Prisma schema models (see prisma-schema-additions.prisma)"));
1480
- console.log(import_chalk.default.gray(" # Copy the models into your prisma/schema.prisma"));
1481
- console.log(import_chalk.default.gray(" npx prisma migrate dev --name add-auth-models"));
1482
- console.log(import_chalk.default.gray(" npx prisma generate"));
1607
+ if (stats.prismaSchemaUpdated) {
1608
+ console.log(import_chalk.default.cyan(" 2. Run Prisma migration"));
1609
+ console.log(import_chalk.default.gray(" npx prisma migrate dev --name add-auth-models"));
1610
+ console.log(import_chalk.default.gray(" npx prisma generate"));
1611
+ } else {
1612
+ console.log(import_chalk.default.cyan(" 2. Add Prisma schema models manually"));
1613
+ console.log(import_chalk.default.gray(" # Check src/auth/README.md for the model definitions"));
1614
+ console.log(import_chalk.default.gray(" npx prisma migrate dev --name add-auth-models"));
1615
+ console.log(import_chalk.default.gray(" npx prisma generate"));
1616
+ }
1483
1617
  } else {
1484
1618
  console.log(import_chalk.default.cyan(" 2. Create database migration (if using TypeORM)"));
1485
1619
  console.log(import_chalk.default.gray(" npm run migration:generate -- src/migrations/CreateUserTable"));
@@ -1602,6 +1736,27 @@ async function run(cwd = process.cwd(), options = {}) {
1602
1736
  );
1603
1737
  process.exit(1);
1604
1738
  }
1739
+ let prismaSchemaUpdated = false;
1740
+ if (config.orm === "prisma") {
1741
+ const prismaSpinner = createSpinner("Updating prisma/schema.prisma...").start();
1742
+ try {
1743
+ const { PrismaSchemaUpdater: PrismaSchemaUpdater2 } = await Promise.resolve().then(() => (init_installer(), installer_exports));
1744
+ const schemaPath = path5.join(projectInfo.root, "prisma", "schema.prisma");
1745
+ const prismaUpdater = new PrismaSchemaUpdater2(schemaPath);
1746
+ const prismaResult = await prismaUpdater.update(config);
1747
+ if (prismaResult.updated) {
1748
+ await prismaUpdater.cleanupBackup();
1749
+ prismaSpinner.succeed(prismaResult.message);
1750
+ prismaSchemaUpdated = true;
1751
+ } else {
1752
+ prismaSpinner.warn(prismaResult.message);
1753
+ }
1754
+ } catch (error) {
1755
+ prismaSpinner.warn(
1756
+ `Could not update prisma/schema.prisma: ${error instanceof Error ? error.message : "Unknown error"}`
1757
+ );
1758
+ }
1759
+ }
1605
1760
  const mainSpinner = createSpinner("Updating main.ts with global guards...").start();
1606
1761
  try {
1607
1762
  const { MainTsUpdater: MainTsUpdater2 } = await Promise.resolve().then(() => (init_installer(), installer_exports));
@@ -1651,7 +1806,8 @@ async function run(cwd = process.cwd(), options = {}) {
1651
1806
  orm: config.orm,
1652
1807
  swagger: config.features.swagger,
1653
1808
  emailVerification: config.features.emailVerification,
1654
- resetPassword: config.features.resetPassword
1809
+ resetPassword: config.features.resetPassword,
1810
+ prismaSchemaUpdated
1655
1811
  });
1656
1812
  console.log("\u{1F41B} Issues? https://github.com/Islamawad132/add-nest-auth/issues");
1657
1813
  console.log("\u2B50 Like it? https://www.npmjs.com/package/nest-authme");