@latticecms/cli 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +140 -2
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +12 -0
- package/dist/bin.js.map +1 -0
- package/dist/changes.d.ts +22 -0
- package/dist/changes.d.ts.map +1 -0
- package/dist/changes.js +118 -0
- package/dist/changes.js.map +1 -0
- package/dist/collection.d.ts +5 -0
- package/dist/collection.d.ts.map +1 -0
- package/dist/collection.js +166 -0
- package/dist/collection.js.map +1 -0
- package/dist/config.d.ts +37 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +111 -0
- package/dist/config.js.map +1 -0
- package/dist/diff.d.ts +6 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +83 -0
- package/dist/diff.js.map +1 -0
- package/dist/edit.d.ts +105 -0
- package/dist/edit.d.ts.map +1 -0
- package/dist/edit.js +571 -0
- package/dist/edit.js.map +1 -0
- package/dist/exit.d.ts +13 -0
- package/dist/exit.d.ts.map +1 -0
- package/dist/exit.js +19 -0
- package/dist/exit.js.map +1 -0
- package/dist/layout.d.ts +20 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +59 -0
- package/dist/layout.js.map +1 -0
- package/dist/main.d.ts +7 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +58 -0
- package/dist/main.js.map +1 -0
- package/dist/migrate.d.ts +9 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +120 -0
- package/dist/migrate.js.map +1 -0
- package/dist/migration.d.ts +5 -0
- package/dist/migration.d.ts.map +1 -0
- package/dist/migration.js +170 -0
- package/dist/migration.js.map +1 -0
- package/dist/names.d.ts +15 -0
- package/dist/names.d.ts.map +1 -0
- package/dist/names.js +32 -0
- package/dist/names.js.map +1 -0
- package/dist/templates.d.ts +21 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +73 -0
- package/dist/templates.js.map +1 -0
- package/dist/typescript.d.ts +9 -0
- package/dist/typescript.d.ts.map +1 -0
- package/dist/typescript.js +60 -0
- package/dist/typescript.js.map +1 -0
- package/package.json +72 -4
- package/src/bin.ts +12 -0
- package/src/changes.ts +170 -0
- package/src/collection.ts +228 -0
- package/src/config.ts +142 -0
- package/src/diff.ts +90 -0
- package/src/edit.ts +812 -0
- package/src/exit.ts +19 -0
- package/src/layout.ts +91 -0
- package/src/main.ts +71 -0
- package/src/migrate.ts +155 -0
- package/src/migration.ts +239 -0
- package/src/names.ts +48 -0
- package/src/templates.ts +104 -0
- package/src/typescript.ts +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Cory Robinson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,141 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @latticecms/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `lattice` command. It adds a collection to a Lattice app: the collection, a test, its registration in the app's composition, and its events in the app's events catalog. It adds a data migration to the app's list, and runs the pending ones as a deploy step. It edits the app's files where their shape gives one place for each edit, or writes nothing and prints the lines to add by hand.
|
|
4
|
+
|
|
5
|
+
Status: before 1.0, not yet published. The root README's [Getting started](../../README.md#3-collections) uses it on an empty app.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install --save-dev @latticecms/cli
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@latticecms/core` and `typescript` are peer dependencies; the app has both. The CLI parses the app's files with the app's own `typescript`, resolved from the app's directory: 5.8 to 6. TypeScript 7 is the native compiler and has no JavaScript API, so the CLI refuses it, and says to install `typescript@^6`. `@latticecms/store-mongo` and `@hexok/logger` are optional peers: `lattice migrate` imports them from the app, and the other commands run without them.
|
|
14
|
+
|
|
15
|
+
Run it from anywhere inside the app, with `npx lattice`. `lattice --help` lists the commands.
|
|
16
|
+
|
|
17
|
+
## Config
|
|
18
|
+
|
|
19
|
+
The CLI reads the `"lattice"` key of the nearest `package.json` at or above the current directory. Every key is optional. Paths are relative to that `package.json`, and default to the layout of [`examples/crm`](../../examples/crm):
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"lattice": {
|
|
24
|
+
"collections": "src/domain/collections",
|
|
25
|
+
"composition": "src/servers/rpc/contract.ts",
|
|
26
|
+
"events": "src/domain/events/domain-events.ts",
|
|
27
|
+
"migrations": "src/infra/migrations",
|
|
28
|
+
"migrate": "dist/servers/scripts/migrate.js",
|
|
29
|
+
"tenant": "organizationId"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
| Key | What it names | Default |
|
|
35
|
+
| --- | --- | --- |
|
|
36
|
+
| `collections` | The directory of collection files | `src/domain/collections` |
|
|
37
|
+
| `composition` | The file with the app's one `framework()` chain | `src/servers/rpc/contract.ts` |
|
|
38
|
+
| `events` | The file with the app's one `EventCatalog()` | `src/domain/events/domain-events.ts` |
|
|
39
|
+
| `migrations` | The directory of migrations, with the list in its `index.ts` | `src/infra/migrations` |
|
|
40
|
+
| `migrate` | The built module `lattice migrate` imports | `dist/servers/scripts/migrate.js` |
|
|
41
|
+
| `tenant` | The tenant field of a new collection | none |
|
|
42
|
+
|
|
43
|
+
`tenant` has no default: a guessed field name would tenant a collection by a field the app does not use. Without it, `lattice collection` needs `--tenant` or `--global`.
|
|
44
|
+
|
|
45
|
+
A bad config is a usage error that names the file and the key: invalid JSON, an unknown key, a value that is not a non-empty string, a reserved or invalid tenant field, a path that is absolute or leaves the app, or a path a command needs that is missing or is a file where a directory belongs, or the reverse. `migrate` names built output, so it may be missing until the app is built.
|
|
46
|
+
|
|
47
|
+
## `lattice collection`
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
lattice collection <slug> [--tenant <field> | --global] [--label <singular>,<plural>] [--dry-run]
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
$ npx lattice collection deal_notes
|
|
55
|
+
created src/domain/collections/deal-notes.ts
|
|
56
|
+
created src/domain/collections/deal-notes.test.ts
|
|
57
|
+
edited src/servers/rpc/contract.ts
|
|
58
|
+
edited src/domain/events/domain-events.ts
|
|
59
|
+
deal_notes is open to any signed-in caller. Tighten its access in src/servers/rpc/contract.ts.
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
- The slug is lower snake case and plural, such as `deal_notes`. The file is `deal-notes.ts`, the export `DealNotes`, and the events `dealNotesCreated`, `dealNotesUpdated` and `dealNotesDeleted`.
|
|
63
|
+
- The collection has one field, `name`, a title for lists and pickers. Edit its schema next.
|
|
64
|
+
- `--tenant <field>` tenants it by that field, over `"lattice.tenant"`. `--global` makes it global.
|
|
65
|
+
- `--label person,people` declares its label, for a slug whose singular core would get wrong. Without it, core's `labelOf(slug)` gives the label.
|
|
66
|
+
- The composition gets an import and `.collection(DealNotes, { access })` at the end of its chain, open to any signed-in caller, inside tenancy. It uses the file's `anyone` when it declares one, else `() => true`. Tighten it.
|
|
67
|
+
- The events catalog gets an import and the three events after the last collection's. The app's publisher takes only the catalog's events, so without them the app does not compile.
|
|
68
|
+
- The test checks what the scaffold made, and builds nothing: the composition's contract has the slug and its six operations (which also proves every ref resolves within the app), and the schema parses `{ name: 'First' }` and refuses `{}` at `name`. A ref, a relation, a method or an override anywhere in the app never breaks it. Test behavior through the app's own test setup.
|
|
69
|
+
- Each edit is inserted as text; the file is never reprinted. Imports go in Biome's order. Inserted lines use the file's line breaks (LF or CRLF) and its indent unit (a tab or its spaces), and go after any comment that ends the line before them.
|
|
70
|
+
- Generated lines break where Biome breaks them at its default width of 80, one level deep. A name long enough to need a second level is left to the app's formatter.
|
|
71
|
+
- `--dry-run` prints each change as a unified diff and writes nothing.
|
|
72
|
+
|
|
73
|
+
Every edit is computed before any file is written. The new texts are staged in files beside their targets. Each edited file must still hold the text that was parsed, and be writable, before any staged file takes its target's place. If a write fails, the staged files are removed and any file already placed is restored, so a failed run changes nothing.
|
|
74
|
+
|
|
75
|
+
The command refuses, writes nothing and exits 1 when:
|
|
76
|
+
|
|
77
|
+
- the slug is not lower snake case, or is one of core's `reservedNames`;
|
|
78
|
+
- the collection's file or test exists, or a file in the collections directory (`.ts`, `.tsx`, `.mts` or `.cts`, but tests and declarations) has the slug in core's `collection()`, by any name it imports it as, or in a relation's `through`;
|
|
79
|
+
- the composition or the catalog already imports or declares the export's name at the top level, or already registers it or lists its events;
|
|
80
|
+
- the field `--tenant` names is one of core's `reservedFields`, or is not a field name;
|
|
81
|
+
- the composition has no `framework()` chain, or more than one, or its chain is not an `export const` of `.collection()` calls; or the events file has no `EventCatalog()` with an object literal, or more than one.
|
|
82
|
+
|
|
83
|
+
For the last two, it also prints every change it would have made, and where each line goes, so the edit can be made by hand. Running the command again after it succeeded is refused: the collection's file exists.
|
|
84
|
+
|
|
85
|
+
## `lattice migration`
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
lattice migration <name> [--transaction] [--dry-run]
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
$ npx lattice migration add-notes-index
|
|
93
|
+
created src/infra/migrations/0002-add-notes-index.ts
|
|
94
|
+
edited src/infra/migrations/index.ts
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
- The name is kebab case. The file is `<id>-<name>.ts`, its id four digits, and its default export is a `Migration` for store-mongo's `migrate()`. Its comment says what `up({ db, session, signal, logger })` receives. Its `up` throws `Migration 2 add-notes-index is not written yet` until it is written, so a build that runs it early, or a server that migrates at startup, fails instead of recording an empty migration as done.
|
|
98
|
+
- The next id is one past the list's: the CLI reads each listed migration's `id` from its file's default export, not from file names.
|
|
99
|
+
- `index.ts` gets a default import, `m0002`, and the name at the end of its one exported array. A list on one line stays there while it fits in 80 columns, and is broken one per line when it does not. A list on several lines gets a line of its own. Comments, line breaks and indent are kept, as in `lattice collection`.
|
|
100
|
+
- `--transaction` writes `transaction: true`: `up` and its record commit together, which needs a replica set.
|
|
101
|
+
- `--dry-run` prints a diff and writes nothing. Writes are staged and placed as `lattice collection`'s are: all or none.
|
|
102
|
+
|
|
103
|
+
It refuses, writes nothing and exits 1 when the list has a gap or a duplicate id, which `migrate()` would refuse too (its error is named); when a listed migration's file has no numeric `id` and string `name`; when a migration of that name is listed or its file exists; or when `index.ts` has no exported array, or more than one. A name that is not kebab case is a usage error.
|
|
104
|
+
|
|
105
|
+
## `lattice migrate`
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
lattice migrate
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Runs the app's pending migrations, as a deploy step for an app that does not migrate at startup, or before it starts. Build the app first: the command imports the module `"lattice.migrate"` names, which exports `store`, a `MongoStore` that is not started, and `migrations`, the list. The crm's:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
// examples/crm/src/servers/scripts/migrate.ts
|
|
115
|
+
import { migrations } from '../../infra/migrations/index.js';
|
|
116
|
+
import { mongoStore } from '../../infra/store.js';
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* What `npx lattice migrate` runs, after `npm run build`: the store, not
|
|
120
|
+
* started, and the migrations. The server also migrates at startup.
|
|
121
|
+
*/
|
|
122
|
+
export const store = mongoStore(process.env);
|
|
123
|
+
export { migrations };
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The module reads its connection settings as the app's server does; the CLI never sees them. The command starts the store, runs `migrate(store, migrations, { logger })` with a `ConsoleLogger`, and stops the store. Ordering, history and the lock are `migrate()`'s; see [store-mongo's README](../store-mongo#migrations). The output is `migrate()`'s log, and the command adds no lines of its own: one line per migration that ran, with its duration, or `migrations up to date`, and a line while it waits for another process's lock:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
$ npm run build && npx lattice migrate
|
|
130
|
+
2026-11-23T10:00:00.000Z info migration {"id":2,"name":"add-notes-index","durationMs":41,"outcome":"ok"}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
It exits 1 with one line when `@latticecms/store-mongo` or `@hexok/logger` is not installed in the app (with the command to install it), when the module is missing or does not load, when it lacks `store` or `migrations`, on a `MigrationError` (its message and data), or when the store cannot connect.
|
|
134
|
+
|
|
135
|
+
## Exit codes
|
|
136
|
+
|
|
137
|
+
| Code | Meaning |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| 0 | Done, or a dry run |
|
|
140
|
+
| 1 | Refused or failed. One line on stderr, `lattice: <why>`. Nothing was written |
|
|
141
|
+
| 2 | A usage error: an unknown command or flag, a missing or bad slug or name, or a bad config. The command's usage line follows |
|
package/dist/bin.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":""}
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { main } from './main.js';
|
|
3
|
+
// A reader that stops early, such as `| head`, closes the pipe. The output
|
|
4
|
+
// it did not read is not an error.
|
|
5
|
+
process.stdout.on('error', (error) => {
|
|
6
|
+
if ('code' in error && error.code === 'EPIPE')
|
|
7
|
+
return;
|
|
8
|
+
throw error;
|
|
9
|
+
});
|
|
10
|
+
// The exit code, not process.exit(), so piped output is flushed first.
|
|
11
|
+
process.exitCode = await main(process.argv.slice(2), process.cwd());
|
|
12
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,2EAA2E;AAC3E,mCAAmC;AACnC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;IACnC,IAAI,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO;QAAE,OAAO;IACtD,MAAM,KAAK,CAAC;AACd,CAAC,CAAC,CAAC;AAEH,uEAAuE;AACvE,OAAO,CAAC,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type FileEdit } from './edit.js';
|
|
2
|
+
/** A file to create or edit, with its text before and after. */
|
|
3
|
+
export type Change = {
|
|
4
|
+
readonly path: string;
|
|
5
|
+
readonly before: string | undefined;
|
|
6
|
+
readonly after: string;
|
|
7
|
+
};
|
|
8
|
+
/** A path from the app's root, with forward slashes, as output names it. */
|
|
9
|
+
export declare const shown: (root: string, path: string) => string;
|
|
10
|
+
/** The import specifier for `to` from a file at `from`: relative, ending in `.js`. */
|
|
11
|
+
export declare function specifierOf(from: string, to: string): string;
|
|
12
|
+
/** The lines a refused edit would have added, and where, for each file. */
|
|
13
|
+
export declare function byHand(root: string, created: readonly Change[], edits: readonly FileEdit[]): string;
|
|
14
|
+
/** The changes an edit makes: its file, before and after its inserts. */
|
|
15
|
+
export declare function changeOf(edit: FileEdit): Change;
|
|
16
|
+
/**
|
|
17
|
+
* Refuses when an edit's file has no place for it, printing every change by
|
|
18
|
+
* hand. Else prints each change as a diff on a dry run, or writes them all
|
|
19
|
+
* and names each file.
|
|
20
|
+
*/
|
|
21
|
+
export declare function apply(root: string, created: readonly Change[], edits: readonly FileEdit[], dryRun: boolean): Promise<void>;
|
|
22
|
+
//# sourceMappingURL=changes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"changes.d.ts","sourceRoot":"","sources":["../src/changes.ts"],"names":[],"mappings":"AAaA,OAAO,EAAgB,KAAK,QAAQ,EAAE,MAAM,WAAW,CAAC;AAGxD,gEAAgE;AAChE,MAAM,MAAM,MAAM,GAAG;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,4EAA4E;AAC5E,eAAO,MAAM,KAAK,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,WACL,CAAC;AAE5C,sFAAsF;AACtF,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAM5D;AAED,2EAA2E;AAC3E,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,KAAK,EAAE,SAAS,QAAQ,EAAE,UAe3B;AAqED,yEAAyE;AACzE,wBAAgB,QAAQ,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,CAS/C;AAED;;;;GAIG;AACH,wBAAsB,KAAK,CACzB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,KAAK,EAAE,SAAS,QAAQ,EAAE,EAC1B,MAAM,EAAE,OAAO,GACd,OAAO,CAAC,IAAI,CAAC,CAuBf"}
|
package/dist/changes.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { constants, existsSync } from 'node:fs';
|
|
2
|
+
import { access, chmod, link, readFile, rename, rm, stat, writeFile, } from 'node:fs/promises';
|
|
3
|
+
import { basename, dirname, join, relative, sep } from 'node:path';
|
|
4
|
+
import { diff } from './diff.js';
|
|
5
|
+
import { applyInserts } from './edit.js';
|
|
6
|
+
import { Exit } from './exit.js';
|
|
7
|
+
/** A path from the app's root, with forward slashes, as output names it. */
|
|
8
|
+
export const shown = (root, path) => relative(root, path).split(sep).join('/');
|
|
9
|
+
/** The import specifier for `to` from a file at `from`: relative, ending in `.js`. */
|
|
10
|
+
export function specifierOf(from, to) {
|
|
11
|
+
const path = relative(dirname(from), to)
|
|
12
|
+
.split(sep)
|
|
13
|
+
.join('/')
|
|
14
|
+
.replace(/\.tsx?$/, '.js');
|
|
15
|
+
return path.startsWith('.') ? path : `./${path}`;
|
|
16
|
+
}
|
|
17
|
+
/** The lines a refused edit would have added, and where, for each file. */
|
|
18
|
+
export function byHand(root, created, edits) {
|
|
19
|
+
return [
|
|
20
|
+
'Nothing was written. Make these changes by hand, or fix the shape and run the command again:',
|
|
21
|
+
...created.map((change) => `${shown(root, change.path)}, a new file:\n${change.after.trimEnd()}`),
|
|
22
|
+
...edits.flatMap((edit) => edit.additions.map((addition) => `${shown(root, edit.path)}, ${addition.where}:\n${addition.lines}`)),
|
|
23
|
+
].join('\n\n');
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Writes every change, or none. Each new text is staged in a file beside
|
|
27
|
+
* its target. Then each edited file must still hold the text that was
|
|
28
|
+
* parsed and be writable, and each new file must still be absent. Then the
|
|
29
|
+
* staged files take their targets' places: a rename for an edited file,
|
|
30
|
+
* and a link, which never replaces a file, for a new one. On any failure
|
|
31
|
+
* the staged files are removed and the files already placed are restored.
|
|
32
|
+
*/
|
|
33
|
+
async function writeAll(root, changes) {
|
|
34
|
+
const staged = [];
|
|
35
|
+
const placed = [];
|
|
36
|
+
let current = '';
|
|
37
|
+
try {
|
|
38
|
+
for (const change of changes) {
|
|
39
|
+
current = change.path;
|
|
40
|
+
const temp = join(dirname(change.path), `.${basename(change.path)}.lattice-${process.pid}.tmp`);
|
|
41
|
+
await writeFile(temp, change.after, { flag: 'wx' });
|
|
42
|
+
staged.push({ change, temp });
|
|
43
|
+
if (change.before !== undefined) {
|
|
44
|
+
await chmod(temp, (await stat(change.path)).mode & 0o7777);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for (const { change } of staged) {
|
|
48
|
+
current = change.path;
|
|
49
|
+
if (change.before === undefined) {
|
|
50
|
+
if (existsSync(change.path)) {
|
|
51
|
+
throw new Exit(`${shown(root, change.path)} already exists`, 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
await access(change.path, constants.W_OK);
|
|
56
|
+
if ((await readFile(change.path, 'utf8')) !== change.before) {
|
|
57
|
+
throw new Exit(`${shown(root, change.path)} changed while lattice ran; nothing was written`, 1);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const { change, temp } of staged) {
|
|
62
|
+
current = change.path;
|
|
63
|
+
if (change.before === undefined) {
|
|
64
|
+
await link(temp, change.path);
|
|
65
|
+
await rm(temp);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
await rename(temp, change.path);
|
|
69
|
+
}
|
|
70
|
+
placed.push(change);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
await Promise.all(staged.map(({ temp }) => rm(temp, { force: true })));
|
|
75
|
+
for (const change of placed.reverse()) {
|
|
76
|
+
if (change.before === undefined)
|
|
77
|
+
await rm(change.path, { force: true });
|
|
78
|
+
else
|
|
79
|
+
await writeFile(change.path, change.before);
|
|
80
|
+
}
|
|
81
|
+
if (error instanceof Exit)
|
|
82
|
+
throw error;
|
|
83
|
+
const code = error instanceof Error && 'code' in error ? ` (${error.code})` : '';
|
|
84
|
+
throw new Exit(`cannot write ${shown(root, current)}${code}; nothing was written`, 1);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The changes an edit makes: its file, before and after its inserts. */
|
|
88
|
+
export function changeOf(edit) {
|
|
89
|
+
return {
|
|
90
|
+
path: edit.path,
|
|
91
|
+
before: edit.before,
|
|
92
|
+
after: applyInserts(edit.before, edit.additions.flatMap((addition) => addition.inserts)),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Refuses when an edit's file has no place for it, printing every change by
|
|
97
|
+
* hand. Else prints each change as a diff on a dry run, or writes them all
|
|
98
|
+
* and names each file.
|
|
99
|
+
*/
|
|
100
|
+
export async function apply(root, created, edits, dryRun) {
|
|
101
|
+
const refused = edits.flatMap((edit) => edit.refused === undefined ? [] : [edit.refused]);
|
|
102
|
+
if (refused.length > 0) {
|
|
103
|
+
throw new Exit(refused.join('; '), 1, byHand(root, created, edits));
|
|
104
|
+
}
|
|
105
|
+
const changes = [...created, ...edits.map(changeOf)];
|
|
106
|
+
if (dryRun) {
|
|
107
|
+
for (const change of changes) {
|
|
108
|
+
process.stdout.write(diff(shown(root, change.path), change.before, change.after));
|
|
109
|
+
}
|
|
110
|
+
process.stdout.write('Dry run: nothing was written.\n');
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
await writeAll(root, changes);
|
|
114
|
+
for (const change of changes) {
|
|
115
|
+
process.stdout.write(`${change.before === undefined ? 'created' : 'edited'} ${shown(root, change.path)}\n`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=changes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"changes.js","sourceRoot":"","sources":["../src/changes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,EACL,MAAM,EACN,KAAK,EACL,IAAI,EACJ,QAAQ,EACR,MAAM,EACN,EAAE,EACF,IAAI,EACJ,SAAS,GACV,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,YAAY,EAAiB,MAAM,WAAW,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AASjC,4EAA4E;AAC5E,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,IAAY,EAAE,IAAY,EAAE,EAAE,CAClD,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE5C,sFAAsF;AACtF,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,EAAU;IAClD,MAAM,IAAI,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC;SACrC,KAAK,CAAC,GAAG,CAAC;SACV,IAAI,CAAC,GAAG,CAAC;SACT,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IAC7B,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAA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CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CACtF,CAAC;IACJ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const collectionUsage = "Usage: lattice collection <slug> [--tenant <field> | --global] [--label <singular>,<plural>] [--dry-run]";
|
|
2
|
+
export declare const collectionHelp = "Usage: lattice collection <slug> [--tenant <field> | --global] [--label <singular>,<plural>] [--dry-run]\n\nAdds a collection to the app: its file, a test, its registration in the\ncomposition, and its events in the events catalog.\n\n <slug> Lower snake case, plural: deal_notes.\n --tenant <field> The tenant field. The default is \"lattice.tenant\" in package.json.\n --global No tenant: one set of rows for every caller.\n --label s,p The label, when the slug's singular is wrong: person,people.\n --dry-run Print the changes as a diff and write nothing.\n";
|
|
3
|
+
/** `lattice collection`: writes a collection, its test and its registrations. */
|
|
4
|
+
export declare function collectionCommand(args: readonly string[], cwd: string): Promise<void>;
|
|
5
|
+
//# sourceMappingURL=collection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,eAAe,6GACgF,CAAC;AAE7G,eAAO,MAAM,cAAc,kmBAU1B,CAAC;AA8GF,iFAAiF;AACjF,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,IAAI,CAAC,CAiFf"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { parseArgs } from 'node:util';
|
|
5
|
+
import { apply, shown, specifierOf } from './changes.js';
|
|
6
|
+
import { existing, loadConfig } from './config.js';
|
|
7
|
+
import { compareSpecifiers, compositionEdit, eventsEdit, importedAs, parse, } from './edit.js';
|
|
8
|
+
import { Exit } from './exit.js';
|
|
9
|
+
import { fieldProblem, namesOf } from './names.js';
|
|
10
|
+
import { collectionText, testText } from './templates.js';
|
|
11
|
+
import { loadTypeScript } from './typescript.js';
|
|
12
|
+
export const collectionUsage = 'Usage: lattice collection <slug> [--tenant <field> | --global] [--label <singular>,<plural>] [--dry-run]';
|
|
13
|
+
export const collectionHelp = `${collectionUsage}
|
|
14
|
+
|
|
15
|
+
Adds a collection to the app: its file, a test, its registration in the
|
|
16
|
+
composition, and its events in the events catalog.
|
|
17
|
+
|
|
18
|
+
<slug> Lower snake case, plural: deal_notes.
|
|
19
|
+
--tenant <field> The tenant field. The default is "lattice.tenant" in package.json.
|
|
20
|
+
--global No tenant: one set of rows for every caller.
|
|
21
|
+
--label s,p The label, when the slug's singular is wrong: person,people.
|
|
22
|
+
--dry-run Print the changes as a diff and write nothing.
|
|
23
|
+
`;
|
|
24
|
+
const options = {
|
|
25
|
+
tenant: { type: 'string' },
|
|
26
|
+
global: { type: 'boolean' },
|
|
27
|
+
label: { type: 'string' },
|
|
28
|
+
'dry-run': { type: 'boolean' },
|
|
29
|
+
help: { type: 'boolean', short: 'h' },
|
|
30
|
+
};
|
|
31
|
+
/** The label from `--label singular,plural`. */
|
|
32
|
+
function labelOf(value) {
|
|
33
|
+
if (value === undefined)
|
|
34
|
+
return undefined;
|
|
35
|
+
const parts = value.split(',').map((part) => part.trim());
|
|
36
|
+
const [singular, plural] = parts;
|
|
37
|
+
const word = /^[\p{L}\p{N}][\p{L}\p{N} -]*$/u;
|
|
38
|
+
if (parts.length !== 2 ||
|
|
39
|
+
singular === undefined ||
|
|
40
|
+
plural === undefined ||
|
|
41
|
+
!word.test(singular) ||
|
|
42
|
+
!word.test(plural)) {
|
|
43
|
+
throw new Exit(`--label takes a singular and a plural of letters, digits, spaces and hyphens: --label person,people`, 2);
|
|
44
|
+
}
|
|
45
|
+
return { singular, plural };
|
|
46
|
+
}
|
|
47
|
+
/** The tenant field from the flags and the config, or a refusal. */
|
|
48
|
+
function tenantOf(flags, configured) {
|
|
49
|
+
if (flags.global === true && flags.tenant !== undefined) {
|
|
50
|
+
throw new Exit('--tenant and --global cannot both be given', 2);
|
|
51
|
+
}
|
|
52
|
+
if (flags.global === true)
|
|
53
|
+
return undefined;
|
|
54
|
+
const tenant = flags.tenant ?? configured;
|
|
55
|
+
if (tenant === undefined) {
|
|
56
|
+
throw new Exit('name the tenant field with --tenant <field>, or pass --global; "lattice.tenant" in package.json sets a default', 2);
|
|
57
|
+
}
|
|
58
|
+
const problem = fieldProblem(tenant);
|
|
59
|
+
if (problem !== undefined)
|
|
60
|
+
throw new Exit(problem, 1);
|
|
61
|
+
return tenant;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Every call of core's `collection('<slug>', ...)`, by whatever name the
|
|
65
|
+
* file imports it as, and every `through: '<slug>'`, in a file.
|
|
66
|
+
*/
|
|
67
|
+
function slugsIn(ts, source) {
|
|
68
|
+
const found = [];
|
|
69
|
+
const collection = importedAs(ts, source, 'collection', '@latticecms/core');
|
|
70
|
+
const visit = (node) => {
|
|
71
|
+
if (collection !== undefined &&
|
|
72
|
+
ts.isCallExpression(node) &&
|
|
73
|
+
ts.isIdentifier(node.expression) &&
|
|
74
|
+
node.expression.text === collection) {
|
|
75
|
+
const [slug] = node.arguments;
|
|
76
|
+
if (slug !== undefined && ts.isStringLiteral(slug)) {
|
|
77
|
+
found.push({ slug: slug.text, join: false });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (ts.isPropertyAssignment(node) &&
|
|
81
|
+
ts.isIdentifier(node.name) &&
|
|
82
|
+
node.name.text === 'through' &&
|
|
83
|
+
ts.isStringLiteral(node.initializer)) {
|
|
84
|
+
found.push({ slug: node.initializer.text, join: true });
|
|
85
|
+
}
|
|
86
|
+
ts.forEachChild(node, visit);
|
|
87
|
+
};
|
|
88
|
+
visit(source);
|
|
89
|
+
return found;
|
|
90
|
+
}
|
|
91
|
+
/** Refuses a slug that a collection or a join collection in the app already has. */
|
|
92
|
+
async function checkUnused(ts, root, directory, names) {
|
|
93
|
+
const files = (await readdir(directory)).filter((file) => /\.[cm]?tsx?$/.test(file) && !/\.(test|d)\.[cm]?tsx?$/.test(file));
|
|
94
|
+
for (const file of files) {
|
|
95
|
+
const path = join(directory, file);
|
|
96
|
+
const source = parse(ts, path, await readFile(path, 'utf8'));
|
|
97
|
+
const taken = slugsIn(ts, source).find(({ slug }) => slug === names.slug);
|
|
98
|
+
if (taken !== undefined) {
|
|
99
|
+
throw new Exit(taken.join
|
|
100
|
+
? `"${names.slug}" is a join collection in ${shown(root, path)}`
|
|
101
|
+
: `"${names.slug}" is a collection in ${shown(root, path)}`, 1);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** `lattice collection`: writes a collection, its test and its registrations. */
|
|
106
|
+
export async function collectionCommand(args, cwd) {
|
|
107
|
+
const { values: flags, positionals } = parseArgs({
|
|
108
|
+
args: [...args],
|
|
109
|
+
options,
|
|
110
|
+
allowPositionals: true,
|
|
111
|
+
strict: true,
|
|
112
|
+
});
|
|
113
|
+
if (flags.help === true) {
|
|
114
|
+
process.stdout.write(collectionHelp);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const [slug, ...extra] = positionals;
|
|
118
|
+
if (slug === undefined || extra.length > 0) {
|
|
119
|
+
throw new Exit(slug === undefined
|
|
120
|
+
? 'collection needs a slug'
|
|
121
|
+
: 'collection takes one slug', 2);
|
|
122
|
+
}
|
|
123
|
+
const label = labelOf(flags.label);
|
|
124
|
+
const config = await loadConfig(cwd);
|
|
125
|
+
const tenant = tenantOf(flags, config.tenant);
|
|
126
|
+
const names = namesOf(slug);
|
|
127
|
+
const { root } = config;
|
|
128
|
+
const directory = await existing(config, 'collections', 'directory');
|
|
129
|
+
const compositionPath = await existing(config, 'composition', 'file');
|
|
130
|
+
const eventsPath = await existing(config, 'events', 'file');
|
|
131
|
+
const ts = loadTypeScript(root);
|
|
132
|
+
const file = join(directory, `${names.file}.ts`);
|
|
133
|
+
const test = join(directory, `${names.file}.test.ts`);
|
|
134
|
+
for (const path of [file, test]) {
|
|
135
|
+
if (existsSync(path)) {
|
|
136
|
+
throw new Exit(`${shown(root, path)} already exists`, 1);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
await checkUnused(ts, root, directory, names);
|
|
140
|
+
const composition = compositionEdit(ts, compositionPath, shown(root, compositionPath), await readFile(compositionPath, 'utf8'), names, specifierOf(compositionPath, file));
|
|
141
|
+
const events = eventsEdit(ts, eventsPath, shown(root, eventsPath), await readFile(eventsPath, 'utf8'), names, specifierOf(eventsPath, file));
|
|
142
|
+
const edits = [composition, events];
|
|
143
|
+
const app = composition.app === undefined
|
|
144
|
+
? undefined
|
|
145
|
+
: {
|
|
146
|
+
name: composition.app,
|
|
147
|
+
specifier: specifierOf(test, compositionPath),
|
|
148
|
+
};
|
|
149
|
+
const created = [
|
|
150
|
+
{
|
|
151
|
+
path: file,
|
|
152
|
+
before: undefined,
|
|
153
|
+
after: collectionText(names, tenant, label),
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
path: test,
|
|
157
|
+
before: undefined,
|
|
158
|
+
after: testText(names, app, compareSpecifiers),
|
|
159
|
+
},
|
|
160
|
+
];
|
|
161
|
+
await apply(root, created, edits, flags['dry-run'] === true);
|
|
162
|
+
if (flags['dry-run'] === true)
|
|
163
|
+
return;
|
|
164
|
+
process.stdout.write(`${names.slug} is open to any signed-in caller. Tighten its access in ${shown(root, compositionPath)}.\n`);
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=collection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.js","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,QAA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,EAAE,KAAK,EAAE,KAAK,CAAC,SAAS,CAAC,KAAK,IAAI,CAAC,CAAC;IAC7D,IAAI,KAAK,CAAC,SAAS,CAAC,KAAK,IAAI;QAAE,OAAO;IACtC,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,GAAG,KAAK,CAAC,IAAI,2DAA2D,KAAK,CAAC,IAAI,EAAE,eAAe,CAAC,KAAK,CAC1G,CAAC;AACJ,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** The `"lattice"` keys that name paths, each defaulting to the crm's layout. */
|
|
2
|
+
declare const defaults: {
|
|
3
|
+
collections: string;
|
|
4
|
+
composition: string;
|
|
5
|
+
events: string;
|
|
6
|
+
migrations: string;
|
|
7
|
+
migrate: string;
|
|
8
|
+
};
|
|
9
|
+
export type PathKey = keyof typeof defaults;
|
|
10
|
+
/** The app's config, checked: every path is inside the app. */
|
|
11
|
+
export type Config = {
|
|
12
|
+
/** The directory of the app's `package.json`. */
|
|
13
|
+
readonly root: string;
|
|
14
|
+
/** The `package.json`, as messages name it. */
|
|
15
|
+
readonly manifest: string;
|
|
16
|
+
/** Each path key's absolute path. */
|
|
17
|
+
readonly paths: Readonly<Record<PathKey, string>>;
|
|
18
|
+
/** Each path key's value as written, for messages. */
|
|
19
|
+
readonly written: Readonly<Record<PathKey, string>>;
|
|
20
|
+
/** The tenant field of a new collection, when the app names one. */
|
|
21
|
+
readonly tenant: string | undefined;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The app's config: the `"lattice"` key of the nearest `package.json`, over
|
|
25
|
+
* the defaults. Anything wrong with it is a usage error that names the file
|
|
26
|
+
* and the key: invalid JSON, an unknown key, a value that is not a
|
|
27
|
+
* non-empty string, a bad tenant field, or a path outside the app. Whether
|
|
28
|
+
* a path exists is checked by the command that needs it, with `existing`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function loadConfig(cwd: string): Promise<Config>;
|
|
31
|
+
/**
|
|
32
|
+
* The path of `key`, which must exist and be a file or a directory as
|
|
33
|
+
* `kind` says. Otherwise a usage error that names the key.
|
|
34
|
+
*/
|
|
35
|
+
export declare function existing(config: Config, key: PathKey, kind: 'file' | 'directory'): Promise<string>;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAMA,iFAAiF;AACjF,QAAA,MAAM,QAAQ;;;;;;CAMb,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,MAAM,OAAO,QAAQ,CAAC;AAK5C,+DAA+D;AAC/D,MAAM,MAAM,MAAM,GAAG;IACnB,iDAAiD;IACjD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,+CAA+C;IAC/C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,qCAAqC;IACrC,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;IAClD,sDAAsD;IACtD,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;IACpD,oEAAoE;IACpE,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;CACrC,CAAC;AAYF;;;;;;GAMG;AACH,wBAAsB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CA+D7D;AAED;;;GAGG;AACH,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,MAAM,GAAG,WAAW,GACzB,OAAO,CAAC,MAAM,CAAC,CAiBjB"}
|