express-file-cluster 0.9.0 → 0.9.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.
- package/dist/auth/index.cjs +8 -2
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +13 -2
- package/dist/auth/index.d.ts +13 -2
- package/dist/auth/index.js +9 -3
- package/dist/{chunk-2J3HHQKP.cjs → chunk-ANUSWEAP.cjs} +55 -2
- package/dist/chunk-ANUSWEAP.cjs.map +1 -0
- package/dist/{chunk-RSZB7VAO.js → chunk-ATL4GYFU.js} +54 -1
- package/dist/chunk-ATL4GYFU.js.map +1 -0
- package/dist/{chunk-2PBMLMO4.js → chunk-QKQZF573.js} +26 -4
- package/dist/chunk-QKQZF573.js.map +1 -0
- package/dist/{chunk-6QNLN6AI.js → chunk-RRQ2RK5M.js} +353 -3
- package/dist/chunk-RRQ2RK5M.js.map +1 -0
- package/dist/{chunk-RGTENTDN.cjs → chunk-RTF7PHS2.cjs} +432 -82
- package/dist/chunk-RTF7PHS2.cjs.map +1 -0
- package/dist/{chunk-66R7S7J3.cjs → chunk-X2US4RAM.cjs} +26 -4
- package/dist/chunk-X2US4RAM.cjs.map +1 -0
- package/dist/cli/db-runner.cjs +17 -17
- package/dist/cli/db-runner.js +2 -2
- package/dist/cli/index.cjs +315 -26
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +295 -6
- package/dist/cli/index.js.map +1 -1
- package/dist/docs/api-reference/auth.md +35 -0
- package/dist/docs/api-reference/collection.md +155 -0
- package/dist/docs/cli/add.md +36 -7
- package/dist/docs/guides/authentication.md +80 -4
- package/dist/docs/guides/coming-from-mongoose.md +60 -0
- package/dist/docs/guides/which-api.md +21 -12
- package/dist/docs/index.md +4 -2
- package/dist/docs/skills/SKILL.md +3 -3
- package/dist/docs/skills/api-surface.md +16 -0
- package/dist/docs/skills/what-not-to-invent.md +3 -2
- package/dist/index.cjs +18 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +193 -3
- package/dist/index.d.ts +193 -3
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/registry/index.json +1 -1
- package/dist/registry/js-mongo/admin-users.json +1 -1
- package/dist/registry/js-mongo/auth.json +1 -1
- package/dist/registry/js-mongo/auth@apple.json +1 -1
- package/dist/registry/js-mongo/auth@google.json +1 -1
- package/dist/registry/js-mongo/email-client.json +1 -1
- package/dist/registry/js-mongo/forgot-password.json +1 -1
- package/dist/registry/js-pg-model/admin-users.json +1 -1
- package/dist/registry/js-pg-model/auth.json +1 -1
- package/dist/registry/js-pg-model/auth@apple.json +1 -1
- package/dist/registry/js-pg-model/auth@google.json +1 -1
- package/dist/registry/js-pg-model/email-client.json +1 -1
- package/dist/registry/js-pg-model/forgot-password.json +1 -1
- package/dist/registry/js-pg-table/admin-users.json +1 -1
- package/dist/registry/js-pg-table/auth.json +1 -1
- package/dist/registry/js-pg-table/auth@apple.json +1 -1
- package/dist/registry/js-pg-table/auth@google.json +1 -1
- package/dist/registry/js-pg-table/email-client.json +1 -1
- package/dist/registry/js-pg-table/forgot-password.json +1 -1
- package/dist/registry/ts-mongo/admin-users.json +1 -1
- package/dist/registry/ts-mongo/auth.json +1 -1
- package/dist/registry/ts-mongo/auth@apple.json +1 -1
- package/dist/registry/ts-mongo/auth@google.json +1 -1
- package/dist/registry/ts-mongo/email-client.json +1 -1
- package/dist/registry/ts-mongo/forgot-password.json +1 -1
- package/dist/registry/ts-pg-model/admin-users.json +1 -1
- package/dist/registry/ts-pg-model/auth.json +1 -1
- package/dist/registry/ts-pg-model/auth@apple.json +1 -1
- package/dist/registry/ts-pg-model/auth@google.json +1 -1
- package/dist/registry/ts-pg-model/email-client.json +1 -1
- package/dist/registry/ts-pg-model/forgot-password.json +1 -1
- package/dist/registry/ts-pg-table/admin-users.json +1 -1
- package/dist/registry/ts-pg-table/auth.json +1 -1
- package/dist/registry/ts-pg-table/auth@apple.json +1 -1
- package/dist/registry/ts-pg-table/auth@google.json +1 -1
- package/dist/registry/ts-pg-table/email-client.json +1 -1
- package/dist/registry/ts-pg-table/forgot-password.json +1 -1
- package/dist/tasks/index.d.cts +1 -1
- package/dist/tasks/index.d.ts +1 -1
- package/dist/{types-gyvA7BtO.d.ts → types-BY-ObnZH.d.cts} +36 -7
- package/dist/{types-gyvA7BtO.d.cts → types-BY-ObnZH.d.ts} +36 -7
- package/package.json +3 -3
- package/dist/chunk-2J3HHQKP.cjs.map +0 -1
- package/dist/chunk-2PBMLMO4.js.map +0 -1
- package/dist/chunk-66R7S7J3.cjs.map +0 -1
- package/dist/chunk-6QNLN6AI.js.map +0 -1
- package/dist/chunk-RGTENTDN.cjs.map +0 -1
- package/dist/chunk-RSZB7VAO.js.map +0 -1
package/dist/cli/index.cjs
CHANGED
|
@@ -10,7 +10,7 @@ var _chunkT4OHFU5Tcjs = require('../chunk-T4OHFU5T.cjs');
|
|
|
10
10
|
var _chunkQL6EWT4Bcjs = require('../chunk-QL6EWT4B.cjs');
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
var
|
|
13
|
+
var _chunkX2US4RAMcjs = require('../chunk-X2US4RAM.cjs');
|
|
14
14
|
require('../chunk-VQSWSYK6.cjs');
|
|
15
15
|
require('../chunk-7YEX4MQV.cjs');
|
|
16
16
|
require('../chunk-TI24L4XE.cjs');
|
|
@@ -879,8 +879,11 @@ var Registry = class {
|
|
|
879
879
|
}
|
|
880
880
|
|
|
881
881
|
|
|
882
|
+
has(name) {
|
|
883
|
+
return this.index.items.some((i) => i.name === name || _optionalChain([i, 'access', _8 => _8.aliases, 'optionalAccess', _9 => _9.includes, 'call', _10 => _10(name)]));
|
|
884
|
+
}
|
|
882
885
|
meta(name) {
|
|
883
|
-
const item = this.index.items.find((i) => i.name === name || _optionalChain([i, 'access',
|
|
886
|
+
const item = this.index.items.find((i) => i.name === name || _optionalChain([i, 'access', _11 => _11.aliases, 'optionalAccess', _12 => _12.includes, 'call', _13 => _13(name)]));
|
|
884
887
|
if (!item) {
|
|
885
888
|
const names = this.index.items.map((i) => i.name).join(", ");
|
|
886
889
|
throw new Error(`Unknown item "${name}". Available: ${names}`);
|
|
@@ -1017,10 +1020,10 @@ function planAdd(cwd, registry, names, opts = {}, cfg = _nullishCoalesce(readEfc
|
|
|
1017
1020
|
const item = registry.item(variant, name);
|
|
1018
1021
|
for (const file of item.files) addFile(file, name);
|
|
1019
1022
|
for (const [dep, range] of Object.entries(item.dependencies)) {
|
|
1020
|
-
if (!_optionalChain([pkg, 'access',
|
|
1023
|
+
if (!_optionalChain([pkg, 'access', _14 => _14.dependencies, 'optionalAccess', _15 => _15[dep]])) plan.dependencies[dep] = range;
|
|
1021
1024
|
}
|
|
1022
1025
|
for (const [dep, range] of Object.entries(item.devDependencies)) {
|
|
1023
|
-
if (!_optionalChain([pkg, 'access',
|
|
1026
|
+
if (!_optionalChain([pkg, 'access', _16 => _16.devDependencies, 'optionalAccess', _17 => _17[dep]]) && !_optionalChain([pkg, 'access', _18 => _18.dependencies, 'optionalAccess', _19 => _19[dep]]))
|
|
1024
1027
|
plan.devDependencies[dep] = range;
|
|
1025
1028
|
}
|
|
1026
1029
|
for (const v of item.env) {
|
|
@@ -1032,7 +1035,7 @@ function planAdd(cwd, registry, names, opts = {}, cfg = _nullishCoalesce(readEfc
|
|
|
1032
1035
|
for (const name of items) {
|
|
1033
1036
|
const prompts = _nullishCoalesce(registry.meta(name).prompts, () => ( []));
|
|
1034
1037
|
const sets = (p) => isEnvName(p.name) ? adding.has(p.name) : (_nullishCoalesce(p.options, () => ( []))).some((o) => Object.keys(_nullishCoalesce(o.env, () => ( {}))).some((k) => adding.has(k))) || prompts.some(
|
|
1035
|
-
(q) => isEnvName(q.name) && adding.has(q.name) && _optionalChain([q, 'access',
|
|
1038
|
+
(q) => isEnvName(q.name) && adding.has(q.name) && _optionalChain([q, 'access', _20 => _20.when, 'optionalAccess', _21 => _21[p.name]]) !== void 0
|
|
1036
1039
|
);
|
|
1037
1040
|
for (const p of prompts) {
|
|
1038
1041
|
const dup = plan.prompts.some(
|
|
@@ -1045,7 +1048,7 @@ function planAdd(cwd, registry, names, opts = {}, cfg = _nullishCoalesce(readEfc
|
|
|
1045
1048
|
}
|
|
1046
1049
|
function readEnvKeys(file) {
|
|
1047
1050
|
if (!_fs2.default.existsSync(file)) return [];
|
|
1048
|
-
return _fs2.default.readFileSync(file, "utf8").split("\n").map((l) => _optionalChain([/^([A-Za-z0-9_]+)=/, 'access',
|
|
1051
|
+
return _fs2.default.readFileSync(file, "utf8").split("\n").map((l) => _optionalChain([/^([A-Za-z0-9_]+)=/, 'access', _22 => _22.exec, 'call', _23 => _23(l), 'optionalAccess', _24 => _24[1]])).filter((k) => !!k);
|
|
1049
1052
|
}
|
|
1050
1053
|
function appendEnv(file, lines) {
|
|
1051
1054
|
if (lines.length === 0) return;
|
|
@@ -1058,7 +1061,7 @@ function promptApplies(p, answers) {
|
|
|
1058
1061
|
return Object.entries(_nullishCoalesce(p.when, () => ( {}))).every(([k, v]) => answers[k] === v);
|
|
1059
1062
|
}
|
|
1060
1063
|
function promptDefault(p, answers) {
|
|
1061
|
-
return _optionalChain([p, 'access',
|
|
1064
|
+
return _optionalChain([p, 'access', _25 => _25.default, 'optionalAccess', _26 => _26.replace, 'call', _27 => _27(/\{\{(\w+)\}\}/g, (_, k) => _nullishCoalesce(answers[k], () => ( "")))]);
|
|
1062
1065
|
}
|
|
1063
1066
|
function normalizeAnswer(p, raw, cwd) {
|
|
1064
1067
|
let value = p.stripSpaces ? raw.replace(/\s/g, "") : raw.trim();
|
|
@@ -1079,8 +1082,8 @@ function envFromAnswers(prompts, answers) {
|
|
|
1079
1082
|
for (const p of prompts) {
|
|
1080
1083
|
if (!(p.name in answers) || !promptApplies(p, answers)) continue;
|
|
1081
1084
|
if (isEnvName(p.name)) env[p.name] = answers[p.name];
|
|
1082
|
-
const chosen = _optionalChain([p, 'access',
|
|
1083
|
-
Object.assign(env, _nullishCoalesce(_optionalChain([chosen, 'optionalAccess',
|
|
1085
|
+
const chosen = _optionalChain([p, 'access', _28 => _28.options, 'optionalAccess', _29 => _29.find, 'call', _30 => _30((o) => o.value === answers[p.name])]);
|
|
1086
|
+
Object.assign(env, _nullishCoalesce(_optionalChain([chosen, 'optionalAccess', _31 => _31.env]), () => ( {})));
|
|
1084
1087
|
}
|
|
1085
1088
|
return env;
|
|
1086
1089
|
}
|
|
@@ -1111,7 +1114,7 @@ function applyAdd(cwd, plan, registry, env = {}) {
|
|
|
1111
1114
|
_fs2.default.mkdirSync(_path2.default.dirname(abs), { recursive: true });
|
|
1112
1115
|
_fs2.default.writeFileSync(abs, f.content);
|
|
1113
1116
|
result.written.push(f.path);
|
|
1114
|
-
const model = _optionalChain([/^src\/model\/([A-Za-z0-9_]+)\.[jt]s$/, 'access',
|
|
1117
|
+
const model = _optionalChain([/^src\/model\/([A-Za-z0-9_]+)\.[jt]s$/, 'access', _32 => _32.exec, 'call', _33 => _33(f.path), 'optionalAccess', _34 => _34[1]]);
|
|
1115
1118
|
if (model && model !== "index" && !existed) newModels.push(model);
|
|
1116
1119
|
}
|
|
1117
1120
|
cfg.files[f.path] = sha256(f.content);
|
|
@@ -1264,6 +1267,200 @@ function lineDiff(a, b) {
|
|
|
1264
1267
|
});
|
|
1265
1268
|
return shown;
|
|
1266
1269
|
}
|
|
1270
|
+
var RESERVED_ENTITIES = /* @__PURE__ */ new Set(["user", "admin"]);
|
|
1271
|
+
function toPascal(s) {
|
|
1272
|
+
const parts = s.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[-_]+/g, " ").trim().split(/\s+/);
|
|
1273
|
+
return parts.map((p) => p.charAt(0).toUpperCase() + p.slice(1).toLowerCase()).join("");
|
|
1274
|
+
}
|
|
1275
|
+
function toCamel(s) {
|
|
1276
|
+
const p = toPascal(s);
|
|
1277
|
+
return p.charAt(0).toLowerCase() + p.slice(1);
|
|
1278
|
+
}
|
|
1279
|
+
function toSlug(s) {
|
|
1280
|
+
return s.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[-_\s]+/g, "-").toLowerCase();
|
|
1281
|
+
}
|
|
1282
|
+
function validateEntityName(name) {
|
|
1283
|
+
const raw = name.trim();
|
|
1284
|
+
if (!raw) return { error: "Entity name cannot be empty" };
|
|
1285
|
+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(raw))
|
|
1286
|
+
return { error: `Entity name must be alphanumeric and start with a letter: "${raw}"` };
|
|
1287
|
+
const pascal = toPascal(raw);
|
|
1288
|
+
const camel = toCamel(raw);
|
|
1289
|
+
const slug = toSlug(raw);
|
|
1290
|
+
if (RESERVED_ENTITIES.has(slug) || RESERVED_ENTITIES.has(pascal.toLowerCase()))
|
|
1291
|
+
return { error: `"${pascal}" is a built-in entity \u2014 use \`efc add auth\` for User/Admin` };
|
|
1292
|
+
return { pascal, camel, slug };
|
|
1293
|
+
}
|
|
1294
|
+
function entityUsesTables(cfg) {
|
|
1295
|
+
return cfg.database === "postgresql" && cfg.modelApi === "defineTable";
|
|
1296
|
+
}
|
|
1297
|
+
function entityOrm(cfg) {
|
|
1298
|
+
const t = entityUsesTables(cfg);
|
|
1299
|
+
return {
|
|
1300
|
+
findOne: (model, filter) => t ? `${model}.findFirst({ where: ${filter} })` : `${model}.findOne(${filter})`,
|
|
1301
|
+
create: (model, data) => t ? `${model}.create({ data: ${data} })` : `${model}.create(${data})`
|
|
1302
|
+
};
|
|
1303
|
+
}
|
|
1304
|
+
function entityModelContent(pascal, slug, cfg) {
|
|
1305
|
+
const api = entityUsesTables(cfg) ? "defineTable" : "defineModel";
|
|
1306
|
+
return `import { ${api} } from 'express-file-cluster';
|
|
1307
|
+
|
|
1308
|
+
export const ${pascal} = ${api}('${pascal}', {
|
|
1309
|
+
name: { type: 'string', required: true },
|
|
1310
|
+
email: { type: 'string', required: true, unique: true },
|
|
1311
|
+
password: { type: 'string', required: true },
|
|
1312
|
+
role: { type: 'string', required: true, default: '${slug}' },
|
|
1313
|
+
avatar: { type: 'string' },
|
|
1314
|
+
isVerified: { type: 'boolean', default: false },
|
|
1315
|
+
isActive: { type: 'boolean', default: true },
|
|
1316
|
+
verifyToken: { type: 'string' },
|
|
1317
|
+
resetToken: { type: 'string' },
|
|
1318
|
+
resetTokenExpiry: { type: 'date' },
|
|
1319
|
+
refreshToken: { type: 'string' },
|
|
1320
|
+
refreshTokenExpiry: { type: 'date' },
|
|
1321
|
+
});
|
|
1322
|
+
`;
|
|
1323
|
+
}
|
|
1324
|
+
function entityRegisterContent(pascal, camel, cfg) {
|
|
1325
|
+
const ts = cfg.language === "typescript";
|
|
1326
|
+
const db = entityOrm(cfg);
|
|
1327
|
+
const typedArgs = ts ? "(req: Request, res: Response)" : "(req, res)";
|
|
1328
|
+
const typeImport = ts ? "import type { Request, Response } from 'express';\n" : "";
|
|
1329
|
+
return `${typeImport}import { hashPassword } from 'express-file-cluster/auth';
|
|
1330
|
+
import { ${pascal} } from '../../../model/${pascal}.js';
|
|
1331
|
+
|
|
1332
|
+
export const POST = async ${typedArgs} => {
|
|
1333
|
+
const { name, email, password } = req.body;
|
|
1334
|
+
if (!name || !email || !password) {
|
|
1335
|
+
return res.status(400).json({ error: 'name, email and password are required' });
|
|
1336
|
+
}
|
|
1337
|
+
const existing = await ${db.findOne(pascal, "{ email }")};
|
|
1338
|
+
if (existing) return res.status(409).json({ error: 'Email already in use' });
|
|
1339
|
+
const hashed = await hashPassword(password);
|
|
1340
|
+
const record = await ${db.create(pascal, "{ name, email, password: hashed }")};
|
|
1341
|
+
const { password: _, ...safe } = record;
|
|
1342
|
+
res.status(201).json({ message: '${pascal} account created', ${camel}: safe });
|
|
1343
|
+
};
|
|
1344
|
+
`;
|
|
1345
|
+
}
|
|
1346
|
+
function entityLoginContent(pascal, slug, cfg) {
|
|
1347
|
+
const ts = cfg.language === "typescript";
|
|
1348
|
+
const db = entityOrm(cfg);
|
|
1349
|
+
const typedArgs = ts ? "(req: Request, res: Response)" : "(req, res)";
|
|
1350
|
+
const typeImport = ts ? "import type { Request, Response } from 'express';\n" : "";
|
|
1351
|
+
return `${typeImport}import { issueToken, verifyPassword } from 'express-file-cluster/auth';
|
|
1352
|
+
import { ${pascal} } from '../../../model/${pascal}.js';
|
|
1353
|
+
|
|
1354
|
+
export const POST = async ${typedArgs} => {
|
|
1355
|
+
const { email, password } = req.body;
|
|
1356
|
+
if (!email || !password) return res.status(400).json({ error: 'email and password are required' });
|
|
1357
|
+
|
|
1358
|
+
const record = await ${db.findOne(pascal, "{ email }")};
|
|
1359
|
+
if (!record) return res.status(401).json({ error: 'Invalid credentials' });
|
|
1360
|
+
const match = await verifyPassword(password, record.password);
|
|
1361
|
+
if (!match) return res.status(401).json({ error: 'Invalid credentials' });
|
|
1362
|
+
if (!record.isActive) return res.status(403).json({ error: 'Account suspended' });
|
|
1363
|
+
await issueToken(res, { id: record.id, role: record.role, email: record.email });
|
|
1364
|
+
res.json({ message: 'Logged in as ${slug}' });
|
|
1365
|
+
};
|
|
1366
|
+
`;
|
|
1367
|
+
}
|
|
1368
|
+
function entityLogoutContent(cfg) {
|
|
1369
|
+
const ts = cfg.language === "typescript";
|
|
1370
|
+
const typedArgs = ts ? "(req: Request, res: Response)" : "(req, res)";
|
|
1371
|
+
const typeImport = ts ? "import type { Request, Response } from 'express';\n" : "";
|
|
1372
|
+
return `${typeImport}import { requireAuth, revokeSession } from 'express-file-cluster/auth';
|
|
1373
|
+
|
|
1374
|
+
export const middlewares = [requireAuth];
|
|
1375
|
+
|
|
1376
|
+
export const POST = async ${typedArgs} => {
|
|
1377
|
+
try {
|
|
1378
|
+
await revokeSession(req, res);
|
|
1379
|
+
} catch {
|
|
1380
|
+
res.clearCookie('efc_token');
|
|
1381
|
+
}
|
|
1382
|
+
res.json({ message: 'Logged out successfully' });
|
|
1383
|
+
};
|
|
1384
|
+
`;
|
|
1385
|
+
}
|
|
1386
|
+
function entityMeContent(slug, cfg) {
|
|
1387
|
+
const ts = cfg.language === "typescript";
|
|
1388
|
+
const typedArgs = ts ? "(req: Request, res: Response)" : "(req, res)";
|
|
1389
|
+
const typeImport = ts ? "import type { Request, Response } from 'express';\n" : "";
|
|
1390
|
+
return `${typeImport}import { requireAuth } from 'express-file-cluster/auth';
|
|
1391
|
+
|
|
1392
|
+
export const middlewares = [requireAuth('${slug}')];
|
|
1393
|
+
|
|
1394
|
+
export const GET = async ${typedArgs} => {
|
|
1395
|
+
res.json((req${ts ? " as any" : ""}).user);
|
|
1396
|
+
};
|
|
1397
|
+
`;
|
|
1398
|
+
}
|
|
1399
|
+
function generateEntityAuth(cwd, entityName, cfg, options) {
|
|
1400
|
+
const validated = validateEntityName(entityName);
|
|
1401
|
+
if ("error" in validated) throw new Error(`[EFC] ${validated.error}`);
|
|
1402
|
+
const { pascal, camel, slug } = validated;
|
|
1403
|
+
const ext = cfg.language === "typescript" ? "ts" : "js";
|
|
1404
|
+
const result = { written: [], manual: [], conflicts: [] };
|
|
1405
|
+
const files = [
|
|
1406
|
+
{ rel: `src/model/${pascal}.${ext}`, content: entityModelContent(pascal, slug, cfg) },
|
|
1407
|
+
{
|
|
1408
|
+
rel: `src/api/auth/${slug}/register.${ext}`,
|
|
1409
|
+
content: entityRegisterContent(pascal, camel, cfg)
|
|
1410
|
+
},
|
|
1411
|
+
{
|
|
1412
|
+
rel: `src/api/auth/${slug}/login.${ext}`,
|
|
1413
|
+
content: entityLoginContent(pascal, slug, cfg)
|
|
1414
|
+
},
|
|
1415
|
+
{
|
|
1416
|
+
rel: `src/api/auth/${slug}/logout.${ext}`,
|
|
1417
|
+
content: entityLogoutContent(cfg)
|
|
1418
|
+
},
|
|
1419
|
+
{
|
|
1420
|
+
rel: `src/api/auth/${slug}/me.${ext}`,
|
|
1421
|
+
content: entityMeContent(slug, cfg)
|
|
1422
|
+
}
|
|
1423
|
+
];
|
|
1424
|
+
for (const f of files) {
|
|
1425
|
+
const abs = _path2.default.join(cwd, f.rel);
|
|
1426
|
+
const exists = _fs2.default.existsSync(abs);
|
|
1427
|
+
if (exists && !_optionalChain([options, 'optionalAccess', _35 => _35.overwrite])) {
|
|
1428
|
+
result.conflicts.push(f.rel);
|
|
1429
|
+
result.manual.push(`${f.rel} already exists \u2014 skipped (pass --overwrite to replace)`);
|
|
1430
|
+
continue;
|
|
1431
|
+
}
|
|
1432
|
+
result.written.push(f.rel);
|
|
1433
|
+
if (!_optionalChain([options, 'optionalAccess', _36 => _36.dryRun])) {
|
|
1434
|
+
_fs2.default.mkdirSync(_path2.default.dirname(abs), { recursive: true });
|
|
1435
|
+
_fs2.default.writeFileSync(abs, f.content);
|
|
1436
|
+
cfg.files[f.rel] = sha256(f.content);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
if (entityUsesTables(cfg) && !_optionalChain([options, 'optionalAccess', _37 => _37.dryRun])) {
|
|
1440
|
+
result.manual.push(...registerModels(cwd, cfg.language, [pascal]));
|
|
1441
|
+
}
|
|
1442
|
+
const envFile = _path2.default.join(cwd, ".env");
|
|
1443
|
+
if (_fs2.default.existsSync(envFile)) {
|
|
1444
|
+
const envKeys = readEnvKeys(envFile);
|
|
1445
|
+
if (!envKeys.includes("JWT_SECRET")) {
|
|
1446
|
+
const secret = _crypto2.default.randomBytes(64).toString("hex");
|
|
1447
|
+
if (!_optionalChain([options, 'optionalAccess', _38 => _38.dryRun])) {
|
|
1448
|
+
appendEnv(envFile, [`JWT_SECRET=${secret}`]);
|
|
1449
|
+
const exampleFile = _path2.default.join(cwd, ".env.example");
|
|
1450
|
+
if (_fs2.default.existsSync(exampleFile) && !readEnvKeys(exampleFile).includes("JWT_SECRET")) {
|
|
1451
|
+
appendEnv(exampleFile, ["JWT_SECRET=<generate with: openssl rand -hex 64>"]);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
result.manual.push("Added JWT_SECRET to .env");
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
const itemName = `auth:${slug}`;
|
|
1458
|
+
if (!cfg.items.includes(itemName)) cfg.items.push(itemName);
|
|
1459
|
+
if (!_optionalChain([options, 'optionalAccess', _39 => _39.dryRun])) {
|
|
1460
|
+
writeEfcJson(cwd, cfg);
|
|
1461
|
+
}
|
|
1462
|
+
return result;
|
|
1463
|
+
}
|
|
1267
1464
|
|
|
1268
1465
|
// src/cli/commands/registry.ts
|
|
1269
1466
|
function registryCommands() {
|
|
@@ -1442,16 +1639,85 @@ function packageManager(cwd) {
|
|
|
1442
1639
|
}
|
|
1443
1640
|
function addCommand() {
|
|
1444
1641
|
return new (0, _commander.Command)("add").description("Add features to the app from the registry (copies their source into src/)").argument(
|
|
1445
|
-
"
|
|
1446
|
-
"registry items
|
|
1447
|
-
).option("-y, --yes", "skip the confirmation").option("--overwrite", "replace files that were changed in this app").option("--dry-run", "show what would change without writing anything").option("--no-install", "don't run the package manager afterwards").option("--no-prompt", "don't ask for credentials \u2014 write placeholders to .env instead").action(
|
|
1642
|
+
"[items...]",
|
|
1643
|
+
"registry items (auth forgot-password admin-users) or custom auth entities (auth:merchant, auth --entity merchant)"
|
|
1644
|
+
).option("-y, --yes", "skip the confirmation").option("--overwrite", "replace files that were changed in this app").option("--dry-run", "show what would change without writing anything").option("--no-install", "don't run the package manager afterwards").option("--no-prompt", "don't ask for credentials \u2014 write placeholders to .env instead").option("--entity <name>", "scaffold a custom auth entity (model + auth routes)").action(
|
|
1448
1645
|
async (names, opts) => {
|
|
1449
1646
|
try {
|
|
1647
|
+
const rawNames = _nullishCoalesce(names, () => ( []));
|
|
1648
|
+
const entityNames = [];
|
|
1649
|
+
const registryNames = [];
|
|
1650
|
+
let i = 0;
|
|
1651
|
+
if (rawNames[0] === "auth" && rawNames[1] === "new" && rawNames.length >= 3) {
|
|
1652
|
+
for (let j = 2; j < rawNames.length; j++) {
|
|
1653
|
+
entityNames.push(rawNames[j]);
|
|
1654
|
+
}
|
|
1655
|
+
i = rawNames.length;
|
|
1656
|
+
}
|
|
1657
|
+
for (; i < rawNames.length; i++) {
|
|
1658
|
+
const n = rawNames[i];
|
|
1659
|
+
const colonMatch = /^auth:([A-Za-z][A-Za-z0-9_-]*)$/.exec(n);
|
|
1660
|
+
if (colonMatch) {
|
|
1661
|
+
entityNames.push(colonMatch[1]);
|
|
1662
|
+
} else if (opts.entity && n === "auth") {
|
|
1663
|
+
continue;
|
|
1664
|
+
} else {
|
|
1665
|
+
registryNames.push(n);
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
if (opts.entity && !entityNames.includes(opts.entity)) {
|
|
1669
|
+
entityNames.push(opts.entity);
|
|
1670
|
+
}
|
|
1671
|
+
if (entityNames.length === 0 && registryNames.length === 0) {
|
|
1672
|
+
fail(
|
|
1673
|
+
"Specify at least one item or entity to add \u2014 e.g. `efc add auth` or `efc add auth:merchant`. Run `efc list` to see available features."
|
|
1674
|
+
);
|
|
1675
|
+
}
|
|
1676
|
+
if (entityNames.length > 0) {
|
|
1677
|
+
const { cwd: cwd2, cfg: cfg2, detected: detected2 } = load();
|
|
1678
|
+
if (detected2) {
|
|
1679
|
+
console.log(
|
|
1680
|
+
_picocolors2.default.dim(`
|
|
1681
|
+
No ${EFC_JSON} yet \u2014 detected ${describe(cfg2)}; it'll be created.`)
|
|
1682
|
+
);
|
|
1683
|
+
}
|
|
1684
|
+
for (const eName of entityNames) {
|
|
1685
|
+
const validated = validateEntityName(eName);
|
|
1686
|
+
if ("error" in validated) fail(validated.error);
|
|
1687
|
+
const { pascal, slug } = validated;
|
|
1688
|
+
console.log(_picocolors2.default.bold(`
|
|
1689
|
+
Adding auth entity: ${pascal}
|
|
1690
|
+
`));
|
|
1691
|
+
const result2 = generateEntityAuth(cwd2, eName, cfg2, {
|
|
1692
|
+
dryRun: opts.dryRun,
|
|
1693
|
+
overwrite: opts.overwrite
|
|
1694
|
+
});
|
|
1695
|
+
if (result2.written.length) {
|
|
1696
|
+
for (const f of result2.written) console.log(` ${_picocolors2.default.green("create ")} ${f}`);
|
|
1697
|
+
const actionVerb = opts.dryRun ? "would be written (dry run)" : "written";
|
|
1698
|
+
console.log(_picocolors2.default.green(`
|
|
1699
|
+
\u2713 ${result2.written.length} file(s) ${actionVerb}`));
|
|
1700
|
+
}
|
|
1701
|
+
for (const m of result2.manual) console.log(_picocolors2.default.yellow(` ! ${m}`));
|
|
1702
|
+
console.log(
|
|
1703
|
+
_picocolors2.default.dim(
|
|
1704
|
+
`
|
|
1705
|
+
Routes: POST /auth/${slug}/register, /login, /logout; GET /me`
|
|
1706
|
+
)
|
|
1707
|
+
);
|
|
1708
|
+
console.log(
|
|
1709
|
+
_picocolors2.default.dim(` Protect routes with: requireAuth('${slug}')`)
|
|
1710
|
+
);
|
|
1711
|
+
}
|
|
1712
|
+
console.log();
|
|
1713
|
+
if (registryNames.length === 0) return;
|
|
1714
|
+
}
|
|
1450
1715
|
const { cwd, registry, cfg, detected } = load();
|
|
1451
|
-
const
|
|
1716
|
+
const itemsToAdd = registryNames;
|
|
1717
|
+
const plan = planAdd(cwd, registry, itemsToAdd, { overwrite: !!opts.overwrite }, cfg);
|
|
1452
1718
|
if (plan.problems.length) fail(plan.problems.join("\n"));
|
|
1453
1719
|
if (plan.items.length === 0) {
|
|
1454
|
-
console.log(_picocolors2.default.dim(`Already installed: ${
|
|
1720
|
+
console.log(_picocolors2.default.dim(`Already installed: ${itemsToAdd.join(", ")}`));
|
|
1455
1721
|
return;
|
|
1456
1722
|
}
|
|
1457
1723
|
if (detected) {
|
|
@@ -1530,7 +1796,7 @@ function listCommand() {
|
|
|
1530
1796
|
try {
|
|
1531
1797
|
const cwd = process.cwd();
|
|
1532
1798
|
const registry = new Registry();
|
|
1533
|
-
const installed = new Set(_nullishCoalesce(_optionalChain([readEfcJson, 'call',
|
|
1799
|
+
const installed = new Set(_nullishCoalesce(_optionalChain([readEfcJson, 'call', _40 => _40(cwd), 'optionalAccess', _41 => _41.items]), () => ( [])));
|
|
1534
1800
|
const groups = [...new Set(registry.index.items.map((i) => i.group))];
|
|
1535
1801
|
for (const group of groups) {
|
|
1536
1802
|
console.log(_picocolors2.default.bold(`
|
|
@@ -1541,6 +1807,13 @@ function listCommand() {
|
|
|
1541
1807
|
console.log(` ${mark} ${_picocolors2.default.cyan(item.name.padEnd(20))} ${item.description}${deps}`);
|
|
1542
1808
|
}
|
|
1543
1809
|
}
|
|
1810
|
+
const entities = (_nullishCoalesce(_optionalChain([readEfcJson, 'call', _42 => _42(cwd), 'optionalAccess', _43 => _43.items]), () => ( []))).filter((i) => i.startsWith("auth:")).map((i) => i.slice(5));
|
|
1811
|
+
if (entities.length > 0) {
|
|
1812
|
+
console.log(_picocolors2.default.bold("\n custom auth entities"));
|
|
1813
|
+
for (const e of entities) {
|
|
1814
|
+
console.log(` ${_picocolors2.default.green("\u2713")} ${_picocolors2.default.cyan(e.padEnd(20))} model + auth routes for ${e}`);
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1544
1817
|
console.log(_picocolors2.default.dim("\n efc add <item...> \xB7 efc view <item>\n"));
|
|
1545
1818
|
} catch (err) {
|
|
1546
1819
|
fail(err);
|
|
@@ -1585,6 +1858,22 @@ function diffCommand() {
|
|
|
1585
1858
|
const names = name ? [name] : cfg.items;
|
|
1586
1859
|
let clean = true;
|
|
1587
1860
|
for (const n of names) {
|
|
1861
|
+
if (!registry.has(n)) {
|
|
1862
|
+
if (n.startsWith("auth:")) {
|
|
1863
|
+
if (name) {
|
|
1864
|
+
console.log(
|
|
1865
|
+
_picocolors2.default.dim(
|
|
1866
|
+
`
|
|
1867
|
+
${n} is a custom auth entity (files are owned by your project, no upstream registry item to diff).`
|
|
1868
|
+
)
|
|
1869
|
+
);
|
|
1870
|
+
}
|
|
1871
|
+
} else {
|
|
1872
|
+
console.log(_picocolors2.default.dim(`
|
|
1873
|
+
${n} is not a registry item.`));
|
|
1874
|
+
}
|
|
1875
|
+
continue;
|
|
1876
|
+
}
|
|
1588
1877
|
for (const f of itemStatus(cwd, registry, cfg, n)) {
|
|
1589
1878
|
if (f.status === "unchanged") continue;
|
|
1590
1879
|
clean = false;
|
|
@@ -1676,15 +1965,15 @@ function parseAttrs(s) {
|
|
|
1676
1965
|
return out;
|
|
1677
1966
|
}
|
|
1678
1967
|
function arg(a, name) {
|
|
1679
|
-
if (!_optionalChain([a, 'optionalAccess',
|
|
1968
|
+
if (!_optionalChain([a, 'optionalAccess', _44 => _44.args])) return void 0;
|
|
1680
1969
|
const parts = splitTop(a.args, ",");
|
|
1681
1970
|
if (name) {
|
|
1682
1971
|
const p = parts.find((x) => x.startsWith(`${name}:`));
|
|
1683
|
-
return _optionalChain([p, 'optionalAccess',
|
|
1972
|
+
return _optionalChain([p, 'optionalAccess', _45 => _45.slice, 'call', _46 => _46(name.length + 1), 'access', _47 => _47.trim, 'call', _48 => _48()]);
|
|
1684
1973
|
}
|
|
1685
1974
|
return parts.find((x) => !/^\w+\s*:/.test(x));
|
|
1686
1975
|
}
|
|
1687
|
-
var unquote = (s) => _optionalChain([s, 'optionalAccess',
|
|
1976
|
+
var unquote = (s) => _optionalChain([s, 'optionalAccess', _49 => _49.replace, 'call', _50 => _50(/^"(.*)"$/, "$1")]);
|
|
1688
1977
|
var list = (s) => s ? splitTop(s.replace(/^\[|\]$/g, ""), ",").map((x) => x.trim()).filter(Boolean) : [];
|
|
1689
1978
|
function parsePrisma(source) {
|
|
1690
1979
|
const text = source.replace(/\/\/.*$/gm, "");
|
|
@@ -1697,7 +1986,7 @@ function parsePrisma(source) {
|
|
|
1697
1986
|
const [, kind, name, body] = block;
|
|
1698
1987
|
const lines = body.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
1699
1988
|
if (kind === "datasource") {
|
|
1700
|
-
provider = unquote(_optionalChain([/provider\s*=\s*("[^"]*")/, 'access',
|
|
1989
|
+
provider = unquote(_optionalChain([/provider\s*=\s*("[^"]*")/, 'access', _51 => _51.exec, 'call', _52 => _52(body), 'optionalAccess', _53 => _53[1]]));
|
|
1701
1990
|
} else if (kind === "enum") {
|
|
1702
1991
|
const values = [];
|
|
1703
1992
|
const names = [];
|
|
@@ -1759,7 +2048,7 @@ function importPrisma(source) {
|
|
|
1759
2048
|
const def = _nullishCoalesce(arg(attr(_nullishCoalesce(id, () => ( { attrs: [] })), "@default")), () => ( ""));
|
|
1760
2049
|
idKind.set(
|
|
1761
2050
|
m.name,
|
|
1762
|
-
_optionalChain([id, 'optionalAccess',
|
|
2051
|
+
_optionalChain([id, 'optionalAccess', _54 => _54.type]) === "Int" && /autoincrement/.test(def) ? "serial" : _optionalChain([id, 'optionalAccess', _55 => _55.type]) === "BigInt" && /autoincrement/.test(def) ? "bigserial" : "uuid"
|
|
1763
2052
|
);
|
|
1764
2053
|
}
|
|
1765
2054
|
const rels = [];
|
|
@@ -1789,7 +2078,7 @@ function importPrisma(source) {
|
|
|
1789
2078
|
const fields = [];
|
|
1790
2079
|
const relations = {};
|
|
1791
2080
|
const table = _nullishCoalesce(unquote(arg(blocks(m, "@@map")[0])), () => ( m.name));
|
|
1792
|
-
if (table !==
|
|
2081
|
+
if (table !== _chunkX2US4RAMcjs.tableNameFor.call(void 0, m.name)) options["collection"] = table;
|
|
1793
2082
|
const kind = idKind.get(m.name);
|
|
1794
2083
|
if (kind !== "uuid") options["id"] = kind;
|
|
1795
2084
|
if (blocks(m, "@@id").length) {
|
|
@@ -1845,7 +2134,7 @@ function importPrisma(source) {
|
|
|
1845
2134
|
continue;
|
|
1846
2135
|
}
|
|
1847
2136
|
const other = partner(r);
|
|
1848
|
-
if (r.list && _optionalChain([other, 'optionalAccess',
|
|
2137
|
+
if (r.list && _optionalChain([other, 'optionalAccess', _56 => _56.list])) {
|
|
1849
2138
|
if (m.name < r.target || m.name === r.target && f.name < other.field) {
|
|
1850
2139
|
fields.push([
|
|
1851
2140
|
f.name,
|
|
@@ -1915,7 +2204,7 @@ function importPrisma(source) {
|
|
|
1915
2204
|
}
|
|
1916
2205
|
function scalarDef(f, enums, notes) {
|
|
1917
2206
|
const db = f.attrs.find((a) => a.name.startsWith("@db."));
|
|
1918
|
-
const dbType = _optionalChain([db, 'optionalAccess',
|
|
2207
|
+
const dbType = _optionalChain([db, 'optionalAccess', _57 => _57.name, 'access', _58 => _58.slice, 'call', _59 => _59(4)]);
|
|
1919
2208
|
let def;
|
|
1920
2209
|
const e = enums.get(f.type);
|
|
1921
2210
|
if (e) def = { type: "string", enum: e.values, nativeEnum: true };
|
|
@@ -1923,7 +2212,7 @@ function scalarDef(f, enums, notes) {
|
|
|
1923
2212
|
switch (f.type) {
|
|
1924
2213
|
case "String":
|
|
1925
2214
|
if (dbType === "Uuid") def = { type: "uuid" };
|
|
1926
|
-
else if (dbType === "VarChar" && _optionalChain([db, 'optionalAccess',
|
|
2215
|
+
else if (dbType === "VarChar" && _optionalChain([db, 'optionalAccess', _60 => _60.args]))
|
|
1927
2216
|
def = { type: "string", maxLength: Number(db.args) };
|
|
1928
2217
|
else def = { type: "string" };
|
|
1929
2218
|
break;
|
|
@@ -1937,7 +2226,7 @@ function scalarDef(f, enums, notes) {
|
|
|
1937
2226
|
def = { type: "number" };
|
|
1938
2227
|
break;
|
|
1939
2228
|
case "Decimal": {
|
|
1940
|
-
const [p, s] = (_nullishCoalesce(_optionalChain([db, 'optionalAccess',
|
|
2229
|
+
const [p, s] = (_nullishCoalesce(_optionalChain([db, 'optionalAccess', _61 => _61.args]), () => ( ""))).split(",").map((x) => Number(x.trim()));
|
|
1941
2230
|
def = p ? { type: "decimal", precision: p, scale: _nullishCoalesce(s, () => ( 0)) } : { type: "decimal" };
|
|
1942
2231
|
break;
|
|
1943
2232
|
}
|