add-nest-auth 1.0.4 → 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 +173 -13
- package/dist/cli.js.map +1 -1
- package/dist/generator/templates/dto/register.dto.ts.hbs +2 -2
- package/dist/generator/templates/jwt/jwt.strategy.ts.hbs +9 -1
- package/dist/generator/templates/shared/README.auth.md.hbs +9 -13
- package/dist/generator/templates/shared/main.ts.snippet.hbs +31 -0
- package/dist/index.js +173 -13
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -310,7 +310,8 @@ var init_generator = __esm({
|
|
|
310
310
|
}
|
|
311
311
|
plan.push(
|
|
312
312
|
{ template: "shared/env.template.hbs", output: ".env.example" },
|
|
313
|
-
{ template: "shared/README.auth.md.hbs", output: `${config.sourceRoot}/auth/README.md` }
|
|
313
|
+
{ template: "shared/README.auth.md.hbs", output: `${config.sourceRoot}/auth/README.md` },
|
|
314
|
+
{ template: "shared/main.ts.snippet.hbs", output: "main.ts.example" }
|
|
314
315
|
);
|
|
315
316
|
return plan;
|
|
316
317
|
}
|
|
@@ -488,13 +489,157 @@ var init_ast_updater = __esm({
|
|
|
488
489
|
}
|
|
489
490
|
});
|
|
490
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
|
+
|
|
491
636
|
// src/installer/package-updater.ts
|
|
492
|
-
var
|
|
637
|
+
var fs6, PackageUpdater;
|
|
493
638
|
var init_package_updater = __esm({
|
|
494
639
|
"src/installer/package-updater.ts"() {
|
|
495
640
|
"use strict";
|
|
496
641
|
init_cjs_shims();
|
|
497
|
-
|
|
642
|
+
fs6 = __toESM(require("fs-extra"));
|
|
498
643
|
PackageUpdater = class {
|
|
499
644
|
constructor(packageJsonPath) {
|
|
500
645
|
this.packageJsonPath = packageJsonPath;
|
|
@@ -506,7 +651,7 @@ var init_package_updater = __esm({
|
|
|
506
651
|
async update(config) {
|
|
507
652
|
await this.createBackup();
|
|
508
653
|
try {
|
|
509
|
-
const packageJson = await
|
|
654
|
+
const packageJson = await fs6.readJSON(this.packageJsonPath);
|
|
510
655
|
const deps = this.getDependencies(config);
|
|
511
656
|
packageJson.dependencies = {
|
|
512
657
|
...packageJson.dependencies,
|
|
@@ -520,7 +665,7 @@ var init_package_updater = __esm({
|
|
|
520
665
|
packageJson.devDependencies = this.sortObject(
|
|
521
666
|
packageJson.devDependencies
|
|
522
667
|
);
|
|
523
|
-
await
|
|
668
|
+
await fs6.writeJSON(this.packageJsonPath, packageJson, { spaces: 2 });
|
|
524
669
|
} catch (error) {
|
|
525
670
|
await this.restoreBackup();
|
|
526
671
|
throw error;
|
|
@@ -580,23 +725,23 @@ var init_package_updater = __esm({
|
|
|
580
725
|
*/
|
|
581
726
|
async createBackup() {
|
|
582
727
|
this.backupPath = `${this.packageJsonPath}.backup`;
|
|
583
|
-
await
|
|
728
|
+
await fs6.copy(this.packageJsonPath, this.backupPath);
|
|
584
729
|
}
|
|
585
730
|
/**
|
|
586
731
|
* Restore backup
|
|
587
732
|
*/
|
|
588
733
|
async restoreBackup() {
|
|
589
|
-
if (this.backupPath && await
|
|
590
|
-
await
|
|
591
|
-
await
|
|
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);
|
|
592
737
|
}
|
|
593
738
|
}
|
|
594
739
|
/**
|
|
595
740
|
* Clean up backup
|
|
596
741
|
*/
|
|
597
742
|
async cleanupBackup() {
|
|
598
|
-
if (this.backupPath && await
|
|
599
|
-
await
|
|
743
|
+
if (this.backupPath && await fs6.pathExists(this.backupPath)) {
|
|
744
|
+
await fs6.remove(this.backupPath);
|
|
600
745
|
}
|
|
601
746
|
}
|
|
602
747
|
};
|
|
@@ -649,6 +794,7 @@ var installer_exports = {};
|
|
|
649
794
|
__export(installer_exports, {
|
|
650
795
|
AppModuleUpdater: () => AppModuleUpdater,
|
|
651
796
|
DependencyInstaller: () => DependencyInstaller,
|
|
797
|
+
MainTsUpdater: () => MainTsUpdater,
|
|
652
798
|
PackageUpdater: () => PackageUpdater
|
|
653
799
|
});
|
|
654
800
|
var init_installer = __esm({
|
|
@@ -656,6 +802,7 @@ var init_installer = __esm({
|
|
|
656
802
|
"use strict";
|
|
657
803
|
init_cjs_shims();
|
|
658
804
|
init_ast_updater();
|
|
805
|
+
init_main_ts_updater();
|
|
659
806
|
init_package_updater();
|
|
660
807
|
init_dependency_installer();
|
|
661
808
|
}
|
|
@@ -729,6 +876,7 @@ var ProjectDetector = class {
|
|
|
729
876
|
errors.push(`app.module.ts not found at ${sourceRoot}/app.module.ts`);
|
|
730
877
|
return this.createInvalidProject(root, errors);
|
|
731
878
|
}
|
|
879
|
+
const mainTsPath = path.join(root, sourceRoot, "main.ts");
|
|
732
880
|
const orm = await detectORM(packageJson);
|
|
733
881
|
const authModulePath = path.join(root, sourceRoot, "auth");
|
|
734
882
|
if (await fs.pathExists(authModulePath)) {
|
|
@@ -738,6 +886,7 @@ var ProjectDetector = class {
|
|
|
738
886
|
root,
|
|
739
887
|
sourceRoot,
|
|
740
888
|
appModulePath,
|
|
889
|
+
mainTsPath,
|
|
741
890
|
packageJsonPath,
|
|
742
891
|
nestCliConfigPath,
|
|
743
892
|
orm,
|
|
@@ -780,6 +929,7 @@ var ProjectDetector = class {
|
|
|
780
929
|
root,
|
|
781
930
|
sourceRoot: "src",
|
|
782
931
|
appModulePath: path.join(root, "src", "app.module.ts"),
|
|
932
|
+
mainTsPath: path.join(root, "src", "main.ts"),
|
|
783
933
|
packageJsonPath: path.join(root, "package.json"),
|
|
784
934
|
nestCliConfigPath: path.join(root, "nest-cli.json"),
|
|
785
935
|
orm: "none",
|
|
@@ -1090,6 +1240,16 @@ async function run(cwd = process.cwd()) {
|
|
|
1090
1240
|
);
|
|
1091
1241
|
process.exit(1);
|
|
1092
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
|
+
}
|
|
1093
1253
|
const pkgSpinner = createSpinner("Updating package.json...").start();
|
|
1094
1254
|
try {
|
|
1095
1255
|
const { PackageUpdater: PackageUpdater2 } = await Promise.resolve().then(() => (init_installer(), installer_exports));
|
|
@@ -1127,8 +1287,8 @@ async function run(cwd = process.cwd()) {
|
|
|
1127
1287
|
refreshExpiration: config.features.refreshTokens ? config.jwt.refreshExpiration : void 0
|
|
1128
1288
|
}
|
|
1129
1289
|
});
|
|
1130
|
-
console.log("\u{1F41B} Issues? https://github.com/
|
|
1131
|
-
console.log("\u2B50 Like it? https://github.com/
|
|
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");
|
|
1132
1292
|
console.log();
|
|
1133
1293
|
}
|
|
1134
1294
|
|