@metaadri/secureauth 1.2.1 → 1.2.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/scaffolder.js +52 -0
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.2.1",
6
+ "version": "1.2.2",
7
7
  "description": "SecureAuth CLI - Add enterprise authentication to any Express app in one command",
8
8
  "bin": {
9
9
  "secureauth": "bin/cli.js"
package/src/scaffolder.js CHANGED
@@ -117,9 +117,61 @@ function scaffold(targetDir, answers) {
117
117
  fileCount++;
118
118
  }
119
119
 
120
+ postProcess(targetDir);
120
121
  return fileCount;
121
122
  }
122
123
 
124
+ function postProcess(targetDir) {
125
+ const files = {
126
+ 'controllers/authController.js': [
127
+ {
128
+ search: /(const failedAttempts = await userModel\.incrementFailedAttempts\(user\.id\);)(?!\s+const remaining)/,
129
+ replace: '$1\n const remaining = MAX_FAILED_ATTEMPTS - failedAttempts;'
130
+ }
131
+ ],
132
+ 'server.js': [
133
+ {
134
+ search: /require\('dotenv'\)\.config\(\);/,
135
+ replace: "const path = require('path');\nrequire('dotenv').config({ path: path.join(__dirname, '.env') });"
136
+ },
137
+ {
138
+ search: /initDatabase\(\)\s*\.then\(async\s*\(\)\s*=>\s*\{[\s\S]*?await seedAdminUser\(\);[\s\S]*?await seedDemoUser\(\);[\s\S]*?dbReady\s*=\s*true;[\s\S]*?console\.log\('Database initialized and seeded'\);\s*\}\)[\s\S]*?\.catch\(err\s*=>\s*\{[\s\S]*?console\.error\('Database initialization failed:',\s*err\.message\);\s*\}\);/,
139
+ replace: "try {\n initDatabase();\n seedAdminUser();\n seedDemoUser();\n dbReady = true;\n console.log('Database initialized and seeded');\n} catch (err) {\n console.error('Database initialization failed:', err.message);\n}"
140
+ }
141
+ ],
142
+ 'database/db.sqlite.js': [
143
+ {
144
+ search: /query:\s*\(text,\s*params\)\s*=>\s*\{[^}]*const stmt\s*=\s*db\.prepare\(text\);[^}]*const rows\s*=\s*params\s*\?\s*stmt\.all\(\.\.\.params\)\s*:\s*stmt\.all\(\);[^}]*return\s*\{\s*rows\s*\};[^}]*\}/,
145
+ replace: "query: (text, params) => {\n const stmt = db.prepare(text);\n const sql = text.trim().toUpperCase();\n if (sql.startsWith('SELECT')) {\n const rows = params ? stmt.all(...params) : stmt.all();\n return { rows };\n }\n const info = params ? stmt.run(...params) : stmt.run();\n return { rows: [], changes: info.changes };\n }"
146
+ }
147
+ ],
148
+ 'routes/authRoutes.js': [
149
+ {
150
+ search: /router\.post\('\/demo\/login', authController\.demoLogin\);\s*/,
151
+ replace: ''
152
+ }
153
+ ]
154
+ };
155
+
156
+ for (const [relative, rules] of Object.entries(files)) {
157
+ const filePath = path.join(targetDir, relative);
158
+ if (!fs.existsSync(filePath)) continue;
159
+ try {
160
+ let content = fs.readFileSync(filePath, 'utf-8');
161
+ const before = content;
162
+ for (const rule of rules) {
163
+ content = content.replace(rule.search, rule.replace);
164
+ }
165
+ if (content !== before) {
166
+ fs.writeFileSync(filePath, content, 'utf-8');
167
+ logger.info(`Patched ${relative}`);
168
+ }
169
+ } catch {
170
+ // skip unreadable files
171
+ }
172
+ }
173
+ }
174
+
123
175
  function collectFiles(dir) {
124
176
  const results = [];
125
177
  for (const entry of fs.readdirSync(dir)) {