@sincpro/client-framework 2.7.0 → 2.9.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.
@@ -5,7 +5,7 @@
5
5
  * the runtime imports it (`make doctor`), and the package's root entry does not re-export it. `sincpro-client generate` (`cli.ts`) is the same, from a terminal.
6
6
  */
7
7
  export type { Bindings, Metamodel, Operation, RestBinding, Schema, } from "./domain/metamodel.js";
8
- export { CheckError, checkImports, type Place, placeOf, type Violation, violationOf, } from "./services/check.js";
8
+ export { CheckError, checkImports, type Place, placeOf, placeViolationOf, type Violation, violationOf, } from "./services/check.js";
9
9
  export { clientFileOf } from "./services/client.js";
10
10
  export { domainFilesOf } from "./services/domain.js";
11
11
  export { entitiesFileOf } from "./services/entities.js";
@@ -1,4 +1,4 @@
1
- export { CheckError, checkImports, placeOf, violationOf, } from "./services/check.js";
1
+ export { CheckError, checkImports, placeOf, placeViolationOf, violationOf, } from "./services/check.js";
2
2
  export { clientFileOf } from "./services/client.js";
3
3
  export { domainFilesOf } from "./services/domain.js";
4
4
  export { entitiesFileOf } from "./services/entities.js";
@@ -15,7 +15,13 @@
15
15
  * | components | composer, a router |
16
16
  * | common | any bounded context |
17
17
  * | any layer | another bounded context — but its `index.ts`, from `infrastructure/` |
18
- * | entrypoints/ root | a bounded context's insides — only its `index.ts` (and `common`) |
18
+ * | entrypoints/ root | a bounded context's insides — only its doors (and `common`) |
19
+ * | entrypoints/<face>| another face's door: `index.ts` or its own (`sales/admin.ts`) |
20
+ *
21
+ * A context's doors are the files in its root: `index.ts` — the bounded context itself — and one
22
+ * per face of the application (`admin.ts`, `sitio.ts`, `portal.ts`) that exports what that face
23
+ * draws of it. A face enters by its own door, so another face's screens never reach its bundle:
24
+ * the public site does not load the back office's.
19
25
  *
20
26
  * Context: the architecture says these rules are enforced, not recommended; a rule only read in a
21
27
  * page is the first to go under a deadline. Each import is resolved the way the application's
@@ -36,21 +42,33 @@ export type Place = {
36
42
  context: string;
37
43
  layer: string | null;
38
44
  index: boolean;
39
- } | {
45
+ /** A file in the context's root is one of its doors: `index`, `react`, or a face's (`admin`). */
46
+ door: string | null;
47
+ /** Inside its `entrypoints/<face>/`: that face. */
48
+ face?: string;
49
+ }
50
+ /** An entrypoint, and the face of the application it belongs to (`entrypoints/admin/…`). */
51
+ | {
40
52
  kind: "entrypoint";
53
+ face: string | null;
41
54
  } | {
42
55
  kind: "other";
43
56
  };
57
+ export declare function placeOf(path: string): Place;
44
58
  /**
45
- * `domains/sales/services/close.ts` → the bounded context `sales`, the layer `services` — wherever
46
- * the `domains/` folder is (`src/domains/…`, a workspace's `domains/sales/src/services/…`).
59
+ * Why a file may not be where it is, whatever it imports — said once per file, or nothing.
60
+ * `common` orchestrates nothing and nothing comes in through it: no services, no screens.
47
61
  */
48
- export declare function placeOf(path: string): Place;
62
+ export declare function placeViolationOf(from: Place): string | undefined;
49
63
  /**
50
64
  * Why `from` may not import `specifier`, which resolves to `to` (`null`: a package), or nothing
51
65
  * when it may.
52
66
  */
53
- export declare function violationOf(from: Place, specifier: string, to: Place | null): string | undefined;
67
+ export declare function violationOf(from: Place, specifier: string, to: Place | null,
68
+ /** Only types are imported (`import type`): they decide nothing at run time. */
69
+ how?: {
70
+ typeOnly?: boolean;
71
+ }): string | undefined;
54
72
  /** What stops the check before it starts: no root, no config, a config that does not parse. */
55
73
  export declare class CheckError extends Error {
56
74
  }
@@ -13,11 +13,16 @@ const LAYERS = new Set([
13
13
  * `domains/sales/services/close.ts` → the bounded context `sales`, the layer `services` — wherever
14
14
  * the `domains/` folder is (`src/domains/…`, a workspace's `domains/sales/src/services/…`).
15
15
  */
16
+ /** What a source file ends in — the checker reads every one, and names a door by what is before. */
17
+ const SOURCE = /\.(ts|tsx|mts|cts|js|jsx|mjs|cjs)$/;
16
18
  export function placeOf(path) {
17
19
  const parts = path.split(/[\\/]/).filter((one) => one !== "");
18
20
  const start = parts[0] === "src" ? 1 : 0;
19
21
  if (parts[start] === "entrypoints")
20
- return { kind: "entrypoint" };
22
+ return {
23
+ kind: "entrypoint",
24
+ face: parts.length > start + 2 ? (parts[start + 1] ?? null) : null,
25
+ };
21
26
  const at = parts.indexOf("domains");
22
27
  const context = at === -1 ? undefined : parts[at + 1];
23
28
  if (context === undefined || parts.length < at + 3)
@@ -26,8 +31,17 @@ export function placeOf(path) {
26
31
  const rest = parts.slice(at + 2);
27
32
  const inside = rest[0] === "src" && rest.length > 1 ? rest.slice(1) : rest;
28
33
  const first = inside[0] ?? "";
29
- const index = inside.length === 1 && /^index\.(ts|tsx|js|jsx|mts)$/.test(first);
30
- return { kind: "context", context, layer: LAYERS.has(first) ? first : null, index };
34
+ const door = inside.length === 1 ? first.replace(SOURCE, "") : null;
35
+ const index = door === "index";
36
+ const face = first === "entrypoints" && inside.length > 2 ? inside[1] : undefined;
37
+ return {
38
+ kind: "context",
39
+ context,
40
+ layer: LAYERS.has(first) ? first : null,
41
+ index,
42
+ door,
43
+ ...(face === undefined ? {} : { face }),
44
+ };
31
45
  }
32
46
  const REACT = /^(react|react-dom|react-native)(\/|$)/;
33
47
  const DRAWING = /^@sincpro\/(web-ui|web-composer)(\/|$)/;
@@ -43,19 +57,38 @@ const NEVER_OWN = {
43
57
  adapters: ["services", "components", "entrypoints"],
44
58
  services: ["components", "entrypoints"],
45
59
  infrastructure: ["components", "entrypoints"],
60
+ // A component is drawn by screens, never the other way: what a screen defines stays its own.
61
+ components: ["entrypoints"],
46
62
  };
63
+ /**
64
+ * Why a file may not be where it is, whatever it imports — said once per file, or nothing.
65
+ * `common` orchestrates nothing and nothing comes in through it: no services, no screens.
66
+ */
67
+ export function placeViolationOf(from) {
68
+ if (from.kind !== "context" || from.context !== "common")
69
+ return undefined;
70
+ if (from.layer === "entrypoints")
71
+ return "common holds no entrypoints: a screen is a bounded context's, a face's frame is entrypoints/<face>/";
72
+ if (from.layer === "services")
73
+ return "common holds no services: a use case is a bounded context's";
74
+ return undefined;
75
+ }
47
76
  /**
48
77
  * Why `from` may not import `specifier`, which resolves to `to` (`null`: a package), or nothing
49
78
  * when it may.
50
79
  */
51
- export function violationOf(from, specifier, to) {
80
+ export function violationOf(from, specifier, to,
81
+ /** Only types are imported (`import type`): they decide nothing at run time. */
82
+ how = {}) {
52
83
  if (to === null) {
53
84
  if (from.kind !== "context" || from.layer === null)
54
85
  return undefined;
55
86
  if (PLAIN.has(from.layer) &&
56
87
  (REACT.test(specifier) || DRAWING.test(specifier) || SCREENS.test(specifier)))
57
88
  return `${from.layer}/ is plain logic: it never imports a screen's library (React, web-ui, composer, web-framework but its core)`;
58
- if (from.layer === "components" && (COMPOSER.test(specifier) || ROUTER.test(specifier)))
89
+ if (from.layer === "components" &&
90
+ how.typeOnly !== true &&
91
+ (COMPOSER.test(specifier) || ROUTER.test(specifier)))
59
92
  return "a component never decides where things open: no composer, no router";
60
93
  return undefined;
61
94
  }
@@ -66,22 +99,44 @@ export function violationOf(from, specifier, to) {
66
99
  : "nothing imports the application's entrypoints";
67
100
  if (to.kind !== "context")
68
101
  return undefined;
69
- // Outside the bounded contexts (the entrypoints, `main.tsx`, a `lib/`): only their index.ts.
70
- if (from.kind !== "context")
71
- return to.context === "common" || to.index
72
- ? undefined
73
- : `outside a bounded context, it is used through its index.ts, never its insides («${to.context}»)`;
102
+ // Outside the bounded contexts (the entrypoints, `main.tsx`, a `lib/`): only their doors — its
103
+ // index.ts, or the door of the face it belongs to (`sales/admin.ts` for `entrypoints/admin/`).
104
+ // Another face's door would bring that face's screens into this face's bundle.
105
+ if (from.kind !== "context") {
106
+ if (to.context === "common" || to.index || to.door === "react")
107
+ return undefined;
108
+ if (to.door !== null) {
109
+ // A module that is neither a face nor a context (a helper, `main.tsx`) opens no face's door:
110
+ // a face that imported it would reach another face's screens through it.
111
+ if (from.kind !== "entrypoint")
112
+ return `a face's door (${to.door}) is opened only by the faces' entrypoints — a module outside them uses «${to.context}» through its index.ts`;
113
+ // The composition root (`entrypoints/app.tsx`, of no face) puts the faces together.
114
+ return from.face === null || to.door === from.face
115
+ ? undefined
116
+ : `the «${from.face}» face enters «${to.context}» by another face's door (${to.door}): by its index.ts or its ${from.face}.ts`;
117
+ }
118
+ return `outside a bounded context, it is used through its doors — its index.ts, or its face's — never its insides («${to.context}»)`;
119
+ }
74
120
  if (from.context === "common")
75
121
  return to.context === "common" ? undefined : "common never imports a bounded context";
76
122
  if (to.context === "common")
77
123
  return undefined;
124
+ // A door of the context: `index.ts` and `react.ts` never re-export a face — what a portal screen
125
+ // defines would reach the site through them —; a face's door re-exports its own face only.
126
+ if (to.context === from.context && from.door !== null && to.layer === "entrypoints") {
127
+ if (from.door === "index" || from.door === "react")
128
+ return `${from.door}.ts never re-exports a face: what it publishes lives in components/ (a face's screens go through its own door, <face>.ts)`;
129
+ if (to.face !== undefined && to.face !== from.door)
130
+ return `${from.door}.ts re-exports another face (${to.face}): a face's door holds its own face only`;
131
+ return undefined;
132
+ }
78
133
  if (to.context === from.context) {
79
134
  const never = from.layer === null ? undefined : NEVER_OWN[from.layer];
80
135
  return to.layer !== null && never?.includes(to.layer) === true
81
136
  ? `${from.layer}/ never imports its own ${to.layer}/`
82
137
  : undefined;
83
138
  }
84
- if (to.index && from.layer === "infrastructure")
139
+ if ((to.index || to.door === "react") && from.layer === "infrastructure")
85
140
  return undefined;
86
141
  return to.index
87
142
  ? `another bounded context («${to.context}») is a dependency: added in infrastructure/, never imported here`
@@ -95,7 +150,7 @@ function sources(root) {
95
150
  const path = join(root, entry.name);
96
151
  if (entry.isDirectory())
97
152
  found.push(...sources(path));
98
- else if (/\.(ts|tsx|mts|cts|js|jsx)$/.test(entry.name) && !entry.name.endsWith(".d.ts"))
153
+ else if (SOURCE.test(entry.name) && !entry.name.endsWith(".d.ts"))
99
154
  found.push(path);
100
155
  }
101
156
  return found;
@@ -154,7 +209,19 @@ export async function checkImports(root, options = {}) {
154
209
  for (const file of sources(root)) {
155
210
  const name = relative(root, file).split(sep).join("/");
156
211
  const from = placeOf(name);
157
- for (const { fileName: specifier } of ts.preProcessFile(readFileSync(file, "utf8"), true, true).importedFiles) {
212
+ const source = readFileSync(file, "utf8");
213
+ // What a file imports only as types (`import type { … } from "x"`): a type opens nothing.
214
+ const asValues = new Set([
215
+ ...source.matchAll(/^(?:import|export)\s+(?!type\b)[^;]*?from\s+["']([^"']+)["']/gms),
216
+ ].map((m) => m[1]));
217
+ const typeOnly = new Set([...source.matchAll(/^import type [^;]*?from\s+["']([^"']+)["']/gms)]
218
+ .map((m) => m[1])
219
+ .filter((specifier) => !asValues.has(specifier)));
220
+ const misplaced = placeViolationOf(from);
221
+ if (misplaced !== undefined)
222
+ violations.push({ file: name, imports: "", why: misplaced });
223
+ for (const { fileName: specifier } of ts.preProcessFile(source, true, true)
224
+ .importedFiles) {
158
225
  if (ASSET.test(specifier))
159
226
  continue;
160
227
  // The first config that finds it inside the application, else any that finds it.
@@ -186,7 +253,7 @@ export async function checkImports(root, options = {}) {
186
253
  // A bounded context that is a package, named whole (`@acme/ventas`): its index.
187
254
  if (to?.kind === "context" && BARE.test(specifier))
188
255
  to = { ...to, index: true };
189
- const why = violationOf(from, specifier, to);
256
+ const why = violationOf(from, specifier, to, { typeOnly: typeOnly.has(specifier) });
190
257
  if (why !== undefined)
191
258
  violations.push({ file: name, imports: specifier, why });
192
259
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/client-framework",
3
- "version": "2.7.0",
3
+ "version": "2.9.0",
4
4
  "description": "The client side of sincpro_framework, for every platform: Feature and ApplicationService traced and reported by default, applications made of domains, and the typed client generated from a service's contract. No React, no DOM.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",