@empire-builder-kit/nx 1.0.0-rc.45 → 1.0.0-rc.47

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 (187) hide show
  1. package/generators.json +11 -1
  2. package/package.json +3 -3
  3. package/src/aws-authority/stage-authority.d.ts +30 -2
  4. package/src/aws-authority/stage-authority.js +220 -25
  5. package/src/developer-agent/generator-planning.js +109 -9
  6. package/src/developer-agent/model.d.ts +7 -3
  7. package/src/developer-agent/model.js +1 -0
  8. package/src/developer-agent/operation-registry.d.ts +1 -0
  9. package/src/developer-agent/operation-registry.js +44 -3
  10. package/src/developer-agent/planning.js +5 -2
  11. package/src/developer-agent/preview-changes.js +10 -0
  12. package/src/developer-agent/validation.js +7 -3
  13. package/src/developer-agent/workspace-inspection.js +51 -4
  14. package/src/executors/deployment-plan/deployment-plan.js +3 -0
  15. package/src/executors/dev-doctor/dev-doctor.d.ts +25 -3
  16. package/src/executors/dev-doctor/dev-doctor.js +136 -49
  17. package/src/executors/dev-setup/dev-setup.d.ts +18 -1
  18. package/src/executors/dev-setup/dev-setup.js +320 -196
  19. package/src/executors/foundation-preflight/foundation-preflight.js +9 -1
  20. package/src/executors/foundation-remove-preflight/foundation-remove-preflight.js +2 -2
  21. package/src/executors/github-bootstrap/github-bootstrap.d.ts +3 -0
  22. package/src/executors/github-bootstrap/github-bootstrap.js +28 -0
  23. package/src/executors/github-bootstrap/schema.json +14 -0
  24. package/src/executors/infrastructure-plan/infrastructure-plan.js +10 -3
  25. package/src/executors/local-deploy/local-deploy.d.ts +15 -0
  26. package/src/executors/local-deploy/local-deploy.js +96 -14
  27. package/src/executors/sst-lifecycle/logging-preflight.js +12 -1
  28. package/src/executors/sst-lifecycle/run-sst-command.js +51 -5
  29. package/src/foundation/cost-estimate.js +1 -1
  30. package/src/foundation/identity-domain-parent-ready.d.ts +91 -0
  31. package/src/foundation/identity-domain-parent-ready.js +333 -0
  32. package/src/foundation/identity-domain.d.ts +94 -0
  33. package/src/foundation/identity-domain.js +115 -0
  34. package/src/foundation/index.d.ts +2 -0
  35. package/src/foundation/index.js +7 -0
  36. package/src/foundation/preflight.js +17 -1
  37. package/src/foundation/root-site-readiness.d.ts +27 -0
  38. package/src/foundation/root-site-readiness.js +78 -0
  39. package/src/foundation/root-site.d.ts +81 -0
  40. package/src/foundation/root-site.js +268 -0
  41. package/src/generators/framework-prerequisites.d.ts +2 -0
  42. package/src/generators/framework-prerequisites.js +34 -0
  43. package/src/generators/function/function-registration.d.ts +24 -0
  44. package/src/generators/function/function-registration.js +66 -0
  45. package/src/generators/function/function.d.ts +16 -1
  46. package/src/generators/function/function.js +101 -18
  47. package/src/generators/function/schema.d.ts +1 -0
  48. package/src/generators/function/schema.json +6 -0
  49. package/src/generators/function-access/function-access.d.ts +14 -0
  50. package/src/generators/function-access/function-access.js +141 -0
  51. package/src/generators/function-access/schema.d.ts +6 -0
  52. package/src/generators/function-access/schema.json +25 -0
  53. package/src/generators/post-generation-install.d.ts +1 -1
  54. package/src/generators/post-generation-install.js +1 -1
  55. package/src/generators/preset/dependency-overrides.d.ts +1 -1
  56. package/src/generators/preset/dependency-overrides.js +1 -1
  57. package/src/generators/preset/workspace-files-adopter-skills.js +14 -0
  58. package/src/generators/preset/workspace-files-developer-agent.js +17 -11
  59. package/src/generators/preset/workspace-files-stage-authority.js +58 -1
  60. package/src/generators/preset/workspace-files-thin-deploy.d.ts +11 -0
  61. package/src/generators/preset/workspace-files-thin-deploy.js +52 -5
  62. package/src/generators/preset/workspace-files-thin-docs.js +33 -5
  63. package/src/generators/preset/workspace-files.js +238 -41
  64. package/src/generators/repository-policy/repository-policy.js +2 -1
  65. package/src/generators/repository-policy/schema.d.ts +2 -0
  66. package/src/generators/repository-policy/schema.json +7 -2
  67. package/src/generators/site/built-in-site-documentation-files.d.ts +2 -0
  68. package/src/generators/site/built-in-site-documentation-files.js +92 -0
  69. package/src/generators/site/built-in-site-files.d.ts +8 -0
  70. package/src/generators/site/built-in-site-files.js +40 -0
  71. package/src/generators/site/built-in-site-infrastructure-files.d.ts +2 -0
  72. package/src/generators/site/built-in-site-infrastructure-files.js +275 -0
  73. package/src/generators/site/built-in-site-plan.d.ts +19 -0
  74. package/src/generators/site/built-in-site-plan.js +65 -0
  75. package/src/generators/site/built-in-site-project-files.d.ts +3 -0
  76. package/src/generators/site/built-in-site-project-files.js +258 -0
  77. package/src/generators/site/built-in-site-script-files.d.ts +2 -0
  78. package/src/generators/site/built-in-site-script-files.js +306 -0
  79. package/src/generators/site/built-in-site-shared.d.ts +9 -0
  80. package/src/generators/site/built-in-site-shared.js +21 -0
  81. package/src/generators/site/built-in-site-smoke-files.d.ts +2 -0
  82. package/src/generators/site/built-in-site-smoke-files.js +37 -0
  83. package/src/generators/site/built-in-site-sst-config-files.d.ts +2 -0
  84. package/src/generators/site/built-in-site-sst-config-files.js +39 -0
  85. package/src/generators/site/built-in-site-web-files.d.ts +2 -0
  86. package/src/generators/site/built-in-site-web-files.js +284 -0
  87. package/src/generators/site/schema.d.ts +4 -0
  88. package/src/generators/site/schema.json +20 -0
  89. package/src/generators/site/site.d.ts +13 -0
  90. package/src/generators/site/site.js +221 -0
  91. package/src/generators/slice/built-in-plan-alarm-identity.d.ts +9 -0
  92. package/src/generators/slice/built-in-plan-alarm-identity.js +8 -1
  93. package/src/generators/slice/built-in-plan-api-contract-files.js +18 -0
  94. package/src/generators/slice/built-in-plan-async-work-files.js +18 -0
  95. package/src/generators/slice/built-in-plan-data-files.js +126 -14
  96. package/src/generators/slice/built-in-plan-foundation-files.d.ts +12 -0
  97. package/src/generators/slice/built-in-plan-foundation-files.js +114 -5
  98. package/src/generators/slice/built-in-plan-frontend-files.js +2 -271
  99. package/src/generators/slice/built-in-plan-frontend-header-proof.d.ts +6 -0
  100. package/src/generators/slice/built-in-plan-frontend-header-proof.js +282 -0
  101. package/src/generators/slice/built-in-plan-infrastructure-files.js +151 -237
  102. package/src/generators/slice/built-in-plan-key-value-cleanup.js +4 -3
  103. package/src/generators/slice/built-in-plan-log-group-helpers.d.ts +29 -0
  104. package/src/generators/slice/built-in-plan-log-group-helpers.js +130 -0
  105. package/src/generators/slice/built-in-plan-metadata-files.js +25 -5
  106. package/src/generators/slice/built-in-plan-next-build-authority.d.ts +5 -0
  107. package/src/generators/slice/built-in-plan-next-build-authority.js +124 -0
  108. package/src/generators/slice/built-in-plan-operations-files.d.ts +6 -0
  109. package/src/generators/slice/built-in-plan-operations-files.js +107 -219
  110. package/src/generators/slice/built-in-plan-performance-files.js +6 -1
  111. package/src/generators/slice/built-in-plan-release-files.js +0 -1
  112. package/src/generators/slice/built-in-plan-reliability-files.js +12 -8
  113. package/src/generators/slice/built-in-plan-shared.d.ts +1 -1
  114. package/src/generators/slice/built-in-plan-shared.js +1 -1
  115. package/src/generators/slice/built-in-plan-sst-config.d.ts +49 -0
  116. package/src/generators/slice/built-in-plan-sst-config.js +160 -0
  117. package/src/generators/slice/built-in-plan-testing-files.js +92 -49
  118. package/src/generators/slice/built-in-plan-verification-files.js +15 -5
  119. package/src/generators/slice/built-in-plan-web-files.js +69 -5
  120. package/src/generators/slice/built-in-plan.js +1 -2
  121. package/src/generators/slice/deployed-smoke-diagnostics.d.ts +35 -0
  122. package/src/generators/slice/deployed-smoke-diagnostics.js +189 -0
  123. package/src/generators/slice/deployed-smoke-readiness.d.ts +7 -0
  124. package/src/generators/slice/deployed-smoke-readiness.js +118 -23
  125. package/src/generators/slice/persistence-contract.js +2 -2
  126. package/src/generators/slice/pnpm-launch-script.d.ts +13 -0
  127. package/src/generators/slice/pnpm-launch-script.js +28 -0
  128. package/src/generators/slice/slice.js +27 -0
  129. package/src/generators/upgrade/custom-domain-notice.d.ts +19 -0
  130. package/src/generators/upgrade/custom-domain-notice.js +38 -0
  131. package/src/generators/upgrade/external-blueprint-compositions.d.ts +84 -0
  132. package/src/generators/upgrade/external-blueprint-compositions.js +478 -0
  133. package/src/generators/upgrade/framework-package-upgrade.d.ts +58 -0
  134. package/src/generators/upgrade/framework-package-upgrade.js +78 -0
  135. package/src/generators/upgrade/function-authentication-notice.d.ts +29 -0
  136. package/src/generators/upgrade/function-authentication-notice.js +74 -0
  137. package/src/generators/upgrade/schema.json +2 -2
  138. package/src/generators/upgrade/upgrade-plan.d.ts +52 -0
  139. package/src/generators/upgrade/upgrade-plan.js +406 -0
  140. package/src/generators/upgrade/upgrade.d.ts +1 -35
  141. package/src/generators/upgrade/upgrade.js +347 -595
  142. package/src/generators/workload/reliability-registration.d.ts +1 -1
  143. package/src/generators/workload/reliability-registration.js +9 -9
  144. package/src/generators/workload/workload.d.ts +1 -1
  145. package/src/generators/workload/workload.js +87 -13
  146. package/src/git-policy/github-bootstrap.d.ts +24 -5
  147. package/src/git-policy/github-bootstrap.js +105 -10
  148. package/src/local-dev/fleet-process.d.ts +9 -2
  149. package/src/local-dev/fleet-process.js +27 -7
  150. package/src/local-dev/fleet-session.js +41 -4
  151. package/src/local-dev/fleet-supervisor-daemon.js +1 -5
  152. package/src/local-dev/fleet-supervisor.d.ts +1 -0
  153. package/src/local-dev/fleet-supervisor.js +34 -11
  154. package/src/local-dev/gateway-daemon.d.ts +1 -1
  155. package/src/local-dev/gateway-daemon.js +6 -4
  156. package/src/local-dev/portless.d.ts +5 -0
  157. package/src/local-dev/portless.js +5 -0
  158. package/src/local-dev/process-group.d.ts +10 -0
  159. package/src/local-dev/process-group.js +33 -0
  160. package/src/local-dev/tls.js +31 -2
  161. package/src/local-dev/trust-store.d.ts +42 -0
  162. package/src/local-dev/trust-store.js +87 -0
  163. package/src/ownership/composition.d.ts +7 -0
  164. package/src/ownership/composition.js +64 -56
  165. package/src/ownership/index.d.ts +1 -0
  166. package/src/ownership/index.js +1 -0
  167. package/src/ownership/record.d.ts +19 -0
  168. package/src/ownership/record.js +26 -2
  169. package/src/ownership/site-record.d.ts +18 -0
  170. package/src/ownership/site-record.js +105 -0
  171. package/src/repository-policy/deployment-plan.d.ts +3 -0
  172. package/src/repository-policy/deployment-plan.js +33 -4
  173. package/src/repository-policy/index.d.ts +1 -0
  174. package/src/repository-policy/index.js +1 -0
  175. package/src/repository-policy/policy.d.ts +13 -1
  176. package/src/repository-policy/policy.js +55 -1
  177. package/src/repository-policy/public-dns.d.ts +65 -0
  178. package/src/repository-policy/public-dns.js +168 -0
  179. package/src/schemas/generated-ownership-v2.json +6 -0
  180. package/src/schemas/repository-policy-v1.json +67 -1
  181. package/src/supply-chain/blueprint-trust-git.js +24 -0
  182. package/src/supply-chain/blueprint-verification-authority.js +42 -3
  183. package/src/supply-chain/generated-blueprint-trust-command.js +15 -0
  184. package/src/supply-chain/sbom.js +74 -12
  185. package/src/work-markers/work-markers.js +4 -1
  186. package/src/workspace-deployment/ownership.d.ts +15 -0
  187. package/src/workspace-deployment/ownership.js +19 -3
@@ -0,0 +1,84 @@
1
+ import type { Tree } from '@nx/devkit';
2
+ import { type GeneratedOwnershipRecord } from '../../ownership/index.js';
3
+ type ProductSliceOwnership = Pick<GeneratedOwnershipRecord, 'blueprint' | 'compositions' | 'paths' | 'slice'>;
4
+ export interface SliceCompositionEvidence {
5
+ /** Every Function the Slice records, keeps or loads, sorted. */
6
+ functions: string[];
7
+ /** Every Job the Slice records, keeps or loads, sorted. */
8
+ jobs: string[];
9
+ /** Every Service the Slice records, keeps or loads, sorted. */
10
+ services: string[];
11
+ /**
12
+ * infra/product.ts's Function registration region holds code, and
13
+ * `functions` names none.
14
+ */
15
+ unnamedFunctionRegistrations: boolean;
16
+ /**
17
+ * Its Job and Service registration region holds code, and `jobs` and
18
+ * `services` name none.
19
+ */
20
+ unnamedWorkloadRegistrations: boolean;
21
+ /**
22
+ * Both markers of each registration region of infra/product.ts whose
23
+ * markers are malformed, the Function region's first.
24
+ */
25
+ malformedRegistrationMarkers: string[];
26
+ /**
27
+ * Each of `infra/functions/`, `jobs/` and `services/`, in that order,
28
+ * that infra/product.ts references as a module itself, as in
29
+ * `import … from './functions'`.
30
+ */
31
+ loadedDirectories: string[];
32
+ }
33
+ /**
34
+ * The Functions, Jobs and Services a Product Slice has, from every place one
35
+ * can live, failing closed:
36
+ * - its recorded compositions;
37
+ * - each file of any kind at any depth under `infra/functions/`, or in a
38
+ * `jobs/<name>/` or `services/<name>/` directory (not its top-level build
39
+ * output), that the ownership record does not give the Blueprint as its
40
+ * origin, other than Markdown files and dependencies;
41
+ * - each module its `infra/product.ts` references under those directories,
42
+ * or one of those directories itself;
43
+ * - code in a registration region of its `infra/product.ts`;
44
+ * - and malformed markers of such a region, where it cannot tell what the
45
+ * region holds.
46
+ * `infra/product.ts` is read from its syntax tree, whatever its ownership
47
+ * mode, and nothing in it counts as the Blueprint's own. Its comments and
48
+ * strings are not code.
49
+ */
50
+ export declare function sliceCompositionEvidence(tree: Tree, root: string, ownership: Pick<GeneratedOwnershipRecord, 'compositions' | 'paths'>): SliceCompositionEvidence;
51
+ export interface ExternalBlueprintUpgradeRefusal {
52
+ /** The Slice, its Blueprint and what it has: the refusal's first line. */
53
+ reason: string;
54
+ /** The adopter's options: remove what it has, or keep the Slice. */
55
+ remedy: string;
56
+ }
57
+ /**
58
+ * Why the Slice upgrade refuses an external-Blueprint Product Slice that has
59
+ * a Function, a Job or a Service, and the adopter's options; undefined when
60
+ * it does not refuse the Slice.
61
+ */
62
+ export declare function externalBlueprintUpgradeRefusal(tree: Tree, root: string, ownership: ProductSliceOwnership): ExternalBlueprintUpgradeRefusal | undefined;
63
+ /**
64
+ * Refuses, before the Blueprint is resolved or its generator runs, an
65
+ * upgrade of an external-Blueprint Product Slice that has a Function, a Job
66
+ * or a Service. Only the Standard Blueprint's branch of the Slice upgrade
67
+ * carries their registrations in `infra/product.ts`; an external Blueprint
68
+ * renders that file itself, without them. The refusal fails closed: in
69
+ * `infra/product.ts`, the Blueprint's own registrations and modules count
70
+ * too.
71
+ */
72
+ export declare function refuseExternalBlueprintCompositions(tree: Tree, root: string, ownership: ProductSliceOwnership): void;
73
+ /**
74
+ * A generator's message that sends the adopter to the Slice upgrade first:
75
+ * `problem`, then `step`, by default `; upgrade the Slice first.`. On a
76
+ * Slice the upgrade refuses, that step would lead into the refusal, so the
77
+ * message gives `first`, what to do before anything else, if any, and then
78
+ * why the upgrade refuses the Slice and the refusal's options.
79
+ */
80
+ export declare function upgradeSliceFirst(tree: Tree, root: string, problem: string, { first, step, }?: {
81
+ first?: string;
82
+ step?: string;
83
+ }): string;
84
+ export {};
@@ -0,0 +1,478 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sliceCompositionEvidence = sliceCompositionEvidence;
4
+ exports.externalBlueprintUpgradeRefusal = externalBlueprintUpgradeRefusal;
5
+ exports.refuseExternalBlueprintCompositions = refuseExternalBlueprintCompositions;
6
+ exports.upgradeSliceFirst = upgradeSliceFirst;
7
+ const node_path_1 = require("node:path");
8
+ const ts = require("typescript");
9
+ const index_js_1 = require("../../blueprints/index.js");
10
+ const deterministic_order_js_1 = require("../../deterministic-order.js");
11
+ const index_js_2 = require("../../ownership/index.js");
12
+ const PRODUCT_PATH = 'infra/product.ts';
13
+ const FUNCTIONS_DIRECTORY = 'infra/functions';
14
+ // Where the workload generator writes each Job and Service.
15
+ const WORKLOAD_DIRECTORIES = { job: 'jobs', service: 'services' };
16
+ const registrationRegion = (name) => ({
17
+ markers: [`// <ebk:${name}:start>`, `// <ebk:${name}:end>`],
18
+ name,
19
+ });
20
+ const FUNCTION_REGISTRATIONS = registrationRegion('function-registrations');
21
+ // Job and Service registrations share this region.
22
+ const WORKLOAD_REGISTRATIONS = registrationRegion('workload-registrations');
23
+ // Never part of a Function, Job or Service: dependencies and Markdown files
24
+ // anywhere, and the build and coverage output at the top of a Job's or
25
+ // Service's directory, which git leaves on disk when the change that
26
+ // generated the Job or Service is reverted.
27
+ const DEPENDENCY_DIRECTORY = 'node_modules';
28
+ const DOCUMENTATION_FILE = /\.md$/u;
29
+ const BUILD_OUTPUT_DIRECTORIES = ['coverage', 'dist'];
30
+ const COMPOSITION_KINDS = ['function', 'job', 'service'];
31
+ const COMPOSITION_DIRECTORIES = {
32
+ function: FUNCTIONS_DIRECTORY,
33
+ ...WORKLOAD_DIRECTORIES,
34
+ };
35
+ /** A Function's name from its path under `infra/functions/`. */
36
+ function functionName(path) {
37
+ const { dir, name } = node_path_1.posix.parse(path);
38
+ return dir ? `${dir}/${name}` : name;
39
+ }
40
+ /**
41
+ * The Function, Job or Service of the Slice at `root` whose directory a
42
+ * module specifier of its infra/product.ts reaches, if any: a module under
43
+ * `infra/functions/`, `jobs/<name>/` or `services/<name>/`, however the
44
+ * relative path is spelled, or one of those three directories itself.
45
+ */
46
+ function compositionModule(root, specifier) {
47
+ if (specifier !== '.' &&
48
+ specifier !== '..' &&
49
+ !specifier.startsWith('./') &&
50
+ !specifier.startsWith('../')) {
51
+ return undefined;
52
+ }
53
+ const slice = node_path_1.posix.normalize(root);
54
+ // With or without a trailing slash, a directory is the same module.
55
+ const resolved = node_path_1.posix
56
+ .join(slice, node_path_1.posix.dirname(PRODUCT_PATH), specifier)
57
+ .replace(/\/+$/u, '');
58
+ if (!resolved.startsWith(`${slice}/`))
59
+ return undefined;
60
+ const path = resolved.slice(slice.length + 1);
61
+ if (path === FUNCTIONS_DIRECTORY)
62
+ return { kind: 'function' };
63
+ if (path.startsWith(`${FUNCTIONS_DIRECTORY}/`)) {
64
+ return {
65
+ kind: 'function',
66
+ name: functionName(path.slice(FUNCTIONS_DIRECTORY.length + 1)),
67
+ };
68
+ }
69
+ const [directory, name] = path.split('/');
70
+ for (const kind of ['job', 'service']) {
71
+ if (directory === WORKLOAD_DIRECTORIES[kind]) {
72
+ return name ? { kind, name } : { kind };
73
+ }
74
+ }
75
+ return undefined;
76
+ }
77
+ /** `expression` without the parentheses, assertions and `await` around it. */
78
+ function unwrapped(expression) {
79
+ let node = expression;
80
+ while (ts.isParenthesizedExpression(node) ||
81
+ ts.isAwaitExpression(node) ||
82
+ ts.isNonNullExpression(node) ||
83
+ ts.isAsExpression(node) ||
84
+ ts.isSatisfiesExpression(node) ||
85
+ ts.isTypeAssertionExpression(node)) {
86
+ node = node.expression;
87
+ }
88
+ return node;
89
+ }
90
+ /** `createRequire`, or a property of that name, as in `module.createRequire`. */
91
+ function isCreateRequire(expression) {
92
+ const target = unwrapped(expression);
93
+ return ts.isIdentifier(target)
94
+ ? target.text === 'createRequire'
95
+ : ts.isPropertyAccessExpression(target) &&
96
+ target.name.text === 'createRequire';
97
+ }
98
+ /**
99
+ * The module a call loads with a string argument, if it loads one:
100
+ * `import('…')`, `require('…')`, or a require function from
101
+ * `createRequire(…)`, called directly as in `createRequire(…)('…')` or
102
+ * through a name declared as one. The string may be in parentheses, or
103
+ * asserted with `as`, `satisfies`, `!` or `<…>`.
104
+ */
105
+ function loadedModule(call, requires) {
106
+ const loader = unwrapped(call.expression);
107
+ const loads = loader.kind === ts.SyntaxKind.ImportKeyword ||
108
+ (ts.isIdentifier(loader) && requires.has(loader.text)) ||
109
+ (ts.isCallExpression(loader) && isCreateRequire(loader.expression));
110
+ const [argument] = call.arguments;
111
+ const literal = argument && unwrapped(argument);
112
+ return loads && literal && ts.isStringLiteralLike(literal)
113
+ ? literal.text
114
+ : undefined;
115
+ }
116
+ /**
117
+ * Every module a parsed infra/product.ts references, anywhere in it, from
118
+ * its syntax tree: import declarations, type-only and side-effect ones
119
+ * included, export declarations, `import … = require(…)`, `import(…)` types,
120
+ * and calls of `import(…)`, `require(…)` or a require function from
121
+ * `createRequire(…)` with a string argument. A string, a template or a
122
+ * comment that holds a module path is not one of them.
123
+ */
124
+ function moduleReferences(file) {
125
+ // `require`, each name declared as a require function from
126
+ // `createRequire(…)`, and each name declared as one of these.
127
+ const requires = new Set(['require']);
128
+ const findRequires = (node) => {
129
+ if (ts.isVariableDeclaration(node) &&
130
+ ts.isIdentifier(node.name) &&
131
+ node.initializer) {
132
+ const value = unwrapped(node.initializer);
133
+ if ((ts.isCallExpression(value) && isCreateRequire(value.expression)) ||
134
+ (ts.isIdentifier(value) && requires.has(value.text))) {
135
+ requires.add(node.name.text);
136
+ }
137
+ }
138
+ ts.forEachChild(node, findRequires);
139
+ };
140
+ findRequires(file);
141
+ const specifiers = [];
142
+ const visit = (node) => {
143
+ if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
144
+ node.moduleSpecifier &&
145
+ ts.isStringLiteral(node.moduleSpecifier)) {
146
+ specifiers.push(node.moduleSpecifier.text);
147
+ }
148
+ else if (ts.isImportEqualsDeclaration(node) &&
149
+ ts.isExternalModuleReference(node.moduleReference) &&
150
+ ts.isStringLiteral(node.moduleReference.expression)) {
151
+ specifiers.push(node.moduleReference.expression.text);
152
+ }
153
+ else if (ts.isImportTypeNode(node) &&
154
+ ts.isLiteralTypeNode(node.argument) &&
155
+ ts.isStringLiteral(node.argument.literal)) {
156
+ specifiers.push(node.argument.literal.text);
157
+ }
158
+ else if (ts.isCallExpression(node)) {
159
+ const specifier = loadedModule(node, requires);
160
+ if (specifier !== undefined)
161
+ specifiers.push(specifier);
162
+ }
163
+ ts.forEachChild(node, visit);
164
+ };
165
+ visit(file);
166
+ return specifiers;
167
+ }
168
+ /** The comments of a parsed file and where its code is. */
169
+ function fileTrivia(file) {
170
+ const comments = new Map();
171
+ const tokens = [];
172
+ const add = (ranges) => {
173
+ for (const range of ranges ?? [])
174
+ comments.set(range.pos, range);
175
+ };
176
+ const visit = (node) => {
177
+ add(ts.getLeadingCommentRanges(file.text, node.pos));
178
+ add(ts.getTrailingCommentRanges(file.text, node.end));
179
+ const children = node.getChildren(file);
180
+ if (children.length === 0 &&
181
+ node.kind <= ts.SyntaxKind.LastToken &&
182
+ node.kind !== ts.SyntaxKind.EndOfFileToken) {
183
+ tokens.push(node.getStart(file));
184
+ }
185
+ for (const child of children) {
186
+ // A JSDoc comment is trivia already found; its parts are no tokens.
187
+ if (child.kind !== ts.SyntaxKind.JSDoc)
188
+ visit(child);
189
+ }
190
+ };
191
+ visit(file);
192
+ return {
193
+ comments: [...comments.values()].sort((a, b) => a.pos - b.pos),
194
+ tokens,
195
+ };
196
+ }
197
+ /**
198
+ * Whether a comment's text holds a region's name, as each of its markers
199
+ * does: ignoring case, whitespace and invisible format characters.
200
+ */
201
+ function holdsRegionName(text, { name }) {
202
+ return text
203
+ .normalize('NFKC')
204
+ .replace(/[\s\p{Cf}]/gu, '')
205
+ .toLowerCase()
206
+ .includes(name);
207
+ }
208
+ /**
209
+ * What a registration region of a parsed file holds. A marker is a comment
210
+ * whose text is the marker exactly; a marker in a string or a template is
211
+ * none. The region holds code if a token is between a start marker and the
212
+ * next end marker. Its markers are malformed, failing closed, if a comment
213
+ * holds the region's name without being one of its markers, if an end marker
214
+ * has no start marker before it, or if a start marker has no end marker
215
+ * after it before the next start marker or the end of the file. A file with
216
+ * neither marker has no such region.
217
+ */
218
+ function readRegion(file, { comments, tokens }, region) {
219
+ const [startMarker, endMarker] = region.markers;
220
+ const reading = { holdsCode: false, malformed: false };
221
+ let open;
222
+ for (const comment of comments) {
223
+ const text = file.text.slice(comment.pos, comment.end).trim();
224
+ if (text === startMarker) {
225
+ if (open === undefined)
226
+ open = comment.end;
227
+ else
228
+ reading.malformed = true;
229
+ }
230
+ else if (text === endMarker) {
231
+ if (open === undefined) {
232
+ reading.malformed = true;
233
+ continue;
234
+ }
235
+ const [start, end] = [open, comment.pos];
236
+ if (tokens.some((token) => token >= start && token < end)) {
237
+ reading.holdsCode = true;
238
+ }
239
+ open = undefined;
240
+ }
241
+ else if (holdsRegionName(text, region)) {
242
+ reading.malformed = true;
243
+ }
244
+ }
245
+ if (open !== undefined)
246
+ reading.malformed = true;
247
+ return reading;
248
+ }
249
+ /**
250
+ * Each file of any kind under a directory of the Slice at `root`, at any
251
+ * depth, relative to `root`: not a Markdown file, not under
252
+ * `node_modules/`, nor under `skipped` directories at the top of it.
253
+ */
254
+ function countedFiles(tree, root, directory, skipped = []) {
255
+ return tree.children(node_path_1.posix.join(root, directory)).flatMap((entry) => {
256
+ if (entry === DEPENDENCY_DIRECTORY || skipped.includes(entry))
257
+ return [];
258
+ const path = node_path_1.posix.join(directory, entry);
259
+ if (!tree.isFile(node_path_1.posix.join(root, path))) {
260
+ return countedFiles(tree, root, path);
261
+ }
262
+ return DOCUMENTATION_FILE.test(entry) ? [] : [path];
263
+ });
264
+ }
265
+ /**
266
+ * The Functions, Jobs and Services a Product Slice has, from every place one
267
+ * can live, failing closed:
268
+ * - its recorded compositions;
269
+ * - each file of any kind at any depth under `infra/functions/`, or in a
270
+ * `jobs/<name>/` or `services/<name>/` directory (not its top-level build
271
+ * output), that the ownership record does not give the Blueprint as its
272
+ * origin, other than Markdown files and dependencies;
273
+ * - each module its `infra/product.ts` references under those directories,
274
+ * or one of those directories itself;
275
+ * - code in a registration region of its `infra/product.ts`;
276
+ * - and malformed markers of such a region, where it cannot tell what the
277
+ * region holds.
278
+ * `infra/product.ts` is read from its syntax tree, whatever its ownership
279
+ * mode, and nothing in it counts as the Blueprint's own. Its comments and
280
+ * strings are not code.
281
+ */
282
+ function sliceCompositionEvidence(tree, root, ownership) {
283
+ const names = {
284
+ function: new Set(),
285
+ job: new Set(),
286
+ service: new Set(),
287
+ };
288
+ for (const { kind, name } of ownership.compositions)
289
+ names[kind].add(name);
290
+ const blueprintPaths = new Set(ownership.paths
291
+ .filter(({ origin }) => origin === 'blueprint')
292
+ .map(({ path }) => path));
293
+ for (const path of countedFiles(tree, root, FUNCTIONS_DIRECTORY)) {
294
+ if (!blueprintPaths.has(path)) {
295
+ names.function.add(functionName(path.slice(FUNCTIONS_DIRECTORY.length + 1)));
296
+ }
297
+ }
298
+ for (const kind of ['job', 'service']) {
299
+ const directory = WORKLOAD_DIRECTORIES[kind];
300
+ for (const entry of tree.children(node_path_1.posix.join(root, directory))) {
301
+ const path = node_path_1.posix.join(directory, entry);
302
+ if (!tree.isFile(node_path_1.posix.join(root, path)) &&
303
+ countedFiles(tree, root, path, BUILD_OUTPUT_DIRECTORIES).some((file) => !blueprintPaths.has(file))) {
304
+ names[kind].add(entry);
305
+ }
306
+ }
307
+ }
308
+ const product = ts.createSourceFile(PRODUCT_PATH, tree.read(node_path_1.posix.join(root, PRODUCT_PATH), 'utf8') ?? '', ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
309
+ const directories = new Set();
310
+ for (const specifier of moduleReferences(product)) {
311
+ const module = compositionModule(root, specifier);
312
+ if (module?.name !== undefined)
313
+ names[module.kind].add(module.name);
314
+ else if (module)
315
+ directories.add(module.kind);
316
+ }
317
+ const trivia = fileTrivia(product);
318
+ const functionRegion = readRegion(product, trivia, FUNCTION_REGISTRATIONS);
319
+ const workloadRegion = readRegion(product, trivia, WORKLOAD_REGISTRATIONS);
320
+ const sorted = (kind) => [...names[kind]].sort(deterministic_order_js_1.compareUtf16);
321
+ const evidence = {
322
+ functions: sorted('function'),
323
+ jobs: sorted('job'),
324
+ services: sorted('service'),
325
+ };
326
+ return {
327
+ ...evidence,
328
+ unnamedFunctionRegistrations: evidence.functions.length === 0 && functionRegion.holdsCode,
329
+ unnamedWorkloadRegistrations: evidence.jobs.length === 0 &&
330
+ evidence.services.length === 0 &&
331
+ workloadRegion.holdsCode,
332
+ malformedRegistrationMarkers: [
333
+ ...(functionRegion.malformed ? FUNCTION_REGISTRATIONS.markers : []),
334
+ ...(workloadRegion.malformed ? WORKLOAD_REGISTRATIONS.markers : []),
335
+ ],
336
+ loadedDirectories: COMPOSITION_KINDS.filter((kind) => directories.has(kind)).map((kind) => `${COMPOSITION_DIRECTORIES[kind]}/`),
337
+ };
338
+ }
339
+ // Keeping the Slice off the upgrade also keeps who may call its Functions.
340
+ const KEPT_FUNCTION_ACCESS = "A Slice kept on its current files keeps its Functions' current access, and a Function generated through RC.46 has no authorizer, so anyone who can reach its URL can call it.";
341
+ function joinWithAnd(items) {
342
+ return items.length < 2
343
+ ? items.join('')
344
+ : `${items.slice(0, -1).join(', ')} and ${items[items.length - 1]}`;
345
+ }
346
+ /**
347
+ * Why the Slice upgrade refuses an external-Blueprint Product Slice that has
348
+ * a Function, a Job or a Service, and the adopter's options; undefined when
349
+ * it does not refuse the Slice.
350
+ */
351
+ function externalBlueprintUpgradeRefusal(tree, root, ownership) {
352
+ if (ownership.blueprint.name === index_js_1.BUILTIN_BLUEPRINT_NAME)
353
+ return undefined;
354
+ const evidence = sliceCompositionEvidence(tree, root, ownership);
355
+ const has = [];
356
+ if (evidence.functions.length > 0) {
357
+ has.push(`Functions: ${evidence.functions.join(', ')}`);
358
+ }
359
+ else if (evidence.unnamedFunctionRegistrations) {
360
+ has.push("code in infra/product.ts's Function registration region");
361
+ }
362
+ if (evidence.jobs.length > 0) {
363
+ has.push(`Jobs: ${evidence.jobs.join(', ')}`);
364
+ }
365
+ if (evidence.services.length > 0) {
366
+ has.push(`Services: ${evidence.services.join(', ')}`);
367
+ }
368
+ const directories = evidence.loadedDirectories;
369
+ if (directories.length > 0) {
370
+ has.push(directories.length === 1
371
+ ? `a module reference in infra/product.ts to the directory ${directories.join('')}`
372
+ : `module references in infra/product.ts to the directories ${joinWithAnd(directories)}`);
373
+ }
374
+ if (evidence.unnamedWorkloadRegistrations) {
375
+ has.push("code in infra/product.ts's Job and Service registration region");
376
+ }
377
+ const loads = (kind) => directories.includes(`${COMPOSITION_DIRECTORIES[kind]}/`);
378
+ const kinds = {
379
+ function: evidence.functions.length > 0 ||
380
+ evidence.unnamedFunctionRegistrations ||
381
+ loads('function'),
382
+ job: evidence.jobs.length > 0 || loads('job'),
383
+ service: evidence.services.length > 0 || loads('service'),
384
+ };
385
+ const these = [
386
+ ...(kinds.function ? ['Functions'] : []),
387
+ ...(kinds.job ? ['Jobs'] : []),
388
+ ...(kinds.service ? ['Services'] : []),
389
+ ...(evidence.unnamedWorkloadRegistrations && !kinds.job && !kinds.service
390
+ ? ['Jobs or Services']
391
+ : []),
392
+ ];
393
+ const markers = evidence.malformedRegistrationMarkers;
394
+ if (markers.length > 0) {
395
+ has.push(`malformed registration region markers in infra/product.ts: ${joinWithAnd(markers)}`);
396
+ }
397
+ if (has.length === 0)
398
+ return undefined;
399
+ const steps = [
400
+ ...(these.length > 0
401
+ ? [
402
+ `remove these ${joinWithAnd(these)} from it, for example by reverting the changes that generated them`,
403
+ ]
404
+ : []),
405
+ ...(markers.length > 0
406
+ ? [
407
+ 'restore those markers, each as a comment that is exactly its text, with each start marker before its end marker',
408
+ ]
409
+ : []),
410
+ ];
411
+ // Malformed Function region markers can hide a Function's registration.
412
+ const mayHaveFunctions = these.includes('Functions') ||
413
+ markers.includes(FUNCTION_REGISTRATIONS.markers[0]);
414
+ return {
415
+ reason: `Product Slice ${ownership.slice} uses the external Blueprint ${ownership.blueprint.name}@${ownership.blueprint.version} and has ${has.join('; ')}.`,
416
+ remedy: [
417
+ `To upgrade this Slice, first ${steps.join(', and ')}. Otherwise keep this Slice on its current files and do not upgrade it.`,
418
+ ...(mayHaveFunctions ? [KEPT_FUNCTION_ACCESS] : []),
419
+ ].join(' '),
420
+ };
421
+ }
422
+ /**
423
+ * Refuses, before the Blueprint is resolved or its generator runs, an
424
+ * upgrade of an external-Blueprint Product Slice that has a Function, a Job
425
+ * or a Service. Only the Standard Blueprint's branch of the Slice upgrade
426
+ * carries their registrations in `infra/product.ts`; an external Blueprint
427
+ * renders that file itself, without them. The refusal fails closed: in
428
+ * `infra/product.ts`, the Blueprint's own registrations and modules count
429
+ * too.
430
+ */
431
+ function refuseExternalBlueprintCompositions(tree, root, ownership) {
432
+ const refusal = externalBlueprintUpgradeRefusal(tree, root, ownership);
433
+ if (refusal === undefined)
434
+ return;
435
+ throw new Error([
436
+ refusal.reason,
437
+ 'The Slice upgrade carries Function, Job and Service registrations in infra/product.ts only for the Standard Blueprint. On this Slice it would drop their imports and registrations from a Blueprint-managed infra/product.ts, so the next deploy would remove them, and it would not update their registrations in a seeded or adopter-created infra/product.ts, which can then fail the next deploy.',
438
+ "In infra/product.ts the refusal counts the Blueprint's own as well: each module it loads from infra/functions/, jobs/ or services/, any code in a registration region, and any malformed marker of one. So it also refuses a Blueprint that renders its own registrations there.",
439
+ `No files were changed. ${refusal.remedy}`,
440
+ ].join('\n'));
441
+ }
442
+ /**
443
+ * On a Slice the upgrade refuses, why it does and the refusal's options.
444
+ * Undefined when the upgrade would not refuse the Slice or its ownership
445
+ * record cannot be read; the upgrade's own checks then speak for it.
446
+ */
447
+ function sliceUpgradeRefusalNotice(tree, root) {
448
+ const source = tree.read(node_path_1.posix.join(root, '.ebk/ownership.json'), 'utf8');
449
+ if (source === null)
450
+ return undefined;
451
+ let parsed;
452
+ try {
453
+ parsed = JSON.parse(source);
454
+ }
455
+ catch {
456
+ return undefined;
457
+ }
458
+ const ownership = (0, index_js_2.validateGeneratedOwnershipRecord)(parsed);
459
+ if (!ownership.valid)
460
+ return undefined;
461
+ const refusal = externalBlueprintUpgradeRefusal(tree, root, ownership.value);
462
+ return (refusal &&
463
+ `The Slice upgrade refuses this Slice: ${refusal.reason} ${refusal.remedy}`);
464
+ }
465
+ /**
466
+ * A generator's message that sends the adopter to the Slice upgrade first:
467
+ * `problem`, then `step`, by default `; upgrade the Slice first.`. On a
468
+ * Slice the upgrade refuses, that step would lead into the refusal, so the
469
+ * message gives `first`, what to do before anything else, if any, and then
470
+ * why the upgrade refuses the Slice and the refusal's options.
471
+ */
472
+ function upgradeSliceFirst(tree, root, problem, { first, step = '; upgrade the Slice first.', } = {}) {
473
+ const refused = sliceUpgradeRefusalNotice(tree, root);
474
+ if (refused === undefined)
475
+ return `${problem}${step}`;
476
+ return [`${problem}.`, ...(first ? [first] : []), refused].join(' ');
477
+ }
478
+ //# sourceMappingURL=external-blueprint-compositions.js.map
@@ -0,0 +1,58 @@
1
+ import type { GeneratorCallback, Tree } from '@nx/devkit';
2
+ import type { GeneratedPathOwnership } from '../../ownership/index.js';
3
+ import type { ValidationResult } from '../../validation.js';
4
+ import type { UpgradeGeneratorSchema } from './schema.js';
5
+ import { type UpgradeReport } from './upgrade-plan.js';
6
+ interface FrameworkPackageOwnership {
7
+ frameworkVersion: string;
8
+ paths: GeneratedPathOwnership[];
9
+ }
10
+ export interface FrameworkPackageTarget<Ownership extends FrameworkPackageOwnership> {
11
+ files: Readonly<Record<string, string>>;
12
+ ownership: Ownership;
13
+ }
14
+ /** An adopter-owned seeded file the upgrade extends by appending only. */
15
+ export interface SeededAppend {
16
+ contents: string;
17
+ path: string;
18
+ }
19
+ /**
20
+ * One framework-owned package: the workspace root, the Standard Foundation or
21
+ * the root site. Each hook carries one real difference between them; a
22
+ * package that leaves a hook out does not have that step.
23
+ */
24
+ export interface FrameworkPackageUpgrade<Ownership extends FrameworkPackageOwnership> {
25
+ /** Names the package in refusals, for example `Standard Foundation`. */
26
+ label: string;
27
+ /** Names the package in the report line, for example `foundation`. */
28
+ reportName: string;
29
+ /** The package directory; '' is the workspace root. */
30
+ root: string;
31
+ /** Names the package in the refusal for a path the target did not render. */
32
+ targetLabel: string;
33
+ /** Refuses an upgrade that would rewrite an existing seeded migration. */
34
+ refusesSeededMigrationRewrites: boolean;
35
+ /** Checks the workspace prerequisites after the option refusal. */
36
+ requirePrerequisites?: () => void;
37
+ validateOwnership: (value: unknown) => ValidationResult<Ownership>;
38
+ /**
39
+ * Renders the installed framework's target after the recorded ownership
40
+ * validates; package preconditions run here, before rendering.
41
+ */
42
+ render: () => FrameworkPackageTarget<Ownership>;
43
+ /** Refuses an invalid rendered target record. */
44
+ validateTarget?: (ownership: Ownership) => void;
45
+ /** Plans an append to a seeded file once the target is merged. */
46
+ planSeededAppend?: (currentFiles: ReadonlyMap<string, string | null>, targetFiles: Readonly<Record<string, string>>) => SeededAppend | null;
47
+ /** Shapes the logged report; defaults to the report itself. */
48
+ describeReport?: (report: UpgradeReport) => unknown;
49
+ /** Runs after the report is logged, in report and apply modes alike. */
50
+ afterReport?: (currentFiles: ReadonlyMap<string, string | null>) => void;
51
+ restamp: (tree: Tree) => void;
52
+ /** Returns the callback that runs after an applied upgrade. */
53
+ installCallback?: () => GeneratorCallback | undefined;
54
+ }
55
+ export declare function upgradeFrameworkPackage<Ownership extends FrameworkPackageOwnership>(tree: Tree, options: UpgradeGeneratorSchema, output: {
56
+ info(message: string): void;
57
+ }, upgrade: FrameworkPackageUpgrade<Ownership>): Promise<GeneratorCallback | void>;
58
+ export {};