@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/dist/layout.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** An object argument: its property lines, and whether it stays expanded. */
|
|
2
|
+
export type ObjectArg = {
|
|
3
|
+
/** Its property lines at `indent`, each ending in a comma. */
|
|
4
|
+
readonly lines: (indent: string) => readonly string[];
|
|
5
|
+
/** Whether it is written over several lines, which Biome keeps. */
|
|
6
|
+
readonly expanded: boolean;
|
|
7
|
+
};
|
|
8
|
+
type Arg = string | ObjectArg;
|
|
9
|
+
/**
|
|
10
|
+
* A call, as Biome lays it out: on one line if it fits; else hugging a last
|
|
11
|
+
* object argument, if the line up to its `{` fits; else one argument per
|
|
12
|
+
* line. `tail` follows the closing parenthesis, such as `;`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function call(indent: string, head: string, args: readonly Arg[], tail: string, unit?: string): string[];
|
|
15
|
+
/** `key: value,`, or the value on the next line when it does not fit. */
|
|
16
|
+
export declare function entry(indent: string, key: string, value: string, unit?: string): string[];
|
|
17
|
+
/** `key: { a, b },`, or the object's properties on their own lines. */
|
|
18
|
+
export declare function objectEntry(indent: string, key: string, props: readonly string[], unit?: string): string[];
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["../src/layout.ts"],"names":[],"mappings":"AAUA,6EAA6E;AAC7E,MAAM,MAAM,SAAS,GAAG;IACtB,8DAA8D;IAC9D,QAAQ,CAAC,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,SAAS,MAAM,EAAE,CAAC;IACtD,mEAAmE;IACnE,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B,CAAC;AAEF,KAAK,GAAG,GAAG,MAAM,GAAG,SAAS,CAAC;AAQ9B;;;;GAIG;AACH,wBAAgB,IAAI,CAClB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,SAAS,GAAG,EAAE,EACpB,IAAI,EAAE,MAAM,EACZ,IAAI,SAAO,GACV,MAAM,EAAE,CAyBV;AAED,yEAAyE;AACzE,wBAAgB,KAAK,CACnB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,IAAI,SAAO,GACV,MAAM,EAAE,CAGV;AAED,uEAAuE;AACvE,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,IAAI,SAAO,GACV,MAAM,EAAE,CASV"}
|
package/dist/layout.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Generated lines follow Biome's defaults (line width 80), so an app that
|
|
2
|
+
// runs Biome or Prettier finds nothing to change in a typical edit. Only the
|
|
3
|
+
// breaks the CLI's own lines need are here, one level deep: a name long
|
|
4
|
+
// enough to need a second level is left for the app's formatter.
|
|
5
|
+
const width = 80;
|
|
6
|
+
/** Whether a line fits Biome's default width. */
|
|
7
|
+
const fits = (line) => line.length <= width;
|
|
8
|
+
/** `{ a: 1, b: 2 }`: an object's property lines on one line. */
|
|
9
|
+
function inline(object) {
|
|
10
|
+
const props = object.lines('').map((line) => line.replace(/,$/, ''));
|
|
11
|
+
return `{ ${props.join(', ')} }`;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A call, as Biome lays it out: on one line if it fits; else hugging a last
|
|
15
|
+
* object argument, if the line up to its `{` fits; else one argument per
|
|
16
|
+
* line. `tail` follows the closing parenthesis, such as `;`.
|
|
17
|
+
*/
|
|
18
|
+
export function call(indent, head, args, tail, unit = ' ') {
|
|
19
|
+
const last = args.at(-1);
|
|
20
|
+
const first = args.slice(0, -1);
|
|
21
|
+
if (args.every((arg) => typeof arg === 'string' || !arg.expanded)) {
|
|
22
|
+
const one = `${indent}${head}(${args
|
|
23
|
+
.map((arg) => (typeof arg === 'string' ? arg : inline(arg)))
|
|
24
|
+
.join(', ')})${tail}`;
|
|
25
|
+
if (fits(one))
|
|
26
|
+
return [one];
|
|
27
|
+
}
|
|
28
|
+
if (last !== undefined && typeof last !== 'string') {
|
|
29
|
+
const open = `${indent}${head}(${[...first, '{'].join(', ')}`;
|
|
30
|
+
if (first.every((arg) => typeof arg === 'string') && fits(open)) {
|
|
31
|
+
return [open, ...last.lines(indent + unit), `${indent}})${tail}`];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const inner = indent + unit;
|
|
35
|
+
return [
|
|
36
|
+
`${indent}${head}(`,
|
|
37
|
+
...args.flatMap((arg) => typeof arg === 'string'
|
|
38
|
+
? [`${inner}${arg},`]
|
|
39
|
+
: [`${inner}{`, ...arg.lines(inner + unit), `${inner}},`]),
|
|
40
|
+
`${indent})${tail}`,
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
/** `key: value,`, or the value on the next line when it does not fit. */
|
|
44
|
+
export function entry(indent, key, value, unit = ' ') {
|
|
45
|
+
const one = `${indent}${key}: ${value},`;
|
|
46
|
+
return fits(one) ? [one] : [`${indent}${key}:`, `${indent}${unit}${value},`];
|
|
47
|
+
}
|
|
48
|
+
/** `key: { a, b },`, or the object's properties on their own lines. */
|
|
49
|
+
export function objectEntry(indent, key, props, unit = ' ') {
|
|
50
|
+
const one = `${indent}${key}: { ${props.join(', ')} },`;
|
|
51
|
+
return fits(one)
|
|
52
|
+
? [one]
|
|
53
|
+
: [
|
|
54
|
+
`${indent}${key}: {`,
|
|
55
|
+
...props.map((prop) => `${indent}${unit}${prop},`),
|
|
56
|
+
`${indent}},`,
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.js","sourceRoot":"","sources":["../src/layout.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6EAA6E;AAC7E,wEAAwE;AACxE,iEAAiE;AAEjE,MAAM,KAAK,GAAG,EAAE,CAAC;AAEjB,iDAAiD;AACjD,MAAM,IAAI,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC;AAYpD,gEAAgE;AAChE,SAAS,MAAM,CAAC,MAAiB;IAC/B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;IACrE,OAAO,KAAK,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;AACnC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,IAAI,CAClB,MAAc,EACd,IAAY,EACZ,IAAoB,EACpB,IAAY,EACZ,IAAI,GAAG,IAAI;IAEX,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IACzB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAChC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,GAAG,CA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AAC,EAAE,GAAG,KAAK,IAAI,CAAC,CAC5D;QACD,GAAG,MAAM,IAAI,IAAI,EAAE;KACpB,CAAC;AACJ,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,KAAK,CACnB,MAAc,EACd,GAAW,EACX,KAAa,EACb,IAAI,GAAG,IAAI;IAEX,MAAM,GAAG,GAAG,GAAG,MAAM,GAAG,GAAG,KAAK,KAAK,GAAG,CAAC;IACzC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,GAAG,GAAG,EAAE,GAAG,MAAM,GAAG,IAAI,GAAG,KAAK,GAAG,CAAC,CAAC;AAC/E,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,WAAW,CACzB,MAAc,EACd,GAAW,EACX,KAAwB,EACxB,IAAI,GAAG,IAAI;IAEX,MAAM,GAAG,GAAG,GAAG,MAAM,GAAG,GAAG,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;IACxD,OAAO,IAAI,CAAC,GAAG,CAAC;QACd,CAAC,CAAC,CAAC,GAAG,CAAC;QACP,CAAC,CAAC;YACE,GAAG,MAAM,GAAG,GAAG,KAAK;YACpB,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,GAAG,CAAC;YAClD,GAAG,MAAM,IAAI;SACd,CAAC;AACR,CAAC"}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const help = "Usage: lattice <command> [options]\n\nCommands:\n collection <slug> Add a collection, its test, and its registrations.\n migration <name> Add the next data migration to the list.\n migrate Run the pending migrations, from the built app.\n\nRun lattice <command> --help for a command's options.\n";
|
|
2
|
+
/**
|
|
3
|
+
* Runs the command `args` names, in `cwd`, and returns the exit code: 0
|
|
4
|
+
* done, 1 refused or failed, 2 a usage error.
|
|
5
|
+
*/
|
|
6
|
+
export declare function main(args: readonly string[], cwd: string): Promise<number>;
|
|
7
|
+
//# sourceMappingURL=main.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,IAAI,gUAQhB,CAAC;AAiBF;;;GAGG;AACH,wBAAsB,IAAI,CACxB,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,MAAM,CAAC,CAiCjB"}
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { collectionCommand, collectionUsage } from './collection.js';
|
|
2
|
+
import { Exit } from './exit.js';
|
|
3
|
+
import { migrateCommand, migrateUsage } from './migrate.js';
|
|
4
|
+
import { migrationCommand, migrationUsage } from './migration.js';
|
|
5
|
+
export const help = `Usage: lattice <command> [options]
|
|
6
|
+
|
|
7
|
+
Commands:
|
|
8
|
+
collection <slug> Add a collection, its test, and its registrations.
|
|
9
|
+
migration <name> Add the next data migration to the list.
|
|
10
|
+
migrate Run the pending migrations, from the built app.
|
|
11
|
+
|
|
12
|
+
Run lattice <command> --help for a command's options.
|
|
13
|
+
`;
|
|
14
|
+
const commands = {
|
|
15
|
+
collection: { usage: collectionUsage, run: collectionCommand },
|
|
16
|
+
migration: { usage: migrationUsage, run: migrationCommand },
|
|
17
|
+
migrate: { usage: migrateUsage, run: migrateCommand },
|
|
18
|
+
};
|
|
19
|
+
/** The first sentence of parseArgs' message, which goes on to explain `--`. */
|
|
20
|
+
const firstSentence = (message) => message.split(/\.\s/)[0] ?? message;
|
|
21
|
+
/**
|
|
22
|
+
* Runs the command `args` names, in `cwd`, and returns the exit code: 0
|
|
23
|
+
* done, 1 refused or failed, 2 a usage error.
|
|
24
|
+
*/
|
|
25
|
+
export async function main(args, cwd) {
|
|
26
|
+
const [name, ...rest] = args;
|
|
27
|
+
if (name === undefined || name === '--help' || name === '-h') {
|
|
28
|
+
process.stdout.write(help);
|
|
29
|
+
return name === undefined ? 2 : 0;
|
|
30
|
+
}
|
|
31
|
+
const command = commands[name];
|
|
32
|
+
if (command === undefined) {
|
|
33
|
+
process.stderr.write(`lattice: unknown command "${name}"\n\n${help}`);
|
|
34
|
+
return 2;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
await command.run(rest, cwd);
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (error instanceof Exit) {
|
|
42
|
+
process.stderr.write(`lattice: ${error.message}\n`);
|
|
43
|
+
if (error.code === 2)
|
|
44
|
+
process.stderr.write(`${command.usage}\n`);
|
|
45
|
+
if (error.details !== '')
|
|
46
|
+
process.stdout.write(`${error.details}\n`);
|
|
47
|
+
return error.code;
|
|
48
|
+
}
|
|
49
|
+
// parseArgs throws a TypeError for an unknown flag or a missing value.
|
|
50
|
+
if (error instanceof TypeError && 'code' in error) {
|
|
51
|
+
process.stderr.write(`lattice: ${firstSentence(error.message)}\n${command.usage}\n`);
|
|
52
|
+
return 2;
|
|
53
|
+
}
|
|
54
|
+
process.stderr.write(`lattice: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACrE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAElE,MAAM,CA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AAI,EAAE,CAAC;YAC1B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,YAAY,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;YACpD,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC;gBAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC;YACjE,IAAI,KAAK,CAAC,OAAO,KAAK,EAAE;gBAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;YACrE,OAAO,KAAK,CAAC,IAAI,CAAC;QACpB,CAAC;QACD,uEAAuE;QACvE,IAAI,KAAK,YAAY,SAAS,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;YAClD,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,YAAY,aAAa,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,KAAK,IAAI,CAC/D,CAAC;YACF,OAAO,CAAC,CAAC;QACX,CAAC;QACD,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,YAAY,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CACvE,CAAC;QACF,OAAO,CAAC,CAAC;IACX,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare const migrateUsage = "Usage: lattice migrate";
|
|
2
|
+
export declare const migrateHelp = "Usage: lattice migrate\n\nRuns the app's pending migrations with @latticecms/store-mongo's migrate(),\nfrom the module \"lattice.migrate\" in package.json names (a built file that\nexports store and migrations). Build the app first.\n";
|
|
3
|
+
/**
|
|
4
|
+
* `lattice migrate`: imports the app's built module, starts its store, runs
|
|
5
|
+
* migrate() with a console logger, and stops the store. Ordering, history
|
|
6
|
+
* and the lock are migrate()'s.
|
|
7
|
+
*/
|
|
8
|
+
export declare function migrateCommand(args: readonly string[], cwd: string): Promise<void>;
|
|
9
|
+
//# sourceMappingURL=migrate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAWA,eAAO,MAAM,YAAY,2BAA2B,CAAC;AAErD,eAAO,MAAM,WAAW,+OAKvB,CAAC;AAuDF;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,IAAI,CAAC,CAyEf"}
|
package/dist/migrate.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import { parseArgs } from 'node:util';
|
|
6
|
+
import { shown } from './changes.js';
|
|
7
|
+
import { loadConfig } from './config.js';
|
|
8
|
+
import { Exit, isRecord } from './exit.js';
|
|
9
|
+
export const migrateUsage = 'Usage: lattice migrate';
|
|
10
|
+
export const migrateHelp = `${migrateUsage}
|
|
11
|
+
|
|
12
|
+
Runs the app's pending migrations with @latticecms/store-mongo's migrate(),
|
|
13
|
+
from the module "lattice.migrate" in package.json names (a built file that
|
|
14
|
+
exports store and migrations). Build the app first.
|
|
15
|
+
`;
|
|
16
|
+
const options = { help: { type: 'boolean', short: 'h' } };
|
|
17
|
+
/** A package's entry, resolved and imported from the app's directory. */
|
|
18
|
+
async function fromApp(root, name, install) {
|
|
19
|
+
let resolved;
|
|
20
|
+
try {
|
|
21
|
+
resolved = createRequire(join(root, 'package.json')).resolve(name);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
throw new Exit(`${name} is not installed in the app: run ${install}`, 1);
|
|
25
|
+
}
|
|
26
|
+
return import(pathToFileURL(resolved).href);
|
|
27
|
+
}
|
|
28
|
+
// Each guard checks what the command uses of the package.
|
|
29
|
+
function isStoreMongo(value) {
|
|
30
|
+
return (isRecord(value) &&
|
|
31
|
+
typeof value.migrate === 'function' &&
|
|
32
|
+
typeof value.MongoStore === 'function' &&
|
|
33
|
+
typeof value.MigrationError === 'function');
|
|
34
|
+
}
|
|
35
|
+
function isLogger(value) {
|
|
36
|
+
return isRecord(value) && typeof value.ConsoleLogger === 'function';
|
|
37
|
+
}
|
|
38
|
+
/** Whether `value` is a migration as migrate() takes one. */
|
|
39
|
+
function isMigration(value) {
|
|
40
|
+
return (isRecord(value) &&
|
|
41
|
+
typeof value.id === 'number' &&
|
|
42
|
+
typeof value.name === 'string' &&
|
|
43
|
+
typeof value.up === 'function' &&
|
|
44
|
+
(value.transaction === undefined || typeof value.transaction === 'boolean'));
|
|
45
|
+
}
|
|
46
|
+
/** One line for an error: a MigrationError's message and data, or the message. */
|
|
47
|
+
function lineOf(storeMongo, error) {
|
|
48
|
+
if (storeMongo.MigrationError.is(error)) {
|
|
49
|
+
const data = 'data' in error ? JSON.stringify(error.data) : undefined;
|
|
50
|
+
return data === undefined || data === '{}'
|
|
51
|
+
? error.message
|
|
52
|
+
: `${error.message} ${data}`;
|
|
53
|
+
}
|
|
54
|
+
return error instanceof Error ? error.message : String(error);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* `lattice migrate`: imports the app's built module, starts its store, runs
|
|
58
|
+
* migrate() with a console logger, and stops the store. Ordering, history
|
|
59
|
+
* and the lock are migrate()'s.
|
|
60
|
+
*/
|
|
61
|
+
export async function migrateCommand(args, cwd) {
|
|
62
|
+
const { values: flags, positionals } = parseArgs({
|
|
63
|
+
args: [...args],
|
|
64
|
+
options,
|
|
65
|
+
allowPositionals: true,
|
|
66
|
+
strict: true,
|
|
67
|
+
});
|
|
68
|
+
if (flags.help === true) {
|
|
69
|
+
process.stdout.write(migrateHelp);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (positionals.length > 0)
|
|
73
|
+
throw new Exit('migrate takes no arguments', 2);
|
|
74
|
+
const config = await loadConfig(cwd);
|
|
75
|
+
const { root } = config;
|
|
76
|
+
const path = config.paths.migrate;
|
|
77
|
+
const file = shown(root, path);
|
|
78
|
+
const storeMongo = await fromApp(root, '@latticecms/store-mongo', 'npm install @latticecms/store-mongo');
|
|
79
|
+
if (!isStoreMongo(storeMongo)) {
|
|
80
|
+
throw new Exit('@latticecms/store-mongo in the app has no migrate()', 1);
|
|
81
|
+
}
|
|
82
|
+
const loggers = await fromApp(root, '@hexok/logger', 'npm install @hexok/logger');
|
|
83
|
+
if (!isLogger(loggers)) {
|
|
84
|
+
throw new Exit('@hexok/logger in the app has no ConsoleLogger', 1);
|
|
85
|
+
}
|
|
86
|
+
if (!existsSync(path)) {
|
|
87
|
+
throw new Exit(`${file} does not exist: build the app first, or set "lattice.migrate" in ${config.manifest}`, 1);
|
|
88
|
+
}
|
|
89
|
+
let module;
|
|
90
|
+
try {
|
|
91
|
+
module = await import(pathToFileURL(path).href);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
throw new Exit(`cannot load ${file}: ${lineOf(storeMongo, error)}`, 1);
|
|
95
|
+
}
|
|
96
|
+
const store = isRecord(module) ? module.store : undefined;
|
|
97
|
+
const migrations = isRecord(module) ? module.migrations : undefined;
|
|
98
|
+
if (!(store instanceof storeMongo.MongoStore)) {
|
|
99
|
+
throw new Exit(`${file} must export store, a MongoStore that is not started`, 1);
|
|
100
|
+
}
|
|
101
|
+
if (!Array.isArray(migrations) || !migrations.every(isMigration)) {
|
|
102
|
+
throw new Exit(`${file} must export migrations, the list of migrations migrate() runs`, 1);
|
|
103
|
+
}
|
|
104
|
+
// migrate() logs each migration it runs, with its duration, and
|
|
105
|
+
// "migrations up to date" when none is pending, so the command adds no
|
|
106
|
+
// lines of its own.
|
|
107
|
+
try {
|
|
108
|
+
await store.start();
|
|
109
|
+
await storeMongo.migrate(store, migrations, {
|
|
110
|
+
logger: new loggers.ConsoleLogger(),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw new Exit(lineOf(storeMongo, error), 1);
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
await store.stop();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=migrate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.js","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGtC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE3C,MAAM,CAAC,MAAM,YAAY,GAAG,wBAAwB,CAAC;AAErD,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,YAAY;;;;;CAKzC,CAAC;AAEF,MAAM,OAAO,GAAG,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE,EAAW,CAAC;AAEnE,yEAAyE;AACzE,KAAK,UAAU,OAAO,CACpB,IAAY,EACZ,IAAY,EACZ,OAAe;IAEf,IAAI,QAAgB,CAAC;IACrB,IAAI,CAAC;QACH,QAAQ,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACrE,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,IAAI,CAAC,GAAG,IAAI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AM,KAAK,CAAC,KAAK,EAAE,CAAC;QACpB,MAAM,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,UAAU,EAAE;YAC1C,MAAM,EAAE,IAAI,OAAO,CAAC,aAAa,EAAE;SACpC,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/C,CAAC;YAAS,CAAC;QACT,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC;IACrB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const migrationUsage = "Usage: lattice migration <name> [--transaction] [--dry-run]";
|
|
2
|
+
export declare const migrationHelp = "Usage: lattice migration <name> [--transaction] [--dry-run]\n\nAdds a data migration: its file, with the next id, and its place at the end\nof the list that migrate() runs.\n\n <name> Kebab case: add-notes-index.\n --transaction Run it and its record in one transaction (a replica set).\n --dry-run Print the changes as a diff and write nothing.\n";
|
|
3
|
+
/** `lattice migration`: writes the next migration and appends it to the list. */
|
|
4
|
+
export declare function migrationCommand(args: readonly string[], cwd: string): Promise<void>;
|
|
5
|
+
//# sourceMappingURL=migration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.d.ts","sourceRoot":"","sources":["../src/migration.ts"],"names":[],"mappings":"AAiBA,eAAO,MAAM,cAAc,gEACoC,CAAC;AAEhE,eAAO,MAAM,aAAa,6XAQzB,CAAC;AAsIF,iFAAiF;AACjF,wBAAsB,gBAAgB,CACpC,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC,IAAI,CAAC,CAwEf"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join, resolve } from 'node:path';
|
|
4
|
+
import { parseArgs } from 'node:util';
|
|
5
|
+
import { apply, shown } from './changes.js';
|
|
6
|
+
import { existing, loadConfig } from './config.js';
|
|
7
|
+
import { migrationList, migrationsEdit, parse, } from './edit.js';
|
|
8
|
+
import { Exit } from './exit.js';
|
|
9
|
+
import { call } from './layout.js';
|
|
10
|
+
import { loadTypeScript } from './typescript.js';
|
|
11
|
+
export const migrationUsage = 'Usage: lattice migration <name> [--transaction] [--dry-run]';
|
|
12
|
+
export const migrationHelp = `${migrationUsage}
|
|
13
|
+
|
|
14
|
+
Adds a data migration: its file, with the next id, and its place at the end
|
|
15
|
+
of the list that migrate() runs.
|
|
16
|
+
|
|
17
|
+
<name> Kebab case: add-notes-index.
|
|
18
|
+
--transaction Run it and its record in one transaction (a replica set).
|
|
19
|
+
--dry-run Print the changes as a diff and write nothing.
|
|
20
|
+
`;
|
|
21
|
+
const options = {
|
|
22
|
+
transaction: { type: 'boolean' },
|
|
23
|
+
'dry-run': { type: 'boolean' },
|
|
24
|
+
help: { type: 'boolean', short: 'h' },
|
|
25
|
+
};
|
|
26
|
+
/** How many digits an id has in a file name, as the crm's `0001-…` have. */
|
|
27
|
+
const digits = 4;
|
|
28
|
+
/** The object a default export holds, under `satisfies`, `as` and parentheses. */
|
|
29
|
+
function exported(ts, source) {
|
|
30
|
+
for (const statement of source.statements) {
|
|
31
|
+
if (!ts.isExportAssignment(statement) || statement.isExportEquals)
|
|
32
|
+
continue;
|
|
33
|
+
let value = statement.expression;
|
|
34
|
+
while (ts.isSatisfiesExpression(value) ||
|
|
35
|
+
ts.isAsExpression(value) ||
|
|
36
|
+
ts.isParenthesizedExpression(value)) {
|
|
37
|
+
value = value.expression;
|
|
38
|
+
}
|
|
39
|
+
return ts.isObjectLiteralExpression(value) ? value : undefined;
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
/** A listed migration's id and name, read from its file's default export. */
|
|
44
|
+
async function readMigration(ts, root, index, element) {
|
|
45
|
+
const path = resolve(dirname(index), element.specifier.replace(/\.js$/, '.ts'));
|
|
46
|
+
const file = shown(root, path);
|
|
47
|
+
const text = await readFile(path, 'utf8').catch(() => undefined);
|
|
48
|
+
if (text === undefined) {
|
|
49
|
+
throw new Exit(`${file}, which the list imports, does not exist`, 1);
|
|
50
|
+
}
|
|
51
|
+
const object = exported(ts, parse(ts, path, text));
|
|
52
|
+
const value = (key) => object?.properties.find((property) => ts.isPropertyAssignment(property) &&
|
|
53
|
+
ts.isIdentifier(property.name) &&
|
|
54
|
+
property.name.text === key)?.initializer;
|
|
55
|
+
const id = value('id');
|
|
56
|
+
const name = value('name');
|
|
57
|
+
if (id === undefined ||
|
|
58
|
+
!ts.isNumericLiteral(id) ||
|
|
59
|
+
name === undefined ||
|
|
60
|
+
!ts.isStringLiteral(name)) {
|
|
61
|
+
throw new Exit(`cannot read the id and name of ${file}: its default export needs a numeric id and a string name`, 1);
|
|
62
|
+
}
|
|
63
|
+
return { id: Number(id.text), name: name.text };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The list's own checks, as migrate() makes them: ids 1, 2, 3 … in order,
|
|
67
|
+
* none twice. The words are migrate()'s, with the error it would throw.
|
|
68
|
+
*/
|
|
69
|
+
function checkIds(file, ids) {
|
|
70
|
+
const seen = new Set();
|
|
71
|
+
for (const id of ids) {
|
|
72
|
+
if (seen.has(id)) {
|
|
73
|
+
throw new Exit(`two migrations in ${file} share an id: ${id} (migrate() throws MigrationError.Duplicate)`, 1);
|
|
74
|
+
}
|
|
75
|
+
seen.add(id);
|
|
76
|
+
}
|
|
77
|
+
ids.forEach((id, index) => {
|
|
78
|
+
if (id !== index + 1) {
|
|
79
|
+
throw new Exit(`migration ids in ${file} must run 1, 2, 3 … in list order, with no gap: expected ${index + 1}, found ${id} (migrate() throws MigrationError.Gap)`, 1);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* A new migration: its id and name, and `transaction` when asked. Its `up`
|
|
85
|
+
* throws until it is written, so a build that runs it early fails instead
|
|
86
|
+
* of recording it as done.
|
|
87
|
+
*/
|
|
88
|
+
function migrationText(id, name, transaction) {
|
|
89
|
+
return [
|
|
90
|
+
"import type { Migration } from '@latticecms/store-mongo';",
|
|
91
|
+
'',
|
|
92
|
+
'/**',
|
|
93
|
+
' * What this migration changes, and why. `up` must be safe to run again',
|
|
94
|
+
' * after it failed partway: the next run retries it.',
|
|
95
|
+
' *',
|
|
96
|
+
" * `up({ db, session, signal, logger })` receives the store's raw `db`,",
|
|
97
|
+
" * whose documents are in their stored shape; the transaction's `session`",
|
|
98
|
+
' * when `transaction` is true, to pass to each call; a `signal` that aborts',
|
|
99
|
+
' * when the migrations lock is lost, to check in long loops; and a',
|
|
100
|
+
" * `logger` with the migration's id and name.",
|
|
101
|
+
' */',
|
|
102
|
+
'export default {',
|
|
103
|
+
` id: ${id},`,
|
|
104
|
+
` name: '${name}',`,
|
|
105
|
+
...(transaction ? [' transaction: true,'] : []),
|
|
106
|
+
' async up() {',
|
|
107
|
+
...call(' ', 'throw new Error', [
|
|
108
|
+
`'Migration ${id} ${name} is not written yet: write up() before running it.'`,
|
|
109
|
+
], ';'),
|
|
110
|
+
' },',
|
|
111
|
+
'} satisfies Migration;',
|
|
112
|
+
'',
|
|
113
|
+
].join('\n');
|
|
114
|
+
}
|
|
115
|
+
/** `lattice migration`: writes the next migration and appends it to the list. */
|
|
116
|
+
export async function migrationCommand(args, cwd) {
|
|
117
|
+
const { values: flags, positionals } = parseArgs({
|
|
118
|
+
args: [...args],
|
|
119
|
+
options,
|
|
120
|
+
allowPositionals: true,
|
|
121
|
+
strict: true,
|
|
122
|
+
});
|
|
123
|
+
if (flags.help === true) {
|
|
124
|
+
process.stdout.write(migrationHelp);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const [name, ...extra] = positionals;
|
|
128
|
+
if (name === undefined || extra.length > 0) {
|
|
129
|
+
throw new Exit(name === undefined
|
|
130
|
+
? 'migration needs a name'
|
|
131
|
+
: 'migration takes one name', 2);
|
|
132
|
+
}
|
|
133
|
+
if (!/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(name)) {
|
|
134
|
+
throw new Exit(`"${name}" is not a migration name: use kebab case, as add-notes-index`, 2);
|
|
135
|
+
}
|
|
136
|
+
const config = await loadConfig(cwd);
|
|
137
|
+
const { root } = config;
|
|
138
|
+
const directory = await existing(config, 'migrations', 'directory');
|
|
139
|
+
const index = join(directory, 'index.ts');
|
|
140
|
+
const file = shown(root, index);
|
|
141
|
+
if (!existsSync(index)) {
|
|
142
|
+
throw new Exit(`${file} does not exist: the migrations list goes there`, 1);
|
|
143
|
+
}
|
|
144
|
+
const ts = loadTypeScript(root);
|
|
145
|
+
const before = await readFile(index, 'utf8');
|
|
146
|
+
const list = migrationList(ts, parse(ts, index, before), file);
|
|
147
|
+
if (typeof list === 'string')
|
|
148
|
+
throw new Exit(list, 1);
|
|
149
|
+
const listed = await Promise.all(list.elements.map((element) => readMigration(ts, root, index, element)));
|
|
150
|
+
checkIds(file, listed.map(({ id }) => id));
|
|
151
|
+
if (listed.some((migration) => migration.name === name)) {
|
|
152
|
+
throw new Exit(`${file} already lists a migration named ${name}`, 1);
|
|
153
|
+
}
|
|
154
|
+
const taken = (await readdir(directory)).find((entry) => entry.replace(/^\d+-/, '').replace(/\.[cm]?tsx?$/, '') === name);
|
|
155
|
+
if (taken !== undefined) {
|
|
156
|
+
throw new Exit(`${shown(root, join(directory, taken))} already exists`, 1);
|
|
157
|
+
}
|
|
158
|
+
const id = listed.length + 1;
|
|
159
|
+
const base = `${String(id).padStart(digits, '0')}-${name}`;
|
|
160
|
+
const path = join(directory, `${base}.ts`);
|
|
161
|
+
const local = `m${String(id).padStart(digits, '0')}`;
|
|
162
|
+
await apply(root, [
|
|
163
|
+
{
|
|
164
|
+
path,
|
|
165
|
+
before: undefined,
|
|
166
|
+
after: migrationText(id, name, flags.transaction === true),
|
|
167
|
+
},
|
|
168
|
+
], [migrationsEdit(ts, index, file, before, list, local, `./${base}.js`)], flags['dry-run'] === true);
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=migration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.js","sourceRoot":"","sources":["../src/migration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAEL,aAAa,EACb,cAAc,EACd,KAAK,GACN,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,cAAc,EAAmB,MAAM,iBAAiB,CAAC;AAElE,MAAM,CAAC,MAAM,cAAc,GACzB,6DAA6D,CAAC;AAEhE,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,cAAc;;;;;;;;CAQ7C,CAAC;AAEF,MAAM,OAAO,GAAG;IACd,WAAW,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;IAChC,SAAS,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;IAC9B,IAAI,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,EAAE;CAC7B,CAAC;AAEX,4EAA4E;AAC5E,MAAM,MAAM,GAAG,CAAC,CAAC;AAEjB,kFAAkF;AAClF,SAAS,QAAQ,CACf,EAAc,EACd,MAAqB;IAErB,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C,EAAE,CAAC,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAC;IACrD,MAAM,KAAK,CACT,IAAI,EACJ;QACE;YACE,IAAI;YACJ,MAAM,EAAE,SAAS;YACjB,KAAK,EAAE,aAAa,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,WAAW,KAAK,IAAI,CAAC;SAC3D;KACF,EACD,CAAC,cAAc,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,IAAI,KAAK,CAAC,CAAC,EACtE,KAAK,CAAC,SAAS,CAAC,KAAK,IAAI,CAC1B,CAAC;AACJ,CAAC"}
|
package/dist/names.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** What a slug becomes: its file name, its export and its events' prefix. */
|
|
2
|
+
export type Names = {
|
|
3
|
+
readonly slug: string;
|
|
4
|
+
/** Kebab case, without an extension: `deal-notes`. */
|
|
5
|
+
readonly file: string;
|
|
6
|
+
/** Pascal case: `DealNotes`. */
|
|
7
|
+
readonly exportName: string;
|
|
8
|
+
/** Camel case, the events' prefix: `dealNotes`. */
|
|
9
|
+
readonly camel: string;
|
|
10
|
+
};
|
|
11
|
+
/** The names of a slug, or a refusal when the slug breaks the rule. */
|
|
12
|
+
export declare function namesOf(slug: string): Names;
|
|
13
|
+
/** Why `name` cannot be a field, such as the tenant field, or undefined when it can. */
|
|
14
|
+
export declare function fieldProblem(name: string): string | undefined;
|
|
15
|
+
//# sourceMappingURL=names.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"names.d.ts","sourceRoot":"","sources":["../src/names.ts"],"names":[],"mappings":"AAGA,6EAA6E;AAC7E,MAAM,MAAM,KAAK,GAAG;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,sDAAsD;IACtD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,gCAAgC;IAChC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,mDAAmD;IACnD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAKF,uEAAuE;AACvE,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK,CAkB3C;AAED,wFAAwF;AACxF,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAQ7D"}
|
package/dist/names.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { reservedFields, reservedNames } from '@latticecms/core';
|
|
2
|
+
import { Exit } from './exit.js';
|
|
3
|
+
/** Lower snake case: a MongoDB collection, a URL segment and a label. */
|
|
4
|
+
const slugPattern = /^[a-z][a-z0-9]*(_[a-z0-9]+)*$/;
|
|
5
|
+
/** The names of a slug, or a refusal when the slug breaks the rule. */
|
|
6
|
+
export function namesOf(slug) {
|
|
7
|
+
if (!slugPattern.test(slug)) {
|
|
8
|
+
throw new Exit(`"${slug}" is not a slug: use lower snake case, as deal_notes`, 1);
|
|
9
|
+
}
|
|
10
|
+
if (reservedNames.some((name) => name === slug)) {
|
|
11
|
+
throw new Exit(`"${slug}" is reserved: a ts-pf implementer key`, 1);
|
|
12
|
+
}
|
|
13
|
+
const words = slug.split('_');
|
|
14
|
+
const upper = (word) => `${word[0]?.toUpperCase()}${word.slice(1)}`;
|
|
15
|
+
return {
|
|
16
|
+
slug,
|
|
17
|
+
file: words.join('-'),
|
|
18
|
+
exportName: words.map(upper).join(''),
|
|
19
|
+
camel: `${words[0]}${words.slice(1).map(upper).join('')}`,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Why `name` cannot be a field, such as the tenant field, or undefined when it can. */
|
|
23
|
+
export function fieldProblem(name) {
|
|
24
|
+
if (reservedFields.some((field) => field === name)) {
|
|
25
|
+
return `"${name}" is a reserved field name`;
|
|
26
|
+
}
|
|
27
|
+
if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(name)) {
|
|
28
|
+
return `"${name}" is not a field name`;
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=names.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"names.js","sourceRoot":"","sources":["../src/names.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAajC,yEAAyE;AACzE,MAAM,WAAW,GAAG,+BAA+B,CAAC;AAEpD,uEAAuE;AACvE,MAAM,UAAU,OAAO,CAAC,IAAY;IAClC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAI,IAAI,CACZ,IAAI,IAAI,sDAAsD,EAC9D,CAAC,CACF,CAAC;IACJ,CAAC;IACD,IAAI,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,IAAI,CAAC,IAAI,IAAI,wCAAwC,EAAE,CAAC,CAAC,CAAC;IACtE,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,KAAK,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IAC5E,OAAO;QACL,IAAI;QACJ,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;QACrB,UAAU,EAAE,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;QACrC,KAAK,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE;KAC1D,CAAC;AACJ,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,YAAY,CAAC,IAAY;IACvC,IAAI,cAAc,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC,EAAE,CAAC;QACnD,OAAO,IAAI,IAAI,4BAA4B,CAAC;IAC9C,CAAC;IACD,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1C,OAAO,IAAI,IAAI,uBAAuB,CAAC;IACzC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Names } from './names.js';
|
|
2
|
+
/** A new collection: a title field to start from, and its tenant and label. */
|
|
3
|
+
export declare function collectionText(names: Names, tenant: string | undefined, label: {
|
|
4
|
+
readonly singular: string;
|
|
5
|
+
readonly plural: string;
|
|
6
|
+
} | undefined): string;
|
|
7
|
+
/** An import of one name for the test, with the specifier that sorts it. */
|
|
8
|
+
export type TestImport = {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly specifier: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The collection's test: what the scaffold made, and nothing it did not.
|
|
14
|
+
* The composition's contract has the slug and its six procedures, which
|
|
15
|
+
* also proves every ref resolves within the app; and the schema parses a
|
|
16
|
+
* document and refuses one without a name. It builds no app, so a ref, a
|
|
17
|
+
* relation, a method or an override anywhere in the app never breaks it.
|
|
18
|
+
* Without a composition export, only the schema test.
|
|
19
|
+
*/
|
|
20
|
+
export declare function testText(names: Names, app: TestImport | undefined, sort: (a: string, b: string) => number): string;
|
|
21
|
+
//# sourceMappingURL=templates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.d.ts","sourceRoot":"","sources":["../src/templates.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAKxC,+EAA+E;AAC/E,wBAAgB,cAAc,CAC5B,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,KAAK,EAAE;IAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,GACxE,MAAM,CAgCR;AAED,4EAA4E;AAC5E,MAAM,MAAM,UAAU,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AAE/E;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CACtB,KAAK,EAAE,KAAK,EACZ,GAAG,EAAE,UAAU,GAAG,SAAS,EAC3B,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,GACrC,MAAM,CA2CR"}
|