@saasicat/cli 0.26.1 → 0.27.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
@@ -1072,21 +1072,21 @@ function structuralOnly(line) {
1072
1072
  return stripLineComment(blankStringLiterals(line));
1073
1073
  }
1074
1074
  __name(structuralOnly, "structuralOnly");
1075
- function extractBlockNames(schema, keyword) {
1075
+ function extractBlockNames(schema2, keyword) {
1076
1076
  const pattern = declarationPattern(keyword);
1077
1077
  const names = [];
1078
- for (const line of schema.split("\n")) {
1078
+ for (const line of schema2.split("\n")) {
1079
1079
  const match = structuralOnly(line).match(pattern);
1080
1080
  if (match) names.push(match[1]);
1081
1081
  }
1082
1082
  return names;
1083
1083
  }
1084
1084
  __name(extractBlockNames, "extractBlockNames");
1085
- function extractBlocks(schema, keyword) {
1085
+ function extractBlocks(schema2, keyword) {
1086
1086
  const pattern = declarationPattern(keyword);
1087
1087
  const blocks = /* @__PURE__ */ new Map();
1088
1088
  let current = null;
1089
- for (const rawLine of schema.split("\n")) {
1089
+ for (const rawLine of schema2.split("\n")) {
1090
1090
  const stripped = structuralOnly(rawLine);
1091
1091
  const openCount = (stripped.match(/\{/g) ?? []).length;
1092
1092
  const closeCount = (stripped.match(/\}/g) ?? []).length;
@@ -1121,16 +1121,16 @@ function blockBodyLines(block) {
1121
1121
  __name(blockBodyLines, "blockBodyLines");
1122
1122
 
1123
1123
  // src/schema-apply.ts
1124
- function extractModelNames(schema) {
1125
- return extractBlockNames(schema, "model");
1124
+ function extractModelNames(schema2) {
1125
+ return extractBlockNames(schema2, "model");
1126
1126
  }
1127
1127
  __name(extractModelNames, "extractModelNames");
1128
1128
  function extractModelBlocks(fragment) {
1129
1129
  return extractBlocks(fragment, "model");
1130
1130
  }
1131
1131
  __name(extractModelBlocks, "extractModelBlocks");
1132
- function applyFragmentBlocks(schema, fragmentBlocks, options = {}) {
1133
- const existing = new Set(extractModelNames(schema));
1132
+ function applyFragmentBlocks(schema2, fragmentBlocks, options = {}) {
1133
+ const existing = new Set(extractModelNames(schema2));
1134
1134
  const added = [];
1135
1135
  const skipped = [];
1136
1136
  const additions = [];
@@ -1146,7 +1146,7 @@ function applyFragmentBlocks(schema, fragmentBlocks, options = {}) {
1146
1146
  return {
1147
1147
  added,
1148
1148
  skipped,
1149
- schema
1149
+ schema: schema2
1150
1150
  };
1151
1151
  }
1152
1152
  const header = options.fragmentLabel ? `
@@ -1158,7 +1158,7 @@ function applyFragmentBlocks(schema, fragmentBlocks, options = {}) {
1158
1158
 
1159
1159
  // Inserted by \`saasicat schema apply\`
1160
1160
  `;
1161
- const trimmedSchema = schema.endsWith("\n") ? schema : schema + "\n";
1161
+ const trimmedSchema = schema2.endsWith("\n") ? schema2 : schema2 + "\n";
1162
1162
  return {
1163
1163
  added,
1164
1164
  skipped,
@@ -1216,15 +1216,15 @@ function parseBlockAttributes(name, block) {
1216
1216
  };
1217
1217
  }
1218
1218
  __name(parseBlockAttributes, "parseBlockAttributes");
1219
- function parseSchema(schema) {
1219
+ function parseSchema(schema2) {
1220
1220
  const models = /* @__PURE__ */ new Map();
1221
1221
  const modelAttributes = /* @__PURE__ */ new Map();
1222
- for (const [name, block] of extractBlocks(schema, "model")) {
1222
+ for (const [name, block] of extractBlocks(schema2, "model")) {
1223
1223
  models.set(name, parseFields(block));
1224
1224
  modelAttributes.set(name, parseBlockAttributes(name, block));
1225
1225
  }
1226
1226
  const enums = /* @__PURE__ */ new Map();
1227
- for (const [name, block] of extractBlocks(schema, "enum")) {
1227
+ for (const [name, block] of extractBlocks(schema2, "enum")) {
1228
1228
  enums.set(name, parseEnumValues(block));
1229
1229
  }
1230
1230
  return {
@@ -1350,6 +1350,496 @@ function checkSchema(specSchema, appSchema) {
1350
1350
  }
1351
1351
  __name(checkSchema, "checkSchema");
1352
1352
 
1353
+ // src/migration-constraints.ts
1354
+ var CONSTRAINTS_MARKER = "-- saasicat:constraints";
1355
+ function migrationCreatedBy(before, after) {
1356
+ const existing = new Set(before);
1357
+ const created = after.filter((name) => /^\d{14}_/.test(name) && !existing.has(name)).sort();
1358
+ return created.length > 0 ? created[created.length - 1] : null;
1359
+ }
1360
+ __name(migrationCreatedBy, "migrationCreatedBy");
1361
+ function hasConstraints(migrationSql) {
1362
+ return migrationSql.includes(CONSTRAINTS_MARKER);
1363
+ }
1364
+ __name(hasConstraints, "hasConstraints");
1365
+ function tablesAddressedBy(statement) {
1366
+ return [
1367
+ ...statement.matchAll(/\bON\s+"?(\w+)"?|\bALTER\s+TABLE\s+"?(\w+)"?/gi)
1368
+ ].map((m) => m[1] ?? m[2]);
1369
+ }
1370
+ __name(tablesAddressedBy, "tablesAddressedBy");
1371
+ function constraintsFor(constraintsSql, tables) {
1372
+ const known = new Set(tables);
1373
+ return constraintsSql.split(/\n\s*\n/).filter((block) => {
1374
+ const addressed = tablesAddressedBy(block);
1375
+ return addressed.length === 0 || addressed.every((table) => known.has(table));
1376
+ }).join("\n\n").trimEnd();
1377
+ }
1378
+ __name(constraintsFor, "constraintsFor");
1379
+ function appendConstraints(migrationSql, constraintsSql) {
1380
+ if (hasConstraints(migrationSql)) return migrationSql;
1381
+ if (constraintsSql.trim() === "") return migrationSql;
1382
+ const body = migrationSql.endsWith("\n") ? migrationSql : `${migrationSql}
1383
+ `;
1384
+ return `${body}
1385
+ ${CONSTRAINTS_MARKER} \u2014 appended by \`saasicat schema migrate\`.
1386
+ -- Source: @saasicat/spec/sql/constraints.postgres.sql. These are part of the
1387
+ -- canonical schema: the adapter contract tests run against a database that has
1388
+ -- them. Edit the spec, not this copy.
1389
+ ${constraintsSql.trimEnd()}
1390
+ `;
1391
+ }
1392
+ __name(appendConstraints, "appendConstraints");
1393
+ function reportConstraints(outcome, context) {
1394
+ switch (outcome) {
1395
+ case "appended":
1396
+ return {
1397
+ outcome,
1398
+ mayApply: true,
1399
+ message: ` + appended to ${context.migration}/migration.sql`
1400
+ };
1401
+ case "already-present":
1402
+ return {
1403
+ outcome,
1404
+ mayApply: true,
1405
+ message: ` = ${context.migration} already carries them.`
1406
+ };
1407
+ case "not-applicable":
1408
+ return {
1409
+ outcome,
1410
+ mayApply: true,
1411
+ message: " = none of them apply to the tables in this schema."
1412
+ };
1413
+ case "no-migration":
1414
+ return {
1415
+ outcome,
1416
+ mayApply: true,
1417
+ message: " = Prisma created no migration \u2014 nothing to append to, and nothing new to apply."
1418
+ };
1419
+ case "failed":
1420
+ return {
1421
+ outcome,
1422
+ mayApply: false,
1423
+ message: ` ! Could not append them, so the migration is incomplete. Add ${context.sqlPath} to it by hand, before applying it \u2014 nothing was applied.`
1424
+ };
1425
+ }
1426
+ }
1427
+ __name(reportConstraints, "reportConstraints");
1428
+
1429
+ // src/fk-pointers.ts
1430
+ var POINTER = /^(\s*)\/\/\s*(\w+)(\s+)(Tenant|User)(\??)(\s+)(@relation\(.*\))\s*$/;
1431
+ function findFkPointers(schema2) {
1432
+ const found = [];
1433
+ let model = "";
1434
+ schema2.split("\n").forEach((text, line) => {
1435
+ const opening = /^model\s+(\w+)\s*\{/.exec(text);
1436
+ if (opening) model = opening[1];
1437
+ const match = POINTER.exec(text);
1438
+ if (match) found.push({
1439
+ line,
1440
+ target: match[4],
1441
+ model,
1442
+ text
1443
+ });
1444
+ });
1445
+ return found;
1446
+ }
1447
+ __name(findFkPointers, "findFkPointers");
1448
+ function relationNameOf(relationAttribute) {
1449
+ const match = /@relation\(\s*"([^"]+)"/.exec(relationAttribute);
1450
+ return match ? match[1] : null;
1451
+ }
1452
+ __name(relationNameOf, "relationNameOf");
1453
+ function escapeForRegExp(value) {
1454
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1455
+ }
1456
+ __name(escapeForRegExp, "escapeForRegExp");
1457
+ function modelBody(schema2, model) {
1458
+ const name = escapeForRegExp(model);
1459
+ const block = new RegExp(`(^|\\n)model\\s+${name}\\s*\\{([\\s\\S]*?)\\n\\}`, "m").exec(schema2);
1460
+ return block ? block[2] : null;
1461
+ }
1462
+ __name(modelBody, "modelBody");
1463
+ function isOneToOne(schema2, model, foreignKey) {
1464
+ const body = modelBody(schema2, model);
1465
+ if (!body) return false;
1466
+ return new RegExp(`^\\s*${escapeForRegExp(foreignKey)}\\s+\\S+.*@unique`, "m").test(body);
1467
+ }
1468
+ __name(isOneToOne, "isOneToOne");
1469
+ function hasBackRelation(schema2, model, owner, relationName, singular = false) {
1470
+ const body = modelBody(schema2, owner);
1471
+ if (body === null) return false;
1472
+ const name = escapeForRegExp(model);
1473
+ const shape = singular ? `${name}\\??` : `${name}\\[\\]`;
1474
+ const candidates = [
1475
+ ...body.matchAll(new RegExp(`^\\s*\\w+\\s+${shape}(\\s.*)?$`, "gm"))
1476
+ ];
1477
+ return candidates.some((line) => relationNameOf(line[0]) === relationName);
1478
+ }
1479
+ __name(hasBackRelation, "hasBackRelation");
1480
+ function enableFkPointers(schema2, models) {
1481
+ const lines = schema2.split("\n");
1482
+ const enabled = [];
1483
+ const skipped = [];
1484
+ const needsBackRelation = [];
1485
+ for (const pointer of findFkPointers(schema2)) {
1486
+ const model = pointer.target === "Tenant" ? models.tenant : models.user;
1487
+ if (!model) {
1488
+ skipped.push({
1489
+ line: pointer.line,
1490
+ target: pointer.target
1491
+ });
1492
+ continue;
1493
+ }
1494
+ const match = POINTER.exec(pointer.text);
1495
+ const relationName = relationNameOf(match[7]);
1496
+ const foreignKey = foreignKeyOf(match[7]);
1497
+ const singular = foreignKey !== null && isOneToOne(schema2, pointer.model, foreignKey);
1498
+ if (!hasBackRelation(schema2, pointer.model, model, relationName, singular)) {
1499
+ needsBackRelation.push({
1500
+ line: pointer.line,
1501
+ owner: model,
1502
+ suggestion: backRelationSuggestion(pointer.model, relationName, singular)
1503
+ });
1504
+ continue;
1505
+ }
1506
+ const [, indent, field, gap1, , optional, gap2, relation] = match;
1507
+ lines[pointer.line] = `${indent}${field}${gap1}${model}${optional}${gap2}${relation}`;
1508
+ enabled.push({
1509
+ line: pointer.line,
1510
+ model
1511
+ });
1512
+ }
1513
+ return {
1514
+ schema: lines.join("\n"),
1515
+ enabled,
1516
+ skipped,
1517
+ needsBackRelation
1518
+ };
1519
+ }
1520
+ __name(enableFkPointers, "enableFkPointers");
1521
+ var lowerFirst = /* @__PURE__ */ __name((value) => value.charAt(0).toLowerCase() + value.slice(1), "lowerFirst");
1522
+ function foreignKeyOf(relationAttribute) {
1523
+ const match = /fields:\s*\[\s*(\w+)/.exec(relationAttribute);
1524
+ return match ? match[1] : null;
1525
+ }
1526
+ __name(foreignKeyOf, "foreignKeyOf");
1527
+ function backRelationSuggestion(model, relationName, singular) {
1528
+ const field = singular ? lowerFirst(model) : `${lowerFirst(model)}s`;
1529
+ const type = singular ? `${model}?` : `${model}[]`;
1530
+ return `${field} ${type}` + (relationName ? ` @relation("${relationName}")` : "");
1531
+ }
1532
+ __name(backRelationSuggestion, "backRelationSuggestion");
1533
+ function assertModelsExist(declaredModels, models) {
1534
+ const missing = Object.entries(models).filter(([, name]) => name && !declaredModels.includes(name)).map(([role, name]) => `--${role}-model=${name}`);
1535
+ if (missing.length === 0) return;
1536
+ throw new Error(`${missing.join(", ")} \u2014 no such model in this schema. It declares: ${declaredModels.slice().sort().join(", ")}.`);
1537
+ }
1538
+ __name(assertModelsExist, "assertModelsExist");
1539
+
1540
+ // src/init/catalog-keys.ts
1541
+ import { planCatalogSchema } from "@saasicat/spec";
1542
+ var schema = planCatalogSchema;
1543
+ function required(value, what) {
1544
+ if (value === void 0) {
1545
+ throw new Error(`plan-catalog.schema.json declares no ${what} \u2014 @saasicat/spec and @saasicat/cli are out of step.`);
1546
+ }
1547
+ return value;
1548
+ }
1549
+ __name(required, "required");
1550
+ function projectKeyPattern() {
1551
+ return new RegExp(required(schema.properties?.projectKey?.pattern, "pattern for projectKey"));
1552
+ }
1553
+ __name(projectKeyPattern, "projectKeyPattern");
1554
+ function quotaKeyPattern() {
1555
+ const patterns = Object.keys(required(schema.$defs?.PlanDef?.properties?.quotas?.patternProperties, "quota key pattern"));
1556
+ if (patterns.length !== 1) {
1557
+ throw new Error(`plan-catalog.schema.json declares ${patterns.length} quota key patterns; this derivation can express one.`);
1558
+ }
1559
+ return new RegExp(patterns[0]);
1560
+ }
1561
+ __name(quotaKeyPattern, "quotaKeyPattern");
1562
+ function minimumQuotasPerPlan() {
1563
+ return required(schema.$defs?.PlanDef?.properties?.quotas?.minProperties, "minProperties");
1564
+ }
1565
+ __name(minimumQuotasPerPlan, "minimumQuotasPerPlan");
1566
+ function assertValidProjectKey(projectKey) {
1567
+ const pattern = projectKeyPattern();
1568
+ if (pattern.test(projectKey)) return;
1569
+ throw new Error(`--project-key=${projectKey} is not a valid project key. It has to match ${pattern.source} \u2014 lower case, starting with a letter, at least two characters. The platform validates config/saas.yaml against the same pattern at boot, so this would fail after every file was written.`);
1570
+ }
1571
+ __name(assertValidProjectKey, "assertValidProjectKey");
1572
+ function assertValidQuotaKey(quotaKey) {
1573
+ const pattern = quotaKeyPattern();
1574
+ if (pattern.test(quotaKey)) return;
1575
+ throw new Error(`--quota=${quotaKey}:\u2026 is not a valid quota key. It has to match ${pattern.source} \u2014 lower camel case, no separators, so \`activeSeats\` rather than \`active-seats\` or \`active_seats\`. The plan's \`quotas\` object forbids additional properties, so the platform rejects config/saas.yaml at boot otherwise.`);
1576
+ }
1577
+ __name(assertValidQuotaKey, "assertValidQuotaKey");
1578
+
1579
+ // src/init/plan.ts
1580
+ function parseQuota(spec) {
1581
+ const [key, model] = spec.split(":");
1582
+ if (!key) throw new Error(`--quota needs a key: got '${spec}'`);
1583
+ assertValidQuotaKey(key);
1584
+ return {
1585
+ key,
1586
+ model: delegateName(model ?? key)
1587
+ };
1588
+ }
1589
+ __name(parseQuota, "parseQuota");
1590
+ var delegateName = /* @__PURE__ */ __name((model) => model.charAt(0).toLowerCase() + model.slice(1), "delegateName");
1591
+ function assertEnoughQuotas(quotas) {
1592
+ const minimum = minimumQuotasPerPlan();
1593
+ if (quotas.length >= minimum) return;
1594
+ throw new Error(`init needs at least ${minimum} --quota=<key>:<Model>. Every plan in config/saas.yaml must declare one, and the platform refuses a catalogue without it \u2014 so a project generated without one cannot boot. Name what your app counts, for example --quota=notes:Note; the generated provider is where you say how to count it.`);
1595
+ }
1596
+ __name(assertEnoughQuotas, "assertEnoughQuotas");
1597
+ function pascalCase(value) {
1598
+ return value.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
1599
+ }
1600
+ __name(pascalCase, "pascalCase");
1601
+ var quotaFileName = /* @__PURE__ */ __name((key) => `${key.replace(/[^A-Za-z0-9]+/g, "-").toLowerCase()}-quota.provider.ts`, "quotaFileName");
1602
+ function planInit(options) {
1603
+ const projectKey = options.projectKey;
1604
+ if (!projectKey) throw new Error("init needs a --project-key.");
1605
+ assertValidProjectKey(projectKey);
1606
+ const appLabel = options.appName ?? pascalCase(projectKey);
1607
+ const appName = pascalCase(appLabel);
1608
+ const apiBase = options.apiBase ?? "/api/v1/admin";
1609
+ const quotas = (options.quotas ?? []).map(parseQuota);
1610
+ assertEnoughQuotas(quotas);
1611
+ const hasherClass = options.skipHasher ? null : `${appName}PasswordHasher`;
1612
+ const featureKey = `${projectKey.replace(/[^A-Za-z0-9]+/g, "_").toUpperCase()}_CORE`;
1613
+ const shared = {
1614
+ PROJECT_KEY: projectKey,
1615
+ APP_NAME: appName,
1616
+ APP_LABEL: appLabel,
1617
+ API_BASE: apiBase,
1618
+ FEATURE_KEY: featureKey,
1619
+ REGISTRY_CONST: `${constantCase(projectKey)}_FEATURE_UI_REGISTRY`,
1620
+ MANIFEST_CONST: `${constantCase(projectKey)}_MANIFEST_CONTRIBUTION`,
1621
+ ADMIN_MODULE_CLASS: `${appName}AdminModule`,
1622
+ HASHER_CLASS: hasherClass ?? "",
1623
+ HASHER_FILE: hasherClass ? `${kebabCase(appName)}-password.hasher` : "",
1624
+ STARTER_QUOTAS: renderQuotaBlock(quotas, 25),
1625
+ PRO_QUOTAS: renderQuotaBlock(quotas, 1e3)
1626
+ };
1627
+ const files = [
1628
+ {
1629
+ path: "config/saas.yaml",
1630
+ template: "config/saas.yaml",
1631
+ tokens: {}
1632
+ },
1633
+ {
1634
+ path: "src/saas/feature-ui-registry.ts",
1635
+ template: "src/saas/feature-ui-registry.ts",
1636
+ tokens: {}
1637
+ },
1638
+ {
1639
+ path: "src/saas/admin-manifest.contribution.ts",
1640
+ template: "src/saas/admin-manifest.contribution.ts",
1641
+ tokens: {}
1642
+ },
1643
+ {
1644
+ path: `src/saas/${kebabCase(appName)}-admin.module.ts`,
1645
+ template: "src/saas/admin.module.ts",
1646
+ tokens: {}
1647
+ }
1648
+ ];
1649
+ files.push({
1650
+ path: "src/saas/persistence.ts",
1651
+ template: hasherClass ? "src/saas/persistence.ts" : "src/saas/persistence-without-hasher.ts",
1652
+ tokens: {}
1653
+ });
1654
+ if (hasherClass) {
1655
+ files.push({
1656
+ path: `src/auth/${kebabCase(appName)}-password.hasher.ts`,
1657
+ template: "src/auth/password.hasher.ts",
1658
+ tokens: {}
1659
+ });
1660
+ }
1661
+ const quotaProviders = quotas.map((quota) => ({
1662
+ className: `${pascalCase(quota.key)}QuotaProvider`,
1663
+ path: `src/saas/${quotaFileName(quota.key)}`
1664
+ }));
1665
+ for (const quota of quotas) {
1666
+ files.push({
1667
+ path: `src/saas/${quotaFileName(quota.key)}`,
1668
+ template: "src/saas/quota.provider.ts",
1669
+ tokens: {
1670
+ QUOTA_KEY: quota.key,
1671
+ QUOTA_LABEL: `${pascalCase(quota.key)} count`,
1672
+ QUOTA_CLASS: `${pascalCase(quota.key)}QuotaProvider`,
1673
+ QUOTA_MODEL: quota.model
1674
+ }
1675
+ });
1676
+ }
1677
+ return {
1678
+ files,
1679
+ tokens: shared,
1680
+ quotaProviders,
1681
+ hasherClass
1682
+ };
1683
+ }
1684
+ __name(planInit, "planInit");
1685
+ var constantCase = /* @__PURE__ */ __name((value) => value.replace(/[^A-Za-z0-9]+/g, "_").toUpperCase(), "constantCase");
1686
+ var kebabCase = /* @__PURE__ */ __name((value) => value.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[^A-Za-z0-9]+/g, "-").toLowerCase(), "kebabCase");
1687
+ function renderQuotaBlock(quotas, limit) {
1688
+ if (quotas.length === 0) return " {}";
1689
+ return quotas.map((quota) => `
1690
+ ${quota.key}: ${limit}`).join("");
1691
+ }
1692
+ __name(renderQuotaBlock, "renderQuotaBlock");
1693
+ function applyTokens(content, tokens) {
1694
+ return content.replace(/__([A-Z_]+)__/g, (full, key) => tokens[key] === void 0 ? full : tokens[key]);
1695
+ }
1696
+ __name(applyTokens, "applyTokens");
1697
+ function patchOptionsFor(plan) {
1698
+ const generates = /* @__PURE__ */ __name((path) => plan.files.some((file) => file.path === path), "generates");
1699
+ return {
1700
+ persistenceImport: generates("src/saas/persistence.ts") ? "./saas/persistence" : null,
1701
+ adminModule: {
1702
+ className: plan.tokens.ADMIN_MODULE_CLASS,
1703
+ importPath: `./saas/${kebabCase(plan.tokens.APP_NAME)}-admin.module`
1704
+ },
1705
+ // Read off the plan, not re-derived: the file it wrote is the file the
1706
+ // import has to name.
1707
+ quotaProviders: plan.quotaProviders.map(({ className, path }) => ({
1708
+ className,
1709
+ importPath: `./${path.replace(/^src\//, "").replace(/\.ts$/, "")}`
1710
+ })),
1711
+ registry: {
1712
+ constName: plan.tokens.REGISTRY_CONST,
1713
+ importPath: "./saas/feature-ui-registry"
1714
+ }
1715
+ };
1716
+ }
1717
+ __name(patchOptionsFor, "patchOptionsFor");
1718
+
1719
+ // src/init/patch-app-module.ts
1720
+ var MARKER = "SaaSiCatModule.forRoot";
1721
+ function patchAppModule(source, options) {
1722
+ const block = renderForRootBlock(options);
1723
+ const manualBlock = `${renderImports(options)}
1724
+
1725
+ ${block}`;
1726
+ if (source.includes(MARKER)) {
1727
+ return {
1728
+ source,
1729
+ status: "already-wired",
1730
+ reason: "",
1731
+ manualBlock: ""
1732
+ };
1733
+ }
1734
+ const importsArray = findImportsArray(source);
1735
+ if (!importsArray) {
1736
+ return {
1737
+ source,
1738
+ status: "declined",
1739
+ reason: "no `@Module({ imports: [ ... ] })` was found in this file, so there is nowhere to add the platform without guessing at the structure",
1740
+ manualBlock
1741
+ };
1742
+ }
1743
+ const withImports = addImportStatements(source, options);
1744
+ const target = findImportsArray(withImports);
1745
+ const indent = " ".repeat(target.indent + 4);
1746
+ const entries = `${block},
1747
+ ${options.adminModule.className},`;
1748
+ const wasEmpty = /^\s*\]/.test(withImports.slice(target.openBracket + 1));
1749
+ const trailing = wasEmpty ? " ".repeat(target.indent) : indent;
1750
+ const inserted = `
1751
+ ${indent}${entries.split("\n").join(`
1752
+ ${indent}`)}
1753
+ ${trailing}`;
1754
+ return {
1755
+ source: withImports.slice(0, target.openBracket + 1) + inserted + withImports.slice(target.openBracket + 1),
1756
+ status: "patched",
1757
+ reason: "",
1758
+ manualBlock: ""
1759
+ };
1760
+ }
1761
+ __name(patchAppModule, "patchAppModule");
1762
+ function findImportsArray(source) {
1763
+ const decorator = source.indexOf("@Module(");
1764
+ if (decorator === -1) return null;
1765
+ const match = /(^|\n)([ \t]*)imports\s*:\s*\[/.exec(source.slice(decorator));
1766
+ if (!match) return null;
1767
+ const openBracket = decorator + match.index + match[0].length - 1;
1768
+ return {
1769
+ openBracket,
1770
+ indent: match[2].length
1771
+ };
1772
+ }
1773
+ __name(findImportsArray, "findImportsArray");
1774
+ function renderImports(options) {
1775
+ const lines = [
1776
+ "import { loadPlanCatalogFromFile } from '@saasicat/nest/billing';",
1777
+ "import { SaaSiCatModule, defineSaaSiCat } from '@saasicat/nest/platform';",
1778
+ `import { ${options.registry.constName} } from '${options.registry.importPath}';`,
1779
+ `import { ${options.adminModule.className} } from '${options.adminModule.importPath}';`
1780
+ ];
1781
+ if (options.persistenceImport) {
1782
+ lines.push(`import { persistence } from '${options.persistenceImport}';`);
1783
+ }
1784
+ for (const quota of options.quotaProviders) {
1785
+ lines.push(`import { ${quota.className} } from '${quota.importPath}';`);
1786
+ }
1787
+ return lines.join("\n");
1788
+ }
1789
+ __name(renderImports, "renderImports");
1790
+ function addImportStatements(source, options) {
1791
+ const lines = source.split("\n");
1792
+ const block = renderImports(options).split("\n");
1793
+ lines.splice(endOfLastImport(lines) + 1, 0, ...block);
1794
+ return lines.join("\n");
1795
+ }
1796
+ __name(addImportStatements, "addImportStatements");
1797
+ function endOfLastImport(lines) {
1798
+ let end = -1;
1799
+ let open = false;
1800
+ for (let index = 0; index < lines.length; index += 1) {
1801
+ const line = lines[index];
1802
+ if (!open && !/^\s*import\s/.test(line)) continue;
1803
+ open = true;
1804
+ if (/\bfrom\s+['"][^'"]+['"]/.test(line) || /^\s*import\s+['"][^'"]+['"]/.test(line)) {
1805
+ end = index;
1806
+ open = false;
1807
+ }
1808
+ }
1809
+ return end;
1810
+ }
1811
+ __name(endOfLastImport, "endOfLastImport");
1812
+ function renderForRootBlock(options) {
1813
+ const quotaList = options.quotaProviders.map((q) => q.className).join(", ");
1814
+ return [
1815
+ "SaaSiCatModule.forRoot(",
1816
+ " defineSaaSiCat({",
1817
+ " // Plans straight from the YAML. Apps that manage plans in the",
1818
+ " // SuperAdmin UI pass `dbCatalog` instead.",
1819
+ " planCatalog: loadPlanCatalogFromFile({ path: 'config/saas.yaml' }),",
1820
+ " // Your authentication guard. This does NOT compile until you",
1821
+ " // name one, and that is deliberate: an empty array is how the",
1822
+ " // platform is told an endpoint should be auth-free, so a",
1823
+ " // placeholder `[]` here would publish GET /admin/discovery \u2014",
1824
+ " // your whole capability inventory \u2014 and the manifest routes to",
1825
+ " // anyone who asks. Import your guard and put it in.",
1826
+ " controller: { guards: [YourAuthGuard] },",
1827
+ options.persistenceImport ? " persistence," : " // persistence: prismaPersistence({ client: PrismaService }),",
1828
+ " catalog: { featureUiRegistry: " + options.registry.constName + " },",
1829
+ " adminResources: true,",
1830
+ " promoCodes: true,",
1831
+ quotaList ? ` quotaProviders: [${quotaList}],` : " quotaProviders: [],",
1832
+ " }),",
1833
+ ")"
1834
+ ].join("\n");
1835
+ }
1836
+ __name(renderForRootBlock, "renderForRootBlock");
1837
+ var LIMIT_FILTER_PROVIDER = "{ provide: APP_FILTER, useClass: LimitExceededFilter }";
1838
+ var LIMIT_FILTER_IMPORTS = [
1839
+ "import { APP_FILTER } from '@nestjs/core';",
1840
+ "import { LimitExceededFilter } from '@saasicat/nest/billing';"
1841
+ ].join("\n");
1842
+
1353
1843
  // src/module.ts
1354
1844
  import { Module } from "@nestjs/common";
1355
1845
  import { asProvider } from "@saasicat/nest";
@@ -2411,6 +2901,7 @@ export {
2411
2901
  AuditTailCommand,
2412
2902
  AuditTailFlow,
2413
2903
  CLI_CONTEXT_CONFIG_TOKEN,
2904
+ CONSTRAINTS_MARKER,
2414
2905
  CliContextModule,
2415
2906
  CliContextService,
2416
2907
  CliError,
@@ -2421,6 +2912,8 @@ export {
2421
2912
  DiscoverySnapshotDoctorCheck,
2422
2913
  DoctorCommands,
2423
2914
  DoctorFlow,
2915
+ LIMIT_FILTER_IMPORTS,
2916
+ LIMIT_FILTER_PROVIDER,
2424
2917
  MANIFEST_ACCESS_PORT_TOKEN,
2425
2918
  MANIFEST_CHECKS_TOKEN,
2426
2919
  ManifestCheckCommand,
@@ -2437,19 +2930,44 @@ export {
2437
2930
  UserCommands,
2438
2931
  UserPortDoctorCheck,
2439
2932
  WhoAmIFlow,
2933
+ appendConstraints,
2440
2934
  applyFragmentBlocks,
2935
+ applyTokens,
2936
+ assertModelsExist,
2937
+ assertValidProjectKey,
2938
+ assertValidQuotaKey,
2441
2939
  blankStringLiterals,
2442
2940
  blockBodyLines,
2443
2941
  breaksContract,
2444
2942
  checkSchema,
2943
+ constraintsFor,
2944
+ enableFkPointers,
2445
2945
  extractBlockNames,
2446
2946
  extractBlocks,
2447
2947
  extractModelBlocks,
2448
2948
  extractModelNames,
2949
+ findFkPointers,
2950
+ foreignKeyOf,
2951
+ hasBackRelation,
2952
+ hasConstraints,
2953
+ isOneToOne,
2954
+ kebabCase,
2955
+ migrationCreatedBy,
2956
+ minimumQuotasPerPlan,
2449
2957
  parseBlockAttributes,
2450
2958
  parseEnumValues,
2451
2959
  parseFields,
2960
+ parseQuota,
2452
2961
  parseSchema,
2962
+ pascalCase,
2963
+ patchAppModule,
2964
+ patchOptionsFor,
2965
+ planInit,
2966
+ projectKeyPattern,
2967
+ quotaKeyPattern,
2968
+ relationNameOf,
2969
+ reportConstraints,
2453
2970
  stripLineComment,
2454
- structuralOnly
2971
+ structuralOnly,
2972
+ tablesAddressedBy
2455
2973
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saasicat/cli",
3
- "version": "0.26.1",
3
+ "version": "0.27.0",
4
4
  "description": "CLI helpers for SaaS platform consumers. Provides CliContextService (identity, MFA, production confirm, audit tag).",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -20,16 +20,17 @@
20
20
  },
21
21
  "files": [
22
22
  "dist",
23
- "bin"
23
+ "bin",
24
+ "templates"
24
25
  ],
25
26
  "bin": {
26
27
  "saasicat": "./bin/saasicat.js"
27
28
  },
28
29
  "dependencies": {
29
30
  "qrcode-terminal": "^0.12.0",
30
- "@saasicat/nest": "^0.26.1",
31
- "@saasicat/spec": "^0.26.1",
32
- "@saasicat/types": "^0.26.1"
31
+ "@saasicat/nest": "^0.27.0",
32
+ "@saasicat/spec": "^0.27.0",
33
+ "@saasicat/types": "^0.27.0"
33
34
  },
34
35
  "peerDependencies": {
35
36
  "@nestjs/common": "^11.0.0",
@@ -42,7 +43,7 @@
42
43
  "tsup": "^8.0.0",
43
44
  "typescript": "^6.0.0"
44
45
  },
45
- "license": "Apache-2.0",
46
+ "license": "PolyForm-Shield-1.0.0",
46
47
  "author": "Taci Uelker",
47
48
  "homepage": "https://github.com/uelker70/saasicat",
48
49
  "repository": {