@nickpaustian/tela 0.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nickpaustian/tela",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A Git-backed headless CMS: collections of fields in TypeScript, edited in a Lightspeed admin, published as JSON commits to a content repo.",
5
5
  "license": "MIT",
6
6
  "author": "Nick Paustian",
@@ -30,6 +30,10 @@
30
30
  "src",
31
31
  "routes",
32
32
  "lib",
33
+ "scripts/check-components.ts",
34
+ "scripts/create-user.ts",
35
+ "scripts/set-password.ts",
36
+ "scripts/media-words.ts",
33
37
  "LICENSE",
34
38
  "README.md"
35
39
  ],
@@ -0,0 +1,9 @@
1
+ import { config } from "../src/config/load.ts"
2
+ import { checkProject, reportCheck } from "../src/config/check.ts"
3
+ import { componentRoots } from "../src/project.ts"
4
+
5
+ /**
6
+ * The pre-build check, runnable on its own. `tela check` is the same call.
7
+ */
8
+ const ok = reportCheck(checkProject(config.collections, componentRoots(), config.overrides))
9
+ if (!ok) process.exit(1)
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Create an account. There is no sign-up route by design: an admin panel with
3
+ * open registration is a way to lose a site. The one exception is the first
4
+ * account, which the sign-in page offers to create while no account exists.
5
+ *
6
+ * bun run scripts/create-user.ts alice@example.com
7
+ * bun run scripts/create-user.ts alice@example.com --editor
8
+ *
9
+ * The first account is an **admin**, because otherwise nobody is: the deleted
10
+ * view and permanent deletion would be behind a role no account holds. After
11
+ * that the default stays admin - the script is run by whoever administers this
12
+ * CMS - and `--editor` is how to make one that is not.
13
+ */
14
+ import { createUser } from "../src/auth/login.ts"
15
+ import { countUsers } from "../src/content/users.ts"
16
+ import { sql } from "../src/db/client.ts"
17
+
18
+ const email = process.argv[2]
19
+ const role = process.argv.includes("--editor") ? "editor" : "admin"
20
+
21
+ if (!email) {
22
+ console.error("usage: bun run scripts/create-user.ts <email>")
23
+ process.exit(1)
24
+ }
25
+
26
+ const password = process.env.TELA_PASSWORD ?? prompt("password:")
27
+
28
+ if (!password || password.length < 10) {
29
+ console.error("A password of at least 10 characters is required.")
30
+ process.exit(1)
31
+ }
32
+
33
+ try {
34
+ const first = (await countUsers()) === 0
35
+ const id = await createUser(email, password, { role })
36
+
37
+ console.log(
38
+ `created ${email} (${id}) as ${role}` + (first ? " - the first account" : ""),
39
+ )
40
+ } catch (error) {
41
+ // Unique violation on email is the common case and worth saying plainly.
42
+ console.error(
43
+ String(error).includes("unique")
44
+ ? `${email} already exists.`
45
+ : `Could not create user: ${error}`,
46
+ )
47
+ process.exit(1)
48
+ } finally {
49
+ await sql.end()
50
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Move words kept on media records into the media library.
3
+ *
4
+ * Over a library, a media record holds no alt text or credit of its own: the
5
+ * library keeps a picture's words, shared and per group, and publishing reads
6
+ * them there. Records written before that still carry words that nothing
7
+ * reads any more. This moves each one into the picture's words for this
8
+ * instance's group - what the record meant, "how this site describes it" -
9
+ * and clears the record.
10
+ *
11
+ * Shows what it would do unless given --apply. A picture that is not in this
12
+ * instance's group cannot hold words for it; those are listed and left, since
13
+ * moving them into the shared words would change every other site's text.
14
+ *
15
+ * tela media-words what would move
16
+ * tela media-words --apply move it
17
+ */
18
+ import { sql } from "../src/db/client.ts"
19
+ import { media } from "../src/media/index.ts"
20
+
21
+ const apply = process.argv.includes("--apply")
22
+ const library = media().library
23
+
24
+ if (!library) {
25
+ console.log("This project's media store is not a library: its records keep their own words, and nothing needs moving.")
26
+ await sql.end()
27
+ process.exit(0)
28
+ }
29
+
30
+ if (!library.tag) {
31
+ console.error("This instance has no group (TELA_PSEUDOLUX_TAG), so there is no group's words to move into.")
32
+ await sql.end()
33
+ process.exit(1)
34
+ }
35
+
36
+ const rows = await sql`
37
+ select id, data from entries
38
+ where collection = 'media' and deleted_at is null
39
+ and (coalesce(data ->> 'alt', '') <> '' or coalesce(data ->> 'credit', '') <> '')
40
+ order by updated_at
41
+ `
42
+
43
+ let moved = 0
44
+ let left = 0
45
+
46
+ for (const row of rows) {
47
+ const data = row.data as { file?: string; title?: string; alt?: string; credit?: string }
48
+ const name = data.title || data.file || row.id
49
+ const words = {
50
+ ...(data.alt ? { altText: data.alt } : {}),
51
+ ...(data.credit ? { credit: data.credit } : {}),
52
+ }
53
+
54
+ const asset = data.file ? await library.client.inspect(data.file).catch(() => null) : null
55
+ if (!asset || !(asset.tags ?? []).includes(library.tag)) {
56
+ console.log(`left ${name}: ${asset ? `not in ${library.tag}` : "not in the library"}`)
57
+ left++
58
+ continue
59
+ }
60
+
61
+ // What the group already says wins; the record only fills a gap.
62
+ const has = asset.views?.[library.tag] ?? {}
63
+ const fill = Object.fromEntries(
64
+ Object.entries(words).filter(([word]) => !has[word as keyof typeof has]),
65
+ )
66
+
67
+ console.log(`${apply ? "moved " : "move "} ${name}: ${Object.keys(fill).join(" and ") || "nothing new"} to ${library.tag}`)
68
+ if (apply) {
69
+ if (Object.keys(fill).length) await library.client.describeView(asset.pid, library.tag, fill)
70
+ await sql`update entries set data = data - 'alt' - 'credit' where id = ${row.id}`
71
+ }
72
+ moved++
73
+ }
74
+
75
+ console.log(
76
+ `\n${rows.length} record${rows.length === 1 ? "" : "s"} with words: ${moved} ${apply ? "moved" : "to move"}, ${left} left.` +
77
+ (apply || !moved ? "" : " Run again with --apply to move them."),
78
+ )
79
+ await sql.end()
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Change an account's password.
3
+ *
4
+ * The companion to `create-user.ts`, and the way back in when somebody is
5
+ * locked out: there is no reset email and no sign-up route, deliberately, so
6
+ * this is the door - and it is a door that needs a shell on the server.
7
+ *
8
+ * bun run scripts/set-password.ts alice@example.com
9
+ * TELA_PASSWORD=… bun run scripts/set-password.ts alice@example.com
10
+ */
11
+ import { sql } from "../src/db/client.ts"
12
+
13
+ const email = process.argv[2]?.toLowerCase()
14
+
15
+ if (!email) {
16
+ console.error("usage: bun run scripts/set-password.ts <email>")
17
+ process.exit(1)
18
+ }
19
+
20
+ // Checked before asking for a password. Being prompted, typing carefully, and
21
+ // then being told the account does not exist is a small insult.
22
+ const [account] = await sql`select id from users where email = ${email}`
23
+
24
+ if (!account) {
25
+ console.error(`No account for ${email}.`)
26
+
27
+ const others = await sql`select email from users order by email`
28
+ if (others.length) {
29
+ console.error(`Accounts: ${others.map((one: any) => one.email).join(", ")}`)
30
+ }
31
+
32
+ await sql.end()
33
+ process.exit(1)
34
+ }
35
+
36
+ const password = process.env.TELA_PASSWORD ?? prompt("new password:")
37
+
38
+ // The same floor `createUser` applies. A script that quietly accepts a weaker
39
+ // password than the form does is a hole in the form.
40
+ if (!password || password.length < 10) {
41
+ console.error("A password of at least 10 characters is required.")
42
+ await sql.end()
43
+ process.exit(1)
44
+ }
45
+
46
+ try {
47
+ await sql`
48
+ update users
49
+ set password_hash = ${await Bun.password.hash(password)},
50
+ updated_at = now()
51
+ where id = ${account.id}
52
+ `
53
+
54
+ // Every session for this account goes. Changing a password because it may
55
+ // have been seen is the main reason to change one, and leaving the existing
56
+ // sessions alive would defeat exactly that.
57
+ const gone = await sql`
58
+ delete from sessions where user_id = ${account.id} returning id
59
+ `
60
+
61
+ console.log(
62
+ `password set for ${email}` +
63
+ (gone.length
64
+ ? ` — ${gone.length} session${gone.length === 1 ? "" : "s"} signed out`
65
+ : ""),
66
+ )
67
+ } catch (error) {
68
+ console.error(`Could not set the password: ${error}`)
69
+ process.exit(1)
70
+ } finally {
71
+ await sql.end()
72
+ }