@latticecms/cli 0.0.0 → 0.1.1
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/README.md +138 -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 +61 -2
- 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/src/templates.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { call, objectEntry } from './layout.js';
|
|
2
|
+
import type { Names } from './names.js';
|
|
3
|
+
|
|
4
|
+
/** A string in single quotes. Labels hold only letters, digits, spaces and hyphens. */
|
|
5
|
+
const quote = (text: string) => `'${text}'`;
|
|
6
|
+
|
|
7
|
+
/** A new collection: a title field to start from, and its tenant and label. */
|
|
8
|
+
export function collectionText(
|
|
9
|
+
names: Names,
|
|
10
|
+
tenant: string | undefined,
|
|
11
|
+
label: { readonly singular: string; readonly plural: string } | undefined,
|
|
12
|
+
): string {
|
|
13
|
+
return [
|
|
14
|
+
"import { collection } from '@latticecms/core';",
|
|
15
|
+
"import { z } from 'zod';",
|
|
16
|
+
'',
|
|
17
|
+
...call(
|
|
18
|
+
'',
|
|
19
|
+
`export const ${names.exportName} = collection`,
|
|
20
|
+
[
|
|
21
|
+
quote(names.slug),
|
|
22
|
+
{
|
|
23
|
+
expanded: true,
|
|
24
|
+
lines: (indent) => [
|
|
25
|
+
`${indent}schema: z.object({`,
|
|
26
|
+
`${indent} name: z.string().min(1),`,
|
|
27
|
+
`${indent}}),`,
|
|
28
|
+
...(tenant === undefined
|
|
29
|
+
? []
|
|
30
|
+
: [`${indent}tenant: ${quote(tenant)},`]),
|
|
31
|
+
...(label === undefined
|
|
32
|
+
? []
|
|
33
|
+
: objectEntry(indent, 'label', [
|
|
34
|
+
`singular: ${quote(label.singular)}`,
|
|
35
|
+
`plural: ${quote(label.plural)}`,
|
|
36
|
+
])),
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
';',
|
|
41
|
+
),
|
|
42
|
+
'',
|
|
43
|
+
].join('\n');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** An import of one name for the test, with the specifier that sorts it. */
|
|
47
|
+
export type TestImport = { readonly name: string; readonly specifier: string };
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The collection's test: what the scaffold made, and nothing it did not.
|
|
51
|
+
* The composition's contract has the slug and its six procedures, which
|
|
52
|
+
* also proves every ref resolves within the app; and the schema parses a
|
|
53
|
+
* document and refuses one without a name. It builds no app, so a ref, a
|
|
54
|
+
* relation, a method or an override anywhere in the app never breaks it.
|
|
55
|
+
* Without a composition export, only the schema test.
|
|
56
|
+
*/
|
|
57
|
+
export function testText(
|
|
58
|
+
names: Names,
|
|
59
|
+
app: TestImport | undefined,
|
|
60
|
+
sort: (a: string, b: string) => number,
|
|
61
|
+
): string {
|
|
62
|
+
const local = [...(app === undefined ? [] : [app])]
|
|
63
|
+
.concat({ name: names.exportName, specifier: `./${names.file}.js` })
|
|
64
|
+
.sort((a, b) => sort(a.specifier, b.specifier));
|
|
65
|
+
const schema = `${names.exportName}.schema`;
|
|
66
|
+
return [
|
|
67
|
+
"import { describe, expect, it } from 'vitest';",
|
|
68
|
+
...local.map(
|
|
69
|
+
({ name, specifier }) => `import { ${name} } from '${specifier}';`,
|
|
70
|
+
),
|
|
71
|
+
'',
|
|
72
|
+
"// What the scaffold made. Test behavior through the app's own setup.",
|
|
73
|
+
`describe(${quote(names.slug)}, () => {`,
|
|
74
|
+
...(app === undefined
|
|
75
|
+
? []
|
|
76
|
+
: [
|
|
77
|
+
" it('is in the app, with its operations', () => {",
|
|
78
|
+
" const operations = ['find', 'count', 'get', 'create', 'update', 'delete'];",
|
|
79
|
+
...call(
|
|
80
|
+
' ',
|
|
81
|
+
'const keys = Object.keys',
|
|
82
|
+
[`${app.name}.contract.${names.slug}`],
|
|
83
|
+
';',
|
|
84
|
+
),
|
|
85
|
+
' expect(keys).toEqual(expect.arrayContaining(operations));',
|
|
86
|
+
' });',
|
|
87
|
+
'',
|
|
88
|
+
]),
|
|
89
|
+
" it('parses a document, and refuses one without a name', () => {",
|
|
90
|
+
...call(
|
|
91
|
+
' ',
|
|
92
|
+
`const parsed = ${schema}.parse`,
|
|
93
|
+
[{ expanded: false, lines: (indent) => [`${indent}name: 'First',`] }],
|
|
94
|
+
';',
|
|
95
|
+
),
|
|
96
|
+
" expect(parsed).toMatchObject({ name: 'First' });",
|
|
97
|
+
...call(' ', `const refused = ${schema}.safeParse`, ['{}'], ';'),
|
|
98
|
+
' const paths = refused.error?.issues.map((issue) => issue.path);',
|
|
99
|
+
" expect(paths).toEqual([['name']]);",
|
|
100
|
+
' });',
|
|
101
|
+
'});',
|
|
102
|
+
'',
|
|
103
|
+
].join('\n');
|
|
104
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import type * as TS from 'typescript';
|
|
4
|
+
import { Exit } from './exit.js';
|
|
5
|
+
|
|
6
|
+
export type TypeScript = typeof TS;
|
|
7
|
+
|
|
8
|
+
/** The oldest TypeScript the CLI's peer range takes. */
|
|
9
|
+
const oldest = { major: 5, minor: 8 };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The first major the CLI cannot use. TypeScript 7 is the native compiler:
|
|
13
|
+
* its package has no JavaScript API, so nothing can parse a file with it.
|
|
14
|
+
*/
|
|
15
|
+
const native = 7;
|
|
16
|
+
|
|
17
|
+
// The guard checks the function the CLI calls first. A package named
|
|
18
|
+
// typescript that has it, at a version in the range, is taken for the
|
|
19
|
+
// compiler.
|
|
20
|
+
function isTypeScript(value: unknown): value is TypeScript {
|
|
21
|
+
return (
|
|
22
|
+
typeof value === 'object' &&
|
|
23
|
+
value !== null &&
|
|
24
|
+
'createSourceFile' in value &&
|
|
25
|
+
typeof value.createSourceFile === 'function'
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The `major.minor` a typescript package reports, if it reports one. */
|
|
30
|
+
function versionOf(value: unknown): string | undefined {
|
|
31
|
+
return typeof value === 'object' &&
|
|
32
|
+
value !== null &&
|
|
33
|
+
'versionMajorMinor' in value &&
|
|
34
|
+
typeof value.versionMajorMinor === 'string'
|
|
35
|
+
? value.versionMajorMinor
|
|
36
|
+
: undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The app's own TypeScript, resolved from its directory, so the CLI parses
|
|
41
|
+
* the app's code with the compiler the app builds it with. Older than 5.8
|
|
42
|
+
* is refused, and so is 7, which has no JavaScript API.
|
|
43
|
+
*/
|
|
44
|
+
export function loadTypeScript(root: string): TypeScript {
|
|
45
|
+
const require = createRequire(join(root, 'package.json'));
|
|
46
|
+
let loaded: unknown;
|
|
47
|
+
try {
|
|
48
|
+
loaded = require('typescript');
|
|
49
|
+
} catch {
|
|
50
|
+
throw new Exit(
|
|
51
|
+
`typescript is not installed in ${root}: run npm install --save-dev typescript@^6`,
|
|
52
|
+
1,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
const version = versionOf(loaded);
|
|
56
|
+
if (version === undefined) {
|
|
57
|
+
throw new Exit(`the typescript package in ${root} is not TypeScript`, 1);
|
|
58
|
+
}
|
|
59
|
+
const [major = 0, minor = 0] = version.split('.').map(Number);
|
|
60
|
+
if (
|
|
61
|
+
major < oldest.major ||
|
|
62
|
+
(major === oldest.major && minor < oldest.minor)
|
|
63
|
+
) {
|
|
64
|
+
throw new Exit(
|
|
65
|
+
`typescript ${version} in ${root} is too old: lattice needs ${oldest.major}.${oldest.minor} or later`,
|
|
66
|
+
1,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (major >= native) {
|
|
70
|
+
throw new Exit(
|
|
71
|
+
`typescript ${version} in ${root} is the native compiler, which has no JavaScript API to parse files with: run npm install --save-dev typescript@^6`,
|
|
72
|
+
1,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (!isTypeScript(loaded)) {
|
|
76
|
+
throw new Exit(`the typescript package in ${root} is not TypeScript`, 1);
|
|
77
|
+
}
|
|
78
|
+
return loaded;
|
|
79
|
+
}
|