@stetcms/cli 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 +202 -0
- package/README.md +100 -0
- package/dist/index.mjs +313 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# @stetcms/cli
|
|
2
|
+
|
|
3
|
+
Stet from the command line, for scripting, seeding, and CI.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @stetcms/cli
|
|
9
|
+
# or run without installing
|
|
10
|
+
npx @stetcms/cli login
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### `stet generate`
|
|
16
|
+
|
|
17
|
+
Generate the typed content client from your organization's content model, exactly as [`@stetcms/vite`](https://github.com/jamiedavenport/stet/tree/main/published/vite) does at build time. Use it where there is no Vite: Next.js apps, other bundlers, or CI.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
STET_API_KEY=stet_... stet generate
|
|
21
|
+
stet generate --key stet_... --url https://stet.example.com --output lib/stet.gen.ts
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
| Flag | Description |
|
|
25
|
+
| ----------------- | -------------------------------------------------------------------- |
|
|
26
|
+
| `--url <origin>` | Stet server origin. Defaults to `$STET_ORIGIN`, then the hosted app. |
|
|
27
|
+
| `--key <api-key>` | Organization API key. Defaults to `$STET_API_KEY`. |
|
|
28
|
+
| `--output <path>` | Where the generated module goes. Defaults to `src/stet.gen.ts`. |
|
|
29
|
+
| `--config <path>` | Path to `stet.config.ts`. Auto-detected by default. |
|
|
30
|
+
|
|
31
|
+
Every flag overrides the same key in `stet.config.ts`, which both this CLI and the Vite plugin read (see [`@stetcms/config`](https://github.com/jamiedavenport/stet/tree/main/published/config)).
|
|
32
|
+
|
|
33
|
+
The generated file imports [`@stetcms/client`](https://github.com/jamiedavenport/stet/tree/main/published/client), so add that to your app's dependencies. It never contains the key: at runtime the client reads `STET_API_KEY` from the environment again, so the file is safe to commit. It reads `STET_ORIGIN` at runtime too, falling back to the origin it was generated against.
|
|
34
|
+
|
|
35
|
+
### `stet sync`
|
|
36
|
+
|
|
37
|
+
Publish your analytics tracking plan, so Stet can chart events your code declares before anyone has fired one. The same job [`@stetcms/vite`](https://github.com/jamiedavenport/stet/tree/main/published/vite) does on dev-server and build start; use this where there is no Vite, or in CI.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
STET_API_KEY=stet_... stet sync
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
| Flag | Description |
|
|
44
|
+
| ----------------- | -------------------------------------------------------------------- |
|
|
45
|
+
| `--url <origin>` | Stet server origin. Defaults to `$STET_ORIGIN`, then the hosted app. |
|
|
46
|
+
| `--key <api-key>` | Organization API key. Defaults to `$STET_API_KEY`. |
|
|
47
|
+
| `--config <path>` | Path to `stet.config.ts`. Auto-detected by default. |
|
|
48
|
+
|
|
49
|
+
Replacement, not merge: an event deleted from your code disappears from the dashboard's list on the next sync, while anything already recorded under that name keeps its history.
|
|
50
|
+
|
|
51
|
+
### `stet login`
|
|
52
|
+
|
|
53
|
+
Log in through your browser using the OAuth device flow. The CLI shows a short code, opens the verification page, and waits for you to approve the request. No password ever touches the terminal.
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
stet login
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
| Flag | Description |
|
|
60
|
+
| ---------------- | -------------------------------------------------------------------------- |
|
|
61
|
+
| `--url <origin>` | Stet server origin. Defaults to `$STET_API_URL`, then the hosted app. |
|
|
62
|
+
| `--no-open` | Print the verification link instead of opening a browser. Useful over SSH. |
|
|
63
|
+
|
|
64
|
+
### `stet whoami`
|
|
65
|
+
|
|
66
|
+
Show the account you are logged in as.
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
stet whoami
|
|
70
|
+
stet whoami --json
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`--json` prints the user, session, and active organization as JSON for scripting. The session token is never included in the output.
|
|
74
|
+
|
|
75
|
+
### `stet org`
|
|
76
|
+
|
|
77
|
+
Show the organization an API key is scoped to, including its billing plan.
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
stet org --api-key stet_... # or set STET_API_KEY
|
|
81
|
+
stet org --json
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Configuration
|
|
85
|
+
|
|
86
|
+
- `STET_API_URL`: default server origin for `stet login`.
|
|
87
|
+
- The session token is stored at `~/.config/stet/auth.json` (`$XDG_CONFIG_HOME/stet/auth.json` when set) with `0600` permissions. Delete the file to log out.
|
|
88
|
+
|
|
89
|
+
## Development
|
|
90
|
+
|
|
91
|
+
This package lives in the [Stet](https://github.com/jamiedavenport/stet) monorepo under `published/cli` and is built with `vp pack` (Vite+). Releases are versioned with Changesets and published from CI.
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
vp install
|
|
95
|
+
vp run build --filter @stetcms/cli
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
Apache-2.0
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
4
|
+
import path, { dirname, resolve } from "node:path";
|
|
5
|
+
import { fetchContentModel, renderContentModule } from "@stetcms/client/codegen";
|
|
6
|
+
import { resolveStetConfig } from "@stetcms/config";
|
|
7
|
+
import { findStetConfig, loadStetConfig, trackingPlanEvents } from "@stetcms/vite/config";
|
|
8
|
+
import { intro, log, note, outro, spinner } from "@clack/prompts";
|
|
9
|
+
import pc from "picocolors";
|
|
10
|
+
import open from "open";
|
|
11
|
+
import { createAuthClient } from "better-auth/client";
|
|
12
|
+
import { deviceAuthorizationClient, organizationClient } from "better-auth/client/plugins";
|
|
13
|
+
import fs from "node:fs";
|
|
14
|
+
import { homedir } from "node:os";
|
|
15
|
+
import Conf from "conf";
|
|
16
|
+
import { createStetClient, safe } from "@stetcms/client";
|
|
17
|
+
import { syncTrackingPlan } from "@stetcms/analytics/sync";
|
|
18
|
+
//#region package.json
|
|
19
|
+
var version = "0.1.0";
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/config.ts
|
|
22
|
+
/**
|
|
23
|
+
* Settles a command's options against `stet.config.ts` the same way
|
|
24
|
+
* `@stetcms/vite` does, so the plugin and the CLI can never disagree about
|
|
25
|
+
* which Stet a project talks to.
|
|
26
|
+
*/
|
|
27
|
+
async function settleOptions(options) {
|
|
28
|
+
const path = options.config === void 0 ? findStetConfig(process.cwd()) : resolve(process.cwd(), options.config);
|
|
29
|
+
const file = path === void 0 ? void 0 : await loadStetConfig(path);
|
|
30
|
+
return {
|
|
31
|
+
...resolveStetConfig(file, {
|
|
32
|
+
origin: options.url,
|
|
33
|
+
apiKey: options.key,
|
|
34
|
+
output: options.output
|
|
35
|
+
}),
|
|
36
|
+
events: trackingPlanEvents(file)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/ui.ts
|
|
41
|
+
function begin(title) {
|
|
42
|
+
intro(pc.inverse(` ${title} `));
|
|
43
|
+
}
|
|
44
|
+
function done(message) {
|
|
45
|
+
outro(pc.green(message));
|
|
46
|
+
}
|
|
47
|
+
function info(message) {
|
|
48
|
+
log.info(message);
|
|
49
|
+
}
|
|
50
|
+
function fail(message) {
|
|
51
|
+
log.error(pc.red(message));
|
|
52
|
+
outro(pc.red("Something went wrong."));
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
function showVerification(verificationUri, userCode) {
|
|
56
|
+
note([`${pc.dim("Visit")} ${verificationUri}`, `${pc.dim("Code")} ${pc.bold(userCode)}`].join("\n"), "Confirm this device");
|
|
57
|
+
}
|
|
58
|
+
function progress() {
|
|
59
|
+
if (process.stdout.isTTY) {
|
|
60
|
+
const active = spinner();
|
|
61
|
+
return {
|
|
62
|
+
start: (message) => active.start(message),
|
|
63
|
+
stop: (message) => active.stop(message)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
start: (message) => log.step(message),
|
|
68
|
+
stop: (message) => log.step(message)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function keyValueRows(rows) {
|
|
72
|
+
const width = Math.max(...rows.map(([label]) => label.length));
|
|
73
|
+
return rows.map(([label, value]) => `${pc.dim(label.padEnd(width))} ${value}`).join("\n");
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/commands/generate.ts
|
|
77
|
+
const generateCommand = new Command("generate").description("Generate the typed content client from your content model").option("--url <origin>", "Stet server origin (defaults to $STET_ORIGIN or the hosted app)").option("--key <api-key>", "Organization API key (defaults to $STET_API_KEY)").option("--output <path>", "Where the generated module goes").option("--config <path>", "Path to stet.config.ts (auto-detected by default)").action(async (options) => {
|
|
78
|
+
begin("stet generate");
|
|
79
|
+
const { origin, apiKey, output } = await settleOptions(options);
|
|
80
|
+
if (apiKey === void 0) fail("No API key. Pass --key or set STET_API_KEY.");
|
|
81
|
+
const fetching = progress();
|
|
82
|
+
fetching.start(`Fetching the content model from ${origin}…`);
|
|
83
|
+
let code;
|
|
84
|
+
try {
|
|
85
|
+
const model = await fetchContentModel(origin, apiKey);
|
|
86
|
+
const count = model.types.length;
|
|
87
|
+
code = renderContentModule(model, origin);
|
|
88
|
+
fetching.stop(`Fetched ${count} ${count === 1 ? "type" : "types"}`);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
fetching.stop("Could not fetch the model");
|
|
91
|
+
fail(String(error));
|
|
92
|
+
}
|
|
93
|
+
const target = resolve(process.cwd(), output);
|
|
94
|
+
await mkdir(dirname(target), { recursive: true });
|
|
95
|
+
await writeFile(target, code, "utf8");
|
|
96
|
+
done(`Generated ${output}`);
|
|
97
|
+
});
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/client.ts
|
|
100
|
+
const DEFAULT_ORIGIN = "https://stetcms.com";
|
|
101
|
+
const CLI_CLIENT_ID = "stet-cli";
|
|
102
|
+
function resolveOrigin(explicitUrl) {
|
|
103
|
+
if (explicitUrl !== void 0 && explicitUrl !== "") return explicitUrl;
|
|
104
|
+
const fromEnv = process.env.STET_API_URL;
|
|
105
|
+
if (fromEnv !== void 0 && fromEnv !== "") return fromEnv;
|
|
106
|
+
return DEFAULT_ORIGIN;
|
|
107
|
+
}
|
|
108
|
+
function createClient(origin) {
|
|
109
|
+
return createAuthClient({
|
|
110
|
+
baseURL: origin,
|
|
111
|
+
plugins: [deviceAuthorizationClient(), organizationClient()]
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/store.ts
|
|
116
|
+
function configDir() {
|
|
117
|
+
const xdgConfigHome = process.env.XDG_CONFIG_HOME;
|
|
118
|
+
if (xdgConfigHome !== void 0 && xdgConfigHome !== "") return path.join(xdgConfigHome, "stet");
|
|
119
|
+
return path.join(homedir(), ".config", "stet");
|
|
120
|
+
}
|
|
121
|
+
function createStore() {
|
|
122
|
+
return new Conf({
|
|
123
|
+
cwd: configDir(),
|
|
124
|
+
configName: "auth",
|
|
125
|
+
configFileMode: 384,
|
|
126
|
+
clearInvalidConfig: true,
|
|
127
|
+
schema: {
|
|
128
|
+
origin: { type: "string" },
|
|
129
|
+
token: { type: "string" }
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
function saveAuth(auth) {
|
|
134
|
+
createStore().set(auth);
|
|
135
|
+
fs.chmodSync(configDir(), 448);
|
|
136
|
+
}
|
|
137
|
+
function loadAuth() {
|
|
138
|
+
const store = createStore();
|
|
139
|
+
const origin = store.get("origin");
|
|
140
|
+
const token = store.get("token");
|
|
141
|
+
if (origin === void 0 || token === void 0) return null;
|
|
142
|
+
return {
|
|
143
|
+
origin,
|
|
144
|
+
token
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/commands/login.ts
|
|
149
|
+
const DEVICE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
|
|
150
|
+
function sleep(milliseconds) {
|
|
151
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
152
|
+
}
|
|
153
|
+
async function pollForToken(client, device) {
|
|
154
|
+
const waiting = progress();
|
|
155
|
+
waiting.start("Waiting for approval in the browser…");
|
|
156
|
+
let intervalSeconds = device.interval ?? 5;
|
|
157
|
+
const deadline = Date.now() + device.expires_in * 1e3;
|
|
158
|
+
while (Date.now() < deadline) {
|
|
159
|
+
await sleep(intervalSeconds * 1e3);
|
|
160
|
+
const { data, error } = await client.device.token({
|
|
161
|
+
grant_type: DEVICE_GRANT_TYPE,
|
|
162
|
+
device_code: device.device_code,
|
|
163
|
+
client_id: CLI_CLIENT_ID
|
|
164
|
+
});
|
|
165
|
+
if (data !== null) {
|
|
166
|
+
waiting.stop("Approved");
|
|
167
|
+
return data.access_token;
|
|
168
|
+
}
|
|
169
|
+
if (error.error === "authorization_pending") continue;
|
|
170
|
+
if (error.error === "slow_down") {
|
|
171
|
+
intervalSeconds = intervalSeconds + 5;
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
waiting.stop("Not approved");
|
|
175
|
+
if (error.error === "access_denied") fail("The request was denied in the browser.");
|
|
176
|
+
if (error.error === "expired_token") fail("The code expired before the login was approved. Run `stet login` again.");
|
|
177
|
+
fail(error.error_description ?? "Device authorization failed.");
|
|
178
|
+
}
|
|
179
|
+
waiting.stop("Timed out");
|
|
180
|
+
fail("The code expired before the login was approved. Run `stet login` again.");
|
|
181
|
+
}
|
|
182
|
+
const loginCommand = new Command("login").description("Log in to Stet from this machine").option("--url <origin>", "Stet server origin (defaults to $STET_API_URL or the hosted app)").option("--no-open", "Print the verification link instead of opening a browser").action(async (options) => {
|
|
183
|
+
const origin = resolveOrigin(options.url);
|
|
184
|
+
const client = createClient(origin);
|
|
185
|
+
begin("stet login");
|
|
186
|
+
const { data: device, error } = await client.device.code({ client_id: CLI_CLIENT_ID });
|
|
187
|
+
if (error !== null || device === null) fail(error?.error_description ?? `Could not reach ${origin}.`);
|
|
188
|
+
showVerification(device.verification_uri, device.user_code);
|
|
189
|
+
if (options.open === true) {
|
|
190
|
+
const verificationUrl = device.verification_uri_complete ?? device.verification_uri;
|
|
191
|
+
try {
|
|
192
|
+
await open(verificationUrl);
|
|
193
|
+
} catch {
|
|
194
|
+
info("Could not open a browser. Use the link above to continue.");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const token = await pollForToken(client, device);
|
|
198
|
+
saveAuth({
|
|
199
|
+
origin,
|
|
200
|
+
token
|
|
201
|
+
});
|
|
202
|
+
const { data: session } = await client.getSession({ fetchOptions: { headers: { Authorization: `Bearer ${token}` } } });
|
|
203
|
+
done(`Logged in as ${session?.user.email ?? "your account"}`);
|
|
204
|
+
});
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/commands/org.ts
|
|
207
|
+
const orgCommand = new Command("org").description("Show the organization your API key is scoped to").option("--api-key <key>", "Organization API key (defaults to STET_API_KEY)").option("--url <url>", "Origin of the Stet deployment").option("--json", "Print the organization as JSON").action(async (options) => {
|
|
208
|
+
const apiKey = options.apiKey ?? process.env.STET_API_KEY;
|
|
209
|
+
if (apiKey === void 0 || apiKey === "") {
|
|
210
|
+
console.error("No API key. Pass --api-key or set STET_API_KEY.");
|
|
211
|
+
process.exit(1);
|
|
212
|
+
}
|
|
213
|
+
const origin = resolveOrigin(options.url);
|
|
214
|
+
const client = createStetClient({
|
|
215
|
+
origin,
|
|
216
|
+
apiKey
|
|
217
|
+
});
|
|
218
|
+
const { data, error, isDefined } = await safe(client.org.current());
|
|
219
|
+
if (isDefined && error.code === "UNAUTHORIZED") {
|
|
220
|
+
console.error("Invalid or revoked API key.");
|
|
221
|
+
process.exit(1);
|
|
222
|
+
}
|
|
223
|
+
if (error !== null) {
|
|
224
|
+
console.error(error.message !== "" ? error.message : `Could not reach ${origin}.`);
|
|
225
|
+
process.exit(1);
|
|
226
|
+
}
|
|
227
|
+
if (options.json === true) {
|
|
228
|
+
console.log(JSON.stringify(data, null, 2));
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const rows = [
|
|
232
|
+
["Name", data.name],
|
|
233
|
+
["Slug", data.slug],
|
|
234
|
+
["Id", data.id],
|
|
235
|
+
["Server", origin]
|
|
236
|
+
];
|
|
237
|
+
const billing = await safe(client.org.billing());
|
|
238
|
+
if (billing.error === null) {
|
|
239
|
+
const suffix = billing.data.cancelAtPeriodEnd ? " (cancels at period end)" : "";
|
|
240
|
+
rows.push(["Plan", `${billing.data.plan}${suffix}`]);
|
|
241
|
+
}
|
|
242
|
+
console.log(keyValueRows(rows));
|
|
243
|
+
});
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region src/commands/sync.ts
|
|
246
|
+
const syncCommand = new Command("sync").description("Publish your analytics tracking plan so Stet can chart its events").option("--url <origin>", "Stet server origin (defaults to $STET_ORIGIN or the hosted app)").option("--key <api-key>", "Organization API key (defaults to $STET_API_KEY)").option("--config <path>", "Path to stet.config.ts (auto-detected by default)").action(async (options) => {
|
|
247
|
+
begin("stet sync");
|
|
248
|
+
const { origin, apiKey, events } = await settleOptions(options);
|
|
249
|
+
if (apiKey === void 0) fail("No API key. Pass --key or set STET_API_KEY.");
|
|
250
|
+
if (events === void 0) fail("No tracking plan found. Add `analytics` to stet.config.ts.");
|
|
251
|
+
const syncing = progress();
|
|
252
|
+
syncing.start(`Publishing the tracking plan to ${origin}…`);
|
|
253
|
+
try {
|
|
254
|
+
const { synced } = await syncTrackingPlan({
|
|
255
|
+
events,
|
|
256
|
+
origin,
|
|
257
|
+
apiKey
|
|
258
|
+
});
|
|
259
|
+
syncing.stop(`Published ${synced} ${synced === 1 ? "event" : "events"}`);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
syncing.stop("Could not publish the plan");
|
|
262
|
+
fail(String(error));
|
|
263
|
+
}
|
|
264
|
+
done("Tracking plan is live");
|
|
265
|
+
});
|
|
266
|
+
//#endregion
|
|
267
|
+
//#region src/commands/whoami.ts
|
|
268
|
+
const whoamiCommand = new Command("whoami").description("Show the account you are logged in as").option("--json", "Print the session as JSON").action(async (options) => {
|
|
269
|
+
const stored = loadAuth();
|
|
270
|
+
if (stored === null) {
|
|
271
|
+
console.error("Not logged in. Run `stet login` first.");
|
|
272
|
+
process.exit(1);
|
|
273
|
+
}
|
|
274
|
+
const client = createClient(stored.origin);
|
|
275
|
+
const fetchOptions = { headers: { Authorization: `Bearer ${stored.token}` } };
|
|
276
|
+
const { data: session, error } = await client.getSession({ fetchOptions });
|
|
277
|
+
if (error !== null) {
|
|
278
|
+
console.error(error.message ?? `Could not reach ${stored.origin}.`);
|
|
279
|
+
process.exit(1);
|
|
280
|
+
}
|
|
281
|
+
if (session === null) {
|
|
282
|
+
console.error("Your session has expired. Run `stet login` again.");
|
|
283
|
+
process.exit(1);
|
|
284
|
+
}
|
|
285
|
+
const { data: organizations } = await client.organization.list({ fetchOptions });
|
|
286
|
+
const activeOrganization = organizations?.find((organization) => organization.id === session.session.activeOrganizationId);
|
|
287
|
+
if (options.json === true) {
|
|
288
|
+
const { token: _token, ...safeSession } = session.session;
|
|
289
|
+
const payload = {
|
|
290
|
+
user: session.user,
|
|
291
|
+
session: safeSession,
|
|
292
|
+
activeOrganization: activeOrganization ?? null
|
|
293
|
+
};
|
|
294
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const rows = [["Name", session.user.name], ["Email", session.user.email]];
|
|
298
|
+
if (activeOrganization !== void 0) rows.push(["Org", activeOrganization.name]);
|
|
299
|
+
rows.push(["Server", stored.origin]);
|
|
300
|
+
console.log(keyValueRows(rows));
|
|
301
|
+
});
|
|
302
|
+
//#endregion
|
|
303
|
+
//#region src/index.ts
|
|
304
|
+
const program = new Command();
|
|
305
|
+
program.name("stet").description("Stet from the command line").version(version);
|
|
306
|
+
program.addCommand(generateCommand);
|
|
307
|
+
program.addCommand(loginCommand);
|
|
308
|
+
program.addCommand(orgCommand);
|
|
309
|
+
program.addCommand(syncCommand);
|
|
310
|
+
program.addCommand(whoamiCommand);
|
|
311
|
+
await program.parseAsync(process.argv);
|
|
312
|
+
//#endregion
|
|
313
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stetcms/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Command line interface for Stet.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"auth",
|
|
7
|
+
"cli",
|
|
8
|
+
"device-flow",
|
|
9
|
+
"stet"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/jamiedavenport/stet/tree/main/published/cli#readme",
|
|
12
|
+
"bugs": "https://github.com/jamiedavenport/stet/issues",
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"author": "Jamie Davenport (https://jxd.dev)",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/jamiedavenport/stet.git",
|
|
18
|
+
"directory": "published/cli"
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"stet": "./dist/index.mjs"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@clack/prompts": "^1.7.0",
|
|
32
|
+
"better-auth": "^1.6.25",
|
|
33
|
+
"commander": "^15.0.0",
|
|
34
|
+
"conf": "^15.1.0",
|
|
35
|
+
"open": "^11.0.0",
|
|
36
|
+
"picocolors": "^1.1.1",
|
|
37
|
+
"@stetcms/analytics": "0.1.0",
|
|
38
|
+
"@stetcms/client": "0.1.0",
|
|
39
|
+
"@stetcms/config": "0.1.0",
|
|
40
|
+
"@stetcms/vite": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@types/node": "^26.1.1",
|
|
44
|
+
"publint": "^0.3.21",
|
|
45
|
+
"typescript": "^5.9.3",
|
|
46
|
+
"vite-plus": "0.2.2"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=20"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "vp pack",
|
|
53
|
+
"tc": "tsc --noEmit"
|
|
54
|
+
}
|
|
55
|
+
}
|