lumibase 1.0.0-rc.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/LICENSE +192 -0
- package/README.md +101 -0
- package/bin/lumibase.js +13 -0
- package/dist/index.js +767 -0
- package/dist/lib.d.ts +1 -0
- package/dist/lib.js +2 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
Copyright 2024-present LumiBase Contributors
|
|
181
|
+
|
|
182
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
183
|
+
you may not use this file except in compliance with the License.
|
|
184
|
+
You may obtain a copy of the License at
|
|
185
|
+
|
|
186
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
187
|
+
|
|
188
|
+
Unless required by applicable law or agreed to in writing, software
|
|
189
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
190
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
191
|
+
See the License for the specific language governing permissions and
|
|
192
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# `lumibase`
|
|
2
|
+
|
|
3
|
+
[LumiBase](https://lumibase.dev) in one package — the JS/TS client plus the `lumibase` CLI: generate TypeScript types from a live schema, check that your setup can reach the CMS, scaffold a project.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install lumibase
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createLumiClient, readItems } from 'lumibase';
|
|
11
|
+
|
|
12
|
+
const client = createLumiClient({ url: 'https://api.mysite.lumibase.dev', token, siteId });
|
|
13
|
+
const { data } = await client.request(readItems('articles', { limit: 10 }));
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The library entry re-exports [`@lumibase/sdk`](https://www.npmjs.com/package/@lumibase/sdk) (REST + GraphQL + realtime, isomorphic ESM) — same client, one name to install. The CLI can also be run without installing:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npx lumibase --help
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Commands
|
|
23
|
+
|
|
24
|
+
| Command | What it does |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `lumibase init [name]` | Scaffold a new project (runs `create-lumibase@<same version>` via `npx` / `pnpm dlx` / `yarn dlx` / `bunx`) |
|
|
27
|
+
| `lumibase types` | Generate TypeScript types from a running CMS |
|
|
28
|
+
| `lumibase doctor` | Report resolved configuration and probe connectivity |
|
|
29
|
+
|
|
30
|
+
## Connection settings
|
|
31
|
+
|
|
32
|
+
Resolved with precedence **flag > environment > `lumibase.config.json`**:
|
|
33
|
+
|
|
34
|
+
| Setting | Flag | Environment | Config file |
|
|
35
|
+
| --- | --- | --- | --- |
|
|
36
|
+
| CMS base URL | `--url` | `LUMIBASE_URL` | `url` |
|
|
37
|
+
| Site / tenant id | `--site` | `LUMIBASE_SITE_ID` | `siteId` |
|
|
38
|
+
| Bearer token | `--token` | `LUMIBASE_TOKEN` | — |
|
|
39
|
+
|
|
40
|
+
The token is deliberately **not** readable from the config file — that file is meant to be committed. A `token` key there is rejected with an error rather than silently ignored.
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"url": "https://api.mysite.lumibase.dev",
|
|
45
|
+
"siteId": "site_abc123",
|
|
46
|
+
"typegen": {
|
|
47
|
+
"out": "src/lumibase-types.d.ts",
|
|
48
|
+
"exclude": ["internal_logs"]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The file is looked up from the working directory upward, so it works from any package inside a monorepo.
|
|
54
|
+
|
|
55
|
+
## `lumibase types`
|
|
56
|
+
|
|
57
|
+
Fetches the collection manifest from `GET /api/v1/typegen/schema` and writes TypeScript definitions.
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
lumibase types # -> lumibase-types.d.ts
|
|
61
|
+
lumibase types --out src/lumibase-types.d.ts # custom path (directories are created)
|
|
62
|
+
lumibase types --include articles,authors # subset of collections
|
|
63
|
+
lumibase types --no-branded # plain string ids
|
|
64
|
+
lumibase types --import-from @lumibase/sdk # module the generated file imports from (default: lumibase)
|
|
65
|
+
lumibase types --stdout # print instead of writing
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The generated file imports its helper types from `lumibase` by default — the package you already have installed. Use `--import-from` (or `typegen.importFrom` in the config file) if your project depends on `@lumibase/sdk` directly.
|
|
69
|
+
|
|
70
|
+
Output is **deterministic** — no timestamp, host, or site id in the header — so the file can be committed and verified in CI:
|
|
71
|
+
|
|
72
|
+
```yaml
|
|
73
|
+
- run: npx lumibase types --check
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`--check` exits non-zero when the committed file is missing or stale, and never writes anything.
|
|
77
|
+
|
|
78
|
+
## `lumibase doctor`
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
✔ node v22.22.2
|
|
82
|
+
✔ config /path/to/lumibase.config.json
|
|
83
|
+
✔ url http://localhost:1989 (from lumibase.config.json)
|
|
84
|
+
✔ siteId site_abc123 (from lumibase.config.json)
|
|
85
|
+
✔ token tok_••••••••3f (from environment)
|
|
86
|
+
✔ health reachable — status: healthy
|
|
87
|
+
✔ schema 12 collections readable
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Exits non-zero if any check fails. Tokens are masked — a short prefix is kept so you can tell *which* token was picked up without the value reaching CI logs.
|
|
91
|
+
|
|
92
|
+
## Requirements
|
|
93
|
+
|
|
94
|
+
Node.js 22+.
|
|
95
|
+
|
|
96
|
+
## Links
|
|
97
|
+
|
|
98
|
+
- Documentation — <https://docs.lumibase.dev>
|
|
99
|
+
- Issues — <https://github.com/khuepm/lumibase/issues>
|
|
100
|
+
|
|
101
|
+
Apache-2.0
|
package/bin/lumibase.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Guard BEFORE loading the ESM graph so the message is a plain sentence rather
|
|
3
|
+
// than a syntax/import error from a Node 22+ feature further down the tree.
|
|
4
|
+
const major = Number(process.versions.node.split('.')[0]);
|
|
5
|
+
if (!Number.isFinite(major) || major < 22) {
|
|
6
|
+
console.error(`lumibase requires Node.js 22+. Current: ${process.versions.node}`);
|
|
7
|
+
process.exit(1);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
import('../dist/index.js').catch((err) => {
|
|
11
|
+
console.error(err);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import process5 from "process";
|
|
3
|
+
import pc4 from "picocolors";
|
|
4
|
+
|
|
5
|
+
// src/cli.ts
|
|
6
|
+
import pc3 from "picocolors";
|
|
7
|
+
|
|
8
|
+
// src/commands/doctor.ts
|
|
9
|
+
import process2 from "process";
|
|
10
|
+
import pc2 from "picocolors";
|
|
11
|
+
|
|
12
|
+
// src/errors.ts
|
|
13
|
+
var CliError = class extends Error {
|
|
14
|
+
constructor(message, hint, exitCode = 1) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.hint = hint;
|
|
17
|
+
this.exitCode = exitCode;
|
|
18
|
+
this.name = "CliError";
|
|
19
|
+
}
|
|
20
|
+
hint;
|
|
21
|
+
exitCode;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/api.ts
|
|
25
|
+
var DEFAULT_TIMEOUT_MS = 15e3;
|
|
26
|
+
async function request(connection, path, query = {}, options = {}) {
|
|
27
|
+
const fetcher = options.fetcher ?? globalThis.fetch;
|
|
28
|
+
const target = new URL(path, `${connection.url}/`);
|
|
29
|
+
for (const [key, value] of Object.entries(query)) {
|
|
30
|
+
if (value) target.searchParams.set(key, value);
|
|
31
|
+
}
|
|
32
|
+
const controller = new AbortController();
|
|
33
|
+
const timeout = setTimeout(() => controller.abort(), options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
34
|
+
let response;
|
|
35
|
+
try {
|
|
36
|
+
response = await fetcher(target, {
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: `Bearer ${connection.token}`,
|
|
39
|
+
"X-Lumi-Site": connection.siteId,
|
|
40
|
+
Accept: "application/json"
|
|
41
|
+
},
|
|
42
|
+
signal: controller.signal
|
|
43
|
+
});
|
|
44
|
+
} catch (err) {
|
|
45
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
46
|
+
throw new CliError(`Request to ${target.href} timed out.`);
|
|
47
|
+
}
|
|
48
|
+
throw new CliError(
|
|
49
|
+
`Could not reach ${target.href}: ${err instanceof Error ? err.message : String(err)}`,
|
|
50
|
+
"Is the CMS running, and is --url / LUMIBASE_URL pointing at it?"
|
|
51
|
+
);
|
|
52
|
+
} finally {
|
|
53
|
+
clearTimeout(timeout);
|
|
54
|
+
}
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
throw new CliError(describeHttpError(response.status, target.href), hintForStatus(response.status));
|
|
57
|
+
}
|
|
58
|
+
return response.json();
|
|
59
|
+
}
|
|
60
|
+
function describeHttpError(status, href) {
|
|
61
|
+
if (status === 401) return `Unauthorized (401) from ${href}.`;
|
|
62
|
+
if (status === 403) return `Forbidden (403) from ${href}.`;
|
|
63
|
+
if (status === 404) return `Not found (404) at ${href}.`;
|
|
64
|
+
return `Request to ${href} failed with HTTP ${status}.`;
|
|
65
|
+
}
|
|
66
|
+
function hintForStatus(status) {
|
|
67
|
+
if (status === 401) return "The token is missing, expired, or not valid for this site.";
|
|
68
|
+
if (status === 403) return "The token is valid but lacks permission to read the schema.";
|
|
69
|
+
if (status === 404) {
|
|
70
|
+
return "Check the base URL \u2014 it should point at the CMS root (e.g. http://localhost:1989), not at an endpoint.";
|
|
71
|
+
}
|
|
72
|
+
return void 0;
|
|
73
|
+
}
|
|
74
|
+
async function fetchTypegenManifest(connection, filters = {}, options = {}) {
|
|
75
|
+
const body = await request(
|
|
76
|
+
connection,
|
|
77
|
+
"api/v1/typegen/schema",
|
|
78
|
+
{
|
|
79
|
+
include: filters.include?.join(","),
|
|
80
|
+
exclude: filters.exclude?.join(",")
|
|
81
|
+
},
|
|
82
|
+
options
|
|
83
|
+
);
|
|
84
|
+
const manifest = body?.data;
|
|
85
|
+
if (typeof manifest !== "object" || manifest === null || !Array.isArray(manifest.collections)) {
|
|
86
|
+
throw new CliError(
|
|
87
|
+
"The schema endpoint returned an unexpected payload.",
|
|
88
|
+
"Make sure the CMS is a LumiBase instance and its version supports GET /api/v1/typegen/schema."
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return manifest;
|
|
92
|
+
}
|
|
93
|
+
async function probeHealth(url, options = {}) {
|
|
94
|
+
const fetcher = options.fetcher ?? globalThis.fetch;
|
|
95
|
+
const target = new URL("health", `${url}/`);
|
|
96
|
+
const controller = new AbortController();
|
|
97
|
+
const timeout = setTimeout(() => controller.abort(), options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
98
|
+
try {
|
|
99
|
+
const response = await fetcher(target, { signal: controller.signal });
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
return { ok: false, detail: `HTTP ${response.status} from ${target.href}` };
|
|
102
|
+
}
|
|
103
|
+
const body = await response.json();
|
|
104
|
+
const status = typeof body.status === "string" ? body.status : void 0;
|
|
105
|
+
return {
|
|
106
|
+
ok: status !== "unhealthy",
|
|
107
|
+
status,
|
|
108
|
+
detail: status ? `reachable \u2014 status: ${status}` : `reachable at ${target.href}`
|
|
109
|
+
};
|
|
110
|
+
} catch (err) {
|
|
111
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
112
|
+
return { ok: false, detail: `timed out reaching ${target.href}` };
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
ok: false,
|
|
116
|
+
detail: `unreachable: ${err instanceof Error ? err.message : String(err)}`
|
|
117
|
+
};
|
|
118
|
+
} finally {
|
|
119
|
+
clearTimeout(timeout);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/config.ts
|
|
124
|
+
import { existsSync, readFileSync } from "fs";
|
|
125
|
+
import { dirname, join, resolve } from "path";
|
|
126
|
+
import process from "process";
|
|
127
|
+
|
|
128
|
+
// src/utils/args.ts
|
|
129
|
+
function parseArgs(argv) {
|
|
130
|
+
const result = { _: [] };
|
|
131
|
+
const passthrough = [];
|
|
132
|
+
let afterDoubleDash = false;
|
|
133
|
+
for (let i = 0; i < argv.length; i++) {
|
|
134
|
+
const arg = argv[i];
|
|
135
|
+
if (!arg) continue;
|
|
136
|
+
if (afterDoubleDash) {
|
|
137
|
+
passthrough.push(arg);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (arg === "--") {
|
|
141
|
+
afterDoubleDash = true;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (arg === "-h") {
|
|
145
|
+
result["help"] = true;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (arg === "-v") {
|
|
149
|
+
result["version"] = true;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (arg.startsWith("--")) {
|
|
153
|
+
const raw = arg.slice(2);
|
|
154
|
+
if (raw.startsWith("no-")) {
|
|
155
|
+
result[raw] = true;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const eqIdx = raw.indexOf("=");
|
|
159
|
+
if (eqIdx !== -1) {
|
|
160
|
+
result[raw.slice(0, eqIdx)] = raw.slice(eqIdx + 1);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const next = argv[i + 1];
|
|
164
|
+
if (next && !next.startsWith("-")) {
|
|
165
|
+
result[raw] = next;
|
|
166
|
+
i++;
|
|
167
|
+
} else {
|
|
168
|
+
result[raw] = true;
|
|
169
|
+
}
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
result._.push(arg);
|
|
173
|
+
}
|
|
174
|
+
if (passthrough.length > 0) {
|
|
175
|
+
result._.push(...passthrough);
|
|
176
|
+
}
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
function argvAfterCommand(argv) {
|
|
180
|
+
for (let i = 0; i < argv.length; i++) {
|
|
181
|
+
const arg = argv[i];
|
|
182
|
+
if (!arg) continue;
|
|
183
|
+
if (arg.startsWith("-")) {
|
|
184
|
+
if (arg.startsWith("--") && !arg.includes("=") && !arg.startsWith("--no-")) {
|
|
185
|
+
const next = argv[i + 1];
|
|
186
|
+
if (next && !next.startsWith("-")) i++;
|
|
187
|
+
}
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
return argv.slice(i + 1);
|
|
191
|
+
}
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
function stringFlag(args, name) {
|
|
195
|
+
const value = args[name];
|
|
196
|
+
if (value === void 0) return void 0;
|
|
197
|
+
if (typeof value !== "string") {
|
|
198
|
+
throw new Error(`Flag --${name} needs a value (e.g. --${name}=<value>).`);
|
|
199
|
+
}
|
|
200
|
+
return value;
|
|
201
|
+
}
|
|
202
|
+
function boolFlag(args, name) {
|
|
203
|
+
return args[name] === true || args[name] === "true";
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/config.ts
|
|
207
|
+
var CONFIG_FILENAME = "lumibase.config.json";
|
|
208
|
+
function findConfigFile(startDir) {
|
|
209
|
+
let dir = resolve(startDir);
|
|
210
|
+
for (; ; ) {
|
|
211
|
+
const candidate = join(dir, CONFIG_FILENAME);
|
|
212
|
+
if (existsSync(candidate)) return candidate;
|
|
213
|
+
const parent = dirname(dir);
|
|
214
|
+
if (parent === dir) return void 0;
|
|
215
|
+
dir = parent;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function readConfigFile(path) {
|
|
219
|
+
let raw;
|
|
220
|
+
try {
|
|
221
|
+
raw = readFileSync(path, "utf8");
|
|
222
|
+
} catch (err) {
|
|
223
|
+
throw new CliError(
|
|
224
|
+
`Could not read ${path}: ${err instanceof Error ? err.message : String(err)}`
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
let parsed;
|
|
228
|
+
try {
|
|
229
|
+
parsed = JSON.parse(raw);
|
|
230
|
+
} catch {
|
|
231
|
+
throw new CliError(`${path} is not valid JSON.`);
|
|
232
|
+
}
|
|
233
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
234
|
+
throw new CliError(`${path} must contain a JSON object.`);
|
|
235
|
+
}
|
|
236
|
+
const config = parsed;
|
|
237
|
+
if ("token" in config) {
|
|
238
|
+
throw new CliError(
|
|
239
|
+
`${CONFIG_FILENAME} must not contain a "token".`,
|
|
240
|
+
"Pass the token via the LUMIBASE_TOKEN environment variable or --token instead \u2014 this file is meant to be committed."
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
url: typeof config["url"] === "string" ? config["url"] : void 0,
|
|
245
|
+
siteId: typeof config["siteId"] === "string" ? config["siteId"] : void 0,
|
|
246
|
+
typegen: readTypegenSection(config["typegen"])
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function readTypegenSection(value) {
|
|
250
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
251
|
+
const section = value;
|
|
252
|
+
return {
|
|
253
|
+
out: typeof section["out"] === "string" ? section["out"] : void 0,
|
|
254
|
+
include: stringArray(section["include"]),
|
|
255
|
+
exclude: stringArray(section["exclude"]),
|
|
256
|
+
branded: typeof section["branded"] === "boolean" ? section["branded"] : void 0,
|
|
257
|
+
importFrom: typeof section["importFrom"] === "string" ? section["importFrom"] : void 0
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
function stringArray(value) {
|
|
261
|
+
if (!Array.isArray(value)) return void 0;
|
|
262
|
+
const items = value.filter((entry) => typeof entry === "string");
|
|
263
|
+
return items.length > 0 ? items : void 0;
|
|
264
|
+
}
|
|
265
|
+
function resolveConfig(args, options = {}) {
|
|
266
|
+
const cwd = options.cwd ?? process.cwd();
|
|
267
|
+
const env = options.env ?? process.env;
|
|
268
|
+
const configPath = findConfigFile(cwd);
|
|
269
|
+
const file = configPath ? readConfigFile(configPath) : {};
|
|
270
|
+
const url = pick(stringFlag(args, "url"), env["LUMIBASE_URL"], file.url);
|
|
271
|
+
const siteId = pick(stringFlag(args, "site"), env["LUMIBASE_SITE_ID"], file.siteId);
|
|
272
|
+
const token = pick(stringFlag(args, "token"), env["LUMIBASE_TOKEN"], void 0);
|
|
273
|
+
return {
|
|
274
|
+
url: url.value ? stripTrailingSlash(url.value) : void 0,
|
|
275
|
+
siteId: siteId.value,
|
|
276
|
+
token: token.value,
|
|
277
|
+
sources: { url: url.source, siteId: siteId.source, token: token.source },
|
|
278
|
+
configPath,
|
|
279
|
+
typegen: file.typegen ?? {}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function pick(flag, envValue, fileValue) {
|
|
283
|
+
if (flag) return { value: flag, source: "flag" };
|
|
284
|
+
if (envValue) return { value: envValue, source: "env" };
|
|
285
|
+
if (fileValue) return { value: fileValue, source: "file" };
|
|
286
|
+
return { source: "missing" };
|
|
287
|
+
}
|
|
288
|
+
function stripTrailingSlash(url) {
|
|
289
|
+
return url.endsWith("/") ? url.slice(0, -1) : url;
|
|
290
|
+
}
|
|
291
|
+
function requireConnection(config) {
|
|
292
|
+
const missing = [];
|
|
293
|
+
if (!config.url) missing.push("url (--url / LUMIBASE_URL)");
|
|
294
|
+
if (!config.siteId) missing.push("siteId (--site / LUMIBASE_SITE_ID)");
|
|
295
|
+
if (!config.token) missing.push("token (--token / LUMIBASE_TOKEN)");
|
|
296
|
+
if (missing.length > 0) {
|
|
297
|
+
throw new CliError(
|
|
298
|
+
`Missing connection settings: ${missing.join(", ")}.`,
|
|
299
|
+
`Set them in the environment, or put "url" and "siteId" in ${CONFIG_FILENAME}. Run \`lumibase doctor\` to see what was resolved.`
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/utils/log.ts
|
|
305
|
+
import pc from "picocolors";
|
|
306
|
+
var log = {
|
|
307
|
+
plain(message = "") {
|
|
308
|
+
console.log(message);
|
|
309
|
+
},
|
|
310
|
+
info(message) {
|
|
311
|
+
console.log(`${pc.cyan("\u2022")} ${message}`);
|
|
312
|
+
},
|
|
313
|
+
success(message) {
|
|
314
|
+
console.log(`${pc.green("\u2714")} ${message}`);
|
|
315
|
+
},
|
|
316
|
+
warn(message) {
|
|
317
|
+
console.warn(`${pc.yellow("!")} ${message}`);
|
|
318
|
+
},
|
|
319
|
+
error(message) {
|
|
320
|
+
console.error(`${pc.red("\u2716")} ${message}`);
|
|
321
|
+
},
|
|
322
|
+
dim(message) {
|
|
323
|
+
console.log(pc.dim(message));
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
function maskSecret(secret) {
|
|
327
|
+
if (secret.length <= 8) return "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022";
|
|
328
|
+
return `${secret.slice(0, 4)}${"\u2022".repeat(8)}${secret.slice(-2)}`;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// src/commands/doctor.ts
|
|
332
|
+
var MIN_NODE_MAJOR = 22;
|
|
333
|
+
function describeSource(source) {
|
|
334
|
+
switch (source) {
|
|
335
|
+
case "flag":
|
|
336
|
+
return "from flag";
|
|
337
|
+
case "env":
|
|
338
|
+
return "from environment";
|
|
339
|
+
case "file":
|
|
340
|
+
return `from ${CONFIG_FILENAME}`;
|
|
341
|
+
case "missing":
|
|
342
|
+
return "not set";
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
function printCheck(check) {
|
|
346
|
+
const mark = check.status === "ok" ? pc2.green("\u2714") : check.status === "warn" ? pc2.yellow("!") : pc2.red("\u2716");
|
|
347
|
+
console.log(`${mark} ${check.label.padEnd(12)} ${pc2.dim(check.detail)}`);
|
|
348
|
+
}
|
|
349
|
+
async function doctorCommand(args, options = {}) {
|
|
350
|
+
const cwd = options.cwd ?? process2.cwd();
|
|
351
|
+
const config = resolveConfig(args, { cwd, env: options.env });
|
|
352
|
+
const checks = [];
|
|
353
|
+
const nodeVersion = options.nodeVersion ?? process2.versions.node;
|
|
354
|
+
const nodeMajor = Number(nodeVersion.split(".")[0]);
|
|
355
|
+
checks.push({
|
|
356
|
+
label: "node",
|
|
357
|
+
status: Number.isFinite(nodeMajor) && nodeMajor >= MIN_NODE_MAJOR ? "ok" : "fail",
|
|
358
|
+
detail: Number.isFinite(nodeMajor) && nodeMajor >= MIN_NODE_MAJOR ? `v${nodeVersion}` : `v${nodeVersion} \u2014 LumiBase needs Node ${MIN_NODE_MAJOR}+`
|
|
359
|
+
});
|
|
360
|
+
checks.push({
|
|
361
|
+
label: "config",
|
|
362
|
+
status: config.configPath ? "ok" : "warn",
|
|
363
|
+
detail: config.configPath ?? `no ${CONFIG_FILENAME} found \u2014 using flags and environment only`
|
|
364
|
+
});
|
|
365
|
+
checks.push({
|
|
366
|
+
label: "url",
|
|
367
|
+
status: config.url ? "ok" : "fail",
|
|
368
|
+
detail: config.url ? `${config.url} (${describeSource(config.sources.url)})` : "not set \u2014 pass --url or set LUMIBASE_URL"
|
|
369
|
+
});
|
|
370
|
+
checks.push({
|
|
371
|
+
label: "siteId",
|
|
372
|
+
status: config.siteId ? "ok" : "fail",
|
|
373
|
+
detail: config.siteId ? `${config.siteId} (${describeSource(config.sources.siteId)})` : "not set \u2014 pass --site or set LUMIBASE_SITE_ID"
|
|
374
|
+
});
|
|
375
|
+
checks.push({
|
|
376
|
+
label: "token",
|
|
377
|
+
status: config.token ? "ok" : "fail",
|
|
378
|
+
detail: config.token ? `${maskSecret(config.token)} (${describeSource(config.sources.token)})` : "not set \u2014 pass --token or set LUMIBASE_TOKEN"
|
|
379
|
+
});
|
|
380
|
+
if (config.url) {
|
|
381
|
+
const health = await probeHealth(config.url, options.request);
|
|
382
|
+
checks.push({
|
|
383
|
+
label: "health",
|
|
384
|
+
status: health.ok ? health.status === "degraded" ? "warn" : "ok" : "fail",
|
|
385
|
+
detail: health.detail
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
if (config.url && config.siteId && config.token) {
|
|
389
|
+
try {
|
|
390
|
+
const manifest = await fetchTypegenManifest(
|
|
391
|
+
{ url: config.url, siteId: config.siteId, token: config.token },
|
|
392
|
+
{},
|
|
393
|
+
options.request
|
|
394
|
+
);
|
|
395
|
+
checks.push({
|
|
396
|
+
label: "schema",
|
|
397
|
+
status: "ok",
|
|
398
|
+
detail: `${manifest.collections.length} collections readable`
|
|
399
|
+
});
|
|
400
|
+
} catch (err) {
|
|
401
|
+
checks.push({
|
|
402
|
+
label: "schema",
|
|
403
|
+
status: "fail",
|
|
404
|
+
detail: err instanceof CliError ? err.message : String(err)
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
log.plain();
|
|
409
|
+
for (const check of checks) printCheck(check);
|
|
410
|
+
log.plain();
|
|
411
|
+
const failures = checks.filter((check) => check.status === "fail");
|
|
412
|
+
if (failures.length > 0) {
|
|
413
|
+
log.error(`${failures.length} check${failures.length === 1 ? "" : "s"} failed.`);
|
|
414
|
+
return 1;
|
|
415
|
+
}
|
|
416
|
+
log.success("Everything looks good.");
|
|
417
|
+
return 0;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// src/commands/init.ts
|
|
421
|
+
import { spawnSync } from "child_process";
|
|
422
|
+
import process3 from "process";
|
|
423
|
+
|
|
424
|
+
// src/version.ts
|
|
425
|
+
import { createRequire } from "module";
|
|
426
|
+
function readVersion(requireFrom = createRequire(import.meta.url)) {
|
|
427
|
+
const manifest = requireFrom("../package.json");
|
|
428
|
+
return manifest.version ?? "0.0.0";
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// src/commands/init.ts
|
|
432
|
+
function resolveScaffoldCommand(version, userAgent = process3.env["npm_config_user_agent"] ?? "") {
|
|
433
|
+
const pkg = `create-lumibase@${version}`;
|
|
434
|
+
const [name = "", ver = ""] = userAgent.split(" ")[0]?.split("/") ?? [];
|
|
435
|
+
const major = Number(ver.split(".")[0]);
|
|
436
|
+
switch (name) {
|
|
437
|
+
case "pnpm":
|
|
438
|
+
return { command: "pnpm", args: ["dlx", pkg] };
|
|
439
|
+
case "yarn":
|
|
440
|
+
if (major >= 2) return { command: "yarn", args: ["dlx", pkg] };
|
|
441
|
+
return { command: "npx", args: ["--yes", pkg] };
|
|
442
|
+
case "bun":
|
|
443
|
+
return { command: "bunx", args: [pkg] };
|
|
444
|
+
default:
|
|
445
|
+
return { command: "npx", args: ["--yes", pkg] };
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function initCommand(argv, options = {}) {
|
|
449
|
+
const scaffold = resolveScaffoldCommand(
|
|
450
|
+
options.version ?? readVersion(),
|
|
451
|
+
options.userAgent
|
|
452
|
+
);
|
|
453
|
+
const run2 = options.run ?? ((command, args) => {
|
|
454
|
+
const result = spawnSync(command, args, {
|
|
455
|
+
stdio: "inherit",
|
|
456
|
+
// npx/pnpm/yarn/bunx are `.cmd` shims on Windows; a shell resolves them.
|
|
457
|
+
shell: process3.platform === "win32"
|
|
458
|
+
});
|
|
459
|
+
if (result.error) {
|
|
460
|
+
throw new CliError(
|
|
461
|
+
`Failed to run the scaffolder: ${result.error.message}`,
|
|
462
|
+
"Run `npm create lumibase@latest` directly."
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
return result.status ?? 1;
|
|
466
|
+
});
|
|
467
|
+
return run2(scaffold.command, [...scaffold.args, ...argv]);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// src/commands/types.ts
|
|
471
|
+
import { existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, writeFileSync } from "fs";
|
|
472
|
+
import { dirname as dirname2, relative, resolve as resolve2 } from "path";
|
|
473
|
+
import process4 from "process";
|
|
474
|
+
|
|
475
|
+
// ../sdk/src/typegen/index.ts
|
|
476
|
+
var DEFAULT_IMPORT_SOURCE = "@lumibase/sdk";
|
|
477
|
+
function generateTypes(manifest, options = {}) {
|
|
478
|
+
const { format = "single", branded = true, importSource = DEFAULT_IMPORT_SOURCE } = options;
|
|
479
|
+
if (format === "single") {
|
|
480
|
+
return generateSingleFile(manifest, branded, importSource);
|
|
481
|
+
}
|
|
482
|
+
return generatePerCollection(manifest, branded, importSource);
|
|
483
|
+
}
|
|
484
|
+
function generateSingleFile(manifest, branded, importSource) {
|
|
485
|
+
const imports = [
|
|
486
|
+
`import type { ID, Locale } from '${importSource}';`,
|
|
487
|
+
branded ? `import type { Brand } from '${importSource}';` : ""
|
|
488
|
+
].filter(Boolean).join("\n");
|
|
489
|
+
const collectionInterfaces = manifest.collections.map((coll) => {
|
|
490
|
+
const base = generateCollectionInterface(coll, branded);
|
|
491
|
+
const expanded = generateExpandedType(coll);
|
|
492
|
+
return [base, expanded].filter(Boolean).join("\n\n");
|
|
493
|
+
}).join("\n\n");
|
|
494
|
+
const collectionsMap = `export interface LumibaseCollections {
|
|
495
|
+
${manifest.collections.map((c) => ` ${c.name}: ${capitalize(c.name)};`).join("\n")}
|
|
496
|
+
}`;
|
|
497
|
+
const schemaType = "export type LumibaseSchema = LumibaseCollections;";
|
|
498
|
+
return `${imports}
|
|
499
|
+
|
|
500
|
+
${collectionInterfaces}
|
|
501
|
+
|
|
502
|
+
${collectionsMap}
|
|
503
|
+
|
|
504
|
+
${schemaType}`;
|
|
505
|
+
}
|
|
506
|
+
function generatePerCollection(manifest, branded, importSource) {
|
|
507
|
+
return generateSingleFile(manifest, branded, importSource);
|
|
508
|
+
}
|
|
509
|
+
function generateCollectionInterface(coll, branded) {
|
|
510
|
+
const fields = coll.fields.map((f) => {
|
|
511
|
+
const tsType = mapFieldTypeToTs(f, coll, branded);
|
|
512
|
+
const optional = f.required ? "" : "?";
|
|
513
|
+
const readonly = f.readonly || f.generated ? "readonly " : "";
|
|
514
|
+
return ` ${readonly}${f.name}${optional}: ${tsType};`;
|
|
515
|
+
}).join("\n");
|
|
516
|
+
return `export interface ${capitalize(coll.name)} {
|
|
517
|
+
${fields}
|
|
518
|
+
}`;
|
|
519
|
+
}
|
|
520
|
+
function generateExpandedType(coll) {
|
|
521
|
+
const relations = coll.relations ?? [];
|
|
522
|
+
if (relations.length === 0) {
|
|
523
|
+
return `export type ${capitalize(coll.name)}Expanded = ${capitalize(coll.name)};`;
|
|
524
|
+
}
|
|
525
|
+
const omittedKeys = relations.map((relation) => quoteProperty(relation.field)).join(" | ");
|
|
526
|
+
const fields = relations.map((relation) => ` ${relation.field}?: ${expandedRelationType(relation)};`).join("\n");
|
|
527
|
+
return `export type ${capitalize(coll.name)}Expanded = Omit<${capitalize(coll.name)}, ${omittedKeys}> & {
|
|
528
|
+
${fields}
|
|
529
|
+
};`;
|
|
530
|
+
}
|
|
531
|
+
function expandedRelationType(relation) {
|
|
532
|
+
if (relation.kind === "m2o") {
|
|
533
|
+
return `${capitalize(relation.target)} | ${capitalize(relation.target)}Expanded | null`;
|
|
534
|
+
}
|
|
535
|
+
if (relation.kind === "o2m" || relation.kind === "m2m") {
|
|
536
|
+
return `Array<${capitalize(relation.target)} | ${capitalize(relation.target)}Expanded>`;
|
|
537
|
+
}
|
|
538
|
+
return `Array<{ collection: string; item: unknown }>`;
|
|
539
|
+
}
|
|
540
|
+
function mapFieldTypeToTs(field, coll, branded) {
|
|
541
|
+
if (field.kind === "m2o") {
|
|
542
|
+
return nullable(field, mapScalarFieldType(field, branded));
|
|
543
|
+
}
|
|
544
|
+
if (field.kind === "o2m" || field.kind === "m2m") {
|
|
545
|
+
return `${capitalize(field.target || "unknown")}[]`;
|
|
546
|
+
}
|
|
547
|
+
if (field.kind === "m2a") {
|
|
548
|
+
return `Array<{ collection: string; item: unknown }>`;
|
|
549
|
+
}
|
|
550
|
+
if (field.enum && field.enum.length > 0) {
|
|
551
|
+
return nullable(field, field.enum.map((v) => JSON.stringify(v)).join(" | "));
|
|
552
|
+
}
|
|
553
|
+
if (field.primaryKey) {
|
|
554
|
+
return nullable(
|
|
555
|
+
field,
|
|
556
|
+
mapPrimaryKeyType(coll.primaryKeyType ?? "nanoid", branded, field.branded)
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
const tsType = mapScalarFieldType(field, branded);
|
|
560
|
+
return nullable(field, field.encrypted ? `${tsType} | '***'` : tsType);
|
|
561
|
+
}
|
|
562
|
+
function mapScalarFieldType(field, branded) {
|
|
563
|
+
const typeMap = {
|
|
564
|
+
string: "string",
|
|
565
|
+
text: "string",
|
|
566
|
+
hash: "string",
|
|
567
|
+
csv: "string",
|
|
568
|
+
integer: "number",
|
|
569
|
+
bigInteger: "number",
|
|
570
|
+
decimal: "number",
|
|
571
|
+
boolean: "boolean",
|
|
572
|
+
json: "unknown",
|
|
573
|
+
uuid: mapPrimaryKeyType("uuid", branded, field.branded),
|
|
574
|
+
date: "string",
|
|
575
|
+
datetime: "string",
|
|
576
|
+
time: "string",
|
|
577
|
+
timestamp: "string",
|
|
578
|
+
geometry: "GeoJSON.Geometry"
|
|
579
|
+
};
|
|
580
|
+
return typeMap[field.type] || "unknown";
|
|
581
|
+
}
|
|
582
|
+
function nullable(field, tsType) {
|
|
583
|
+
if (!field.required || field.nullable) {
|
|
584
|
+
return `${tsType} | null`;
|
|
585
|
+
}
|
|
586
|
+
return tsType;
|
|
587
|
+
}
|
|
588
|
+
function mapPrimaryKeyType(primaryKeyType, branded, brand) {
|
|
589
|
+
if (primaryKeyType === "integer" || primaryKeyType === "bigInteger") return "number";
|
|
590
|
+
if (branded) return `Brand<'${brand || "ID"}', string>`;
|
|
591
|
+
return "ID";
|
|
592
|
+
}
|
|
593
|
+
function capitalize(str) {
|
|
594
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
595
|
+
}
|
|
596
|
+
function quoteProperty(name) {
|
|
597
|
+
return JSON.stringify(name);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// src/commands/types.ts
|
|
601
|
+
var DEFAULT_OUT = "lumibase-types.d.ts";
|
|
602
|
+
var DEFAULT_IMPORT_FROM = "lumibase";
|
|
603
|
+
var BANNER = [
|
|
604
|
+
"// Generated by `lumibase types` \u2014 do not edit by hand.",
|
|
605
|
+
"// Re-run the command after changing collections or fields."
|
|
606
|
+
].join("\n");
|
|
607
|
+
function renderTypes(manifest, options) {
|
|
608
|
+
const { branded, importFrom = DEFAULT_IMPORT_FROM } = options;
|
|
609
|
+
const body = generateTypes(manifest, { format: "single", branded, importSource: importFrom });
|
|
610
|
+
return `${BANNER}
|
|
611
|
+
|
|
612
|
+
${body.trimEnd()}
|
|
613
|
+
`;
|
|
614
|
+
}
|
|
615
|
+
function splitList(value) {
|
|
616
|
+
if (!value) return void 0;
|
|
617
|
+
const items = value.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
618
|
+
return items.length > 0 ? items : void 0;
|
|
619
|
+
}
|
|
620
|
+
async function typesCommand(args, options = {}) {
|
|
621
|
+
const cwd = options.cwd ?? process4.cwd();
|
|
622
|
+
const config = resolveConfig(args, { cwd, env: options.env });
|
|
623
|
+
requireConnection(config);
|
|
624
|
+
const check = boolFlag(args, "check");
|
|
625
|
+
const toStdout = boolFlag(args, "stdout");
|
|
626
|
+
const branded = boolFlag(args, "no-branded") ? false : config.typegen.branded ?? true;
|
|
627
|
+
const importFrom = stringFlag(args, "import-from") ?? config.typegen.importFrom ?? DEFAULT_IMPORT_FROM;
|
|
628
|
+
const outPath = resolve2(
|
|
629
|
+
cwd,
|
|
630
|
+
stringFlag(args, "out") ?? config.typegen.out ?? DEFAULT_OUT
|
|
631
|
+
);
|
|
632
|
+
const manifest = await fetchTypegenManifest(
|
|
633
|
+
{ url: config.url, siteId: config.siteId, token: config.token },
|
|
634
|
+
{
|
|
635
|
+
include: splitList(stringFlag(args, "include")) ?? config.typegen.include,
|
|
636
|
+
exclude: splitList(stringFlag(args, "exclude")) ?? config.typegen.exclude
|
|
637
|
+
},
|
|
638
|
+
options.request
|
|
639
|
+
);
|
|
640
|
+
const generated = renderTypes(manifest, { branded, importFrom });
|
|
641
|
+
const collectionCount = manifest.collections.length;
|
|
642
|
+
if (toStdout) {
|
|
643
|
+
process4.stdout.write(generated);
|
|
644
|
+
return 0;
|
|
645
|
+
}
|
|
646
|
+
const displayPath = relative(cwd, outPath) || outPath;
|
|
647
|
+
const existing = existsSync2(outPath) ? readFileSync2(outPath, "utf8") : void 0;
|
|
648
|
+
if (check) {
|
|
649
|
+
if (existing === void 0) {
|
|
650
|
+
throw new CliError(
|
|
651
|
+
`${displayPath} does not exist.`,
|
|
652
|
+
"Run `lumibase types` and commit the result."
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
if (existing !== generated) {
|
|
656
|
+
throw new CliError(
|
|
657
|
+
`${displayPath} is out of date with the live schema.`,
|
|
658
|
+
"Run `lumibase types` and commit the result."
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
log.success(`${displayPath} is up to date (${collectionCount} collections).`);
|
|
662
|
+
return 0;
|
|
663
|
+
}
|
|
664
|
+
if (existing === generated) {
|
|
665
|
+
log.success(`${displayPath} already up to date (${collectionCount} collections).`);
|
|
666
|
+
return 0;
|
|
667
|
+
}
|
|
668
|
+
mkdirSync(dirname2(outPath), { recursive: true });
|
|
669
|
+
writeFileSync(outPath, generated, "utf8");
|
|
670
|
+
log.success(`Wrote ${displayPath} \u2014 ${collectionCount} collections.`);
|
|
671
|
+
log.dim(` Generated types import from ${importFrom} \u2014 install it in this project.`);
|
|
672
|
+
return 0;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// src/cli.ts
|
|
676
|
+
var HELP = `
|
|
677
|
+
${pc3.bold(pc3.cyan("lumibase"))} ${pc3.dim("\u2014 the LumiBase CLI")}
|
|
678
|
+
|
|
679
|
+
${pc3.bold("Usage")}
|
|
680
|
+
lumibase <command> [options]
|
|
681
|
+
|
|
682
|
+
${pc3.bold("Commands")}
|
|
683
|
+
init [name] Scaffold a new LumiBase project
|
|
684
|
+
types Generate TypeScript types from a live schema
|
|
685
|
+
doctor Check configuration and connectivity
|
|
686
|
+
|
|
687
|
+
${pc3.bold("Connection")}
|
|
688
|
+
Resolved with precedence flag > environment > ${pc3.cyan("lumibase.config.json")}.
|
|
689
|
+
The token is never read from the config file.
|
|
690
|
+
|
|
691
|
+
--url <url> CMS base URL ${pc3.dim("(LUMIBASE_URL)")}
|
|
692
|
+
--site <id> Site / tenant id ${pc3.dim("(LUMIBASE_SITE_ID)")}
|
|
693
|
+
--token <tok> Bearer token ${pc3.dim("(LUMIBASE_TOKEN)")}
|
|
694
|
+
|
|
695
|
+
${pc3.bold("lumibase types")}
|
|
696
|
+
--out <path> Output file ${pc3.dim(`(default: ${DEFAULT_OUT})`)}
|
|
697
|
+
--include <a,b> Only these collections
|
|
698
|
+
--exclude <a,b> Skip these collections
|
|
699
|
+
--no-branded Emit plain string ids instead of branded ones
|
|
700
|
+
--import-from <pkg> Module the generated file imports from ${pc3.dim("(default: lumibase)")}
|
|
701
|
+
--check Fail if the file is missing or stale ${pc3.dim("(use in CI)")}
|
|
702
|
+
--stdout Print to stdout instead of writing a file
|
|
703
|
+
|
|
704
|
+
${pc3.bold("Examples")}
|
|
705
|
+
${pc3.dim("$")} lumibase init my-site
|
|
706
|
+
${pc3.dim("$")} lumibase types --out src/lumibase-types.d.ts
|
|
707
|
+
${pc3.dim("$")} lumibase types --check
|
|
708
|
+
${pc3.dim("$")} lumibase doctor
|
|
709
|
+
|
|
710
|
+
Docs: ${pc3.cyan("https://docs.lumibase.dev")}
|
|
711
|
+
`;
|
|
712
|
+
async function run(argv) {
|
|
713
|
+
const args = parseArgs(argv);
|
|
714
|
+
const command = args._[0];
|
|
715
|
+
if (boolFlag(args, "version") && !command) {
|
|
716
|
+
log.plain(readVersion());
|
|
717
|
+
return 0;
|
|
718
|
+
}
|
|
719
|
+
if (!command || boolFlag(args, "help")) {
|
|
720
|
+
log.plain(HELP);
|
|
721
|
+
return boolFlag(args, "help") ? 0 : 1;
|
|
722
|
+
}
|
|
723
|
+
switch (command) {
|
|
724
|
+
case "init":
|
|
725
|
+
return initCommand(argvAfterCommand(argv));
|
|
726
|
+
case "types":
|
|
727
|
+
return typesCommand(args);
|
|
728
|
+
case "doctor":
|
|
729
|
+
return doctorCommand(args);
|
|
730
|
+
case "help":
|
|
731
|
+
log.plain(HELP);
|
|
732
|
+
return 0;
|
|
733
|
+
case "version":
|
|
734
|
+
log.plain(readVersion());
|
|
735
|
+
return 0;
|
|
736
|
+
default:
|
|
737
|
+
throw new CliError(
|
|
738
|
+
`Unknown command: ${command}`,
|
|
739
|
+
"Run `lumibase --help` to see the available commands."
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// src/index.ts
|
|
745
|
+
async function main() {
|
|
746
|
+
const exitCode = await run(process5.argv.slice(2));
|
|
747
|
+
process5.exitCode = exitCode;
|
|
748
|
+
}
|
|
749
|
+
main().catch((err) => {
|
|
750
|
+
if (err instanceof CliError) {
|
|
751
|
+
log.plain();
|
|
752
|
+
log.error(err.message);
|
|
753
|
+
if (err.hint) log.dim(` ${err.hint}`);
|
|
754
|
+
log.plain();
|
|
755
|
+
process5.exitCode = err.exitCode;
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
if (process5.env["DEBUG"]) {
|
|
759
|
+
console.error(err);
|
|
760
|
+
} else {
|
|
761
|
+
log.plain();
|
|
762
|
+
log.error(err instanceof Error ? err.message : String(err));
|
|
763
|
+
console.error(pc4.dim(" Run with DEBUG=1 for the full stack trace."));
|
|
764
|
+
log.plain();
|
|
765
|
+
}
|
|
766
|
+
process5.exitCode = 1;
|
|
767
|
+
});
|
package/dist/lib.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@lumibase/sdk';
|
package/dist/lib.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lumibase",
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
|
+
"description": "LumiBase in one package — the JS/TS client (re-exports @lumibase/sdk) plus the `lumibase` CLI: generate TypeScript types from a live schema, check your setup, scaffold a project.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"lumibase": "./bin/lumibase.js"
|
|
9
|
+
},
|
|
10
|
+
"main": "./dist/lib.js",
|
|
11
|
+
"types": "./dist/lib.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/lib.d.ts",
|
|
15
|
+
"import": "./dist/lib.js"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"picocolors": "^1.1.1",
|
|
25
|
+
"@lumibase/sdk": "1.0.0-rc.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^26.4.0",
|
|
29
|
+
"tsup": "^8.5.1",
|
|
30
|
+
"typescript": "^5.6.2",
|
|
31
|
+
"vitest": "^4.1.11"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"bin"
|
|
36
|
+
],
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=22"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"lumibase",
|
|
42
|
+
"sdk",
|
|
43
|
+
"client",
|
|
44
|
+
"cli",
|
|
45
|
+
"cms",
|
|
46
|
+
"headless-cms",
|
|
47
|
+
"typegen",
|
|
48
|
+
"codegen",
|
|
49
|
+
"typescript",
|
|
50
|
+
"scaffold"
|
|
51
|
+
],
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/khuepm/lumibase.git",
|
|
55
|
+
"directory": "packages/cli"
|
|
56
|
+
},
|
|
57
|
+
"homepage": "https://docs.lumibase.dev",
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/khuepm/lumibase/issues"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "tsup",
|
|
63
|
+
"dev": "tsup --watch",
|
|
64
|
+
"test": "vitest --run",
|
|
65
|
+
"typecheck": "tsc --noEmit",
|
|
66
|
+
"clean": "rm -rf .turbo dist"
|
|
67
|
+
}
|
|
68
|
+
}
|