create-pincast 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/starter.js ADDED
@@ -0,0 +1,156 @@
1
+ import { constants, chmodSync, copyFileSync, mkdirSync, readFileSync, rmdirSync, rmSync, writeFileSync } from 'node:fs'
2
+ import { dirname, join } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
+ import { COMMAND } from './name.js'
5
+
6
+ const root = join(dirname(fileURLToPath(import.meta.url)), '..')
7
+
8
+ // The starter's files and the commit they came from.
9
+ export const manifest = JSON.parse(readFileSync(join(root, 'template.json'), 'utf8'))
10
+
11
+ const STARTER_ID = 'lantern-walk'
12
+ export const SET_ID = 'old-town-lanterns'
13
+ export const TEST_PERSON = 'test_demo'
14
+
15
+ // The one collect the command sends. The printed command repeats the same body and the same retry key,
16
+ // so Pincast treats the two as one call for 24 hours, and a lost answer can't count a second collect.
17
+ export const FIRST_COLLECT = { card: 'lantern:ferry-steps', actor: { id: TEST_PERSON, source: 'anonymous' } }
18
+ export const COLLECT_RETRY_KEY = 'create-pincast-first-collect'
19
+
20
+ // The example app's README predates test keys. template/ is rebuilt byte for byte from that repo, so a
21
+ // fix made there would be lost at the next sync. Each new app's copy is corrected here instead. Once the
22
+ // example drops the sentence, this replaces nothing.
23
+ const OLD_KEY_LINE = 'Every Pincast key is live, so anything you write with one is real.'
24
+ export const LIVE_KEY_LINE = 'A live key writes real data, and no API call deletes it.'
25
+
26
+ // npm pack drops or rewrites these names, so the package stores them under another.
27
+ const STORED = { '.gitignore': '_gitignore', '.env.example': '_env.example', 'package-lock.json': '_package-lock.json' }
28
+ export const storedPath = file => join(root, 'template', STORED[file] ?? file)
29
+
30
+ /**
31
+ * Copies the starter into an empty folder. Refuses to replace any file. Each file joins `written` as
32
+ * it lands, so a copy that fails partway can still be taken back.
33
+ */
34
+ export function copyStarter(target, written) {
35
+ for (const file of Object.keys(manifest.files)) {
36
+ const to = join(target, file)
37
+ mkdirSync(dirname(to), { recursive: true })
38
+ copyFileSync(storedPath(file), to, constants.COPYFILE_EXCL)
39
+ written.push(file)
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Takes back what copyStarter wrote. madeFrom is the highest folder this run made, if any: those
45
+ * folders go too, unless something else is in them.
46
+ */
47
+ export function removeStarter(target, files, madeFrom) {
48
+ for (const file of files) rmSync(join(target, file), { force: true })
49
+ const folders = new Set()
50
+ for (const file of Object.keys(manifest.files)) {
51
+ for (let d = dirname(file); d !== '.'; d = dirname(d)) folders.add(d)
52
+ }
53
+ for (const d of [...folders].sort((a, b) => b.length - a.length)) {
54
+ try {
55
+ rmdirSync(join(target, d))
56
+ } catch { /* missing, or something else is in it: leave it */ }
57
+ }
58
+ if (!madeFrom) return
59
+ for (let d = target; d.length >= madeFrom.length; d = dirname(d)) {
60
+ try {
61
+ rmdirSync(d)
62
+ } catch {
63
+ return
64
+ }
65
+ if (d === madeFrom) return
66
+ }
67
+ }
68
+
69
+ /** Writes .env, readable only by this account. The claim link is quoted because it holds a #. */
70
+ export function writeEnv(target, { key, claimUrl, customBase }) {
71
+ const lines = [
72
+ `# Made by ${COMMAND}. This file holds your test key, so keep it private.`,
73
+ `PINCAST_KEY=${key}`,
74
+ `PINCAST_ACTOR=${TEST_PERSON}`,
75
+ `PINCAST_CLAIM_URL="${claimUrl}"`,
76
+ ]
77
+ if (customBase) lines.push(`PINCAST_API=${customBase}`)
78
+ const path = join(target, '.env')
79
+ writeFileSync(path, `${lines.join('\n')}\n`, { flag: 'wx', mode: 0o600 })
80
+ chmodSync(path, 0o600)
81
+ }
82
+
83
+ /** Puts the app's id in shared/app.js and in the goldens, which draw it into every card's data-id. */
84
+ export function setAppId(target, id) {
85
+ const appFile = join(target, 'shared', 'app.js')
86
+ const source = readFileSync(appFile, 'utf8')
87
+ const line = `{ id: '${STARTER_ID}',`
88
+ if (source.split(line).length !== 2) throw new Error('app id line not found')
89
+ writeFileSync(appFile, source.replace(line, `{ id: '${id}',`))
90
+ for (const file of Object.keys(manifest.files)) {
91
+ if (!/^test\/goldens\/.+\.html$/.test(file)) continue
92
+ const path = join(target, file)
93
+ writeFileSync(path, readFileSync(path, 'utf8').replaceAll(`data-id="${STARTER_ID}:`, `data-id="${id}:`))
94
+ }
95
+ }
96
+
97
+ /**
98
+ * The commands that send what the first run still needs, as printed and as written in the README.
99
+ * left is 'both', 'collect' or 'none'. After a collect that may have landed ('collect'), the command
100
+ * carries the retry key, so running it can't count a second collect.
101
+ */
102
+ export function firstRunCommands(base, left = 'both') {
103
+ if (left === 'none') return []
104
+ const put = [
105
+ `curl -X PUT ${base}/sets/${SET_ID} \\`,
106
+ ' -H "X-Pincast-Key: $PINCAST_KEY" \\',
107
+ ' -H "Content-Type: application/json" \\',
108
+ ' -d @data/set.json',
109
+ ]
110
+ const collect = [
111
+ `curl -X POST ${base}/sets/${SET_ID}/collects \\`,
112
+ ' -H "X-Pincast-Key: $PINCAST_KEY" \\',
113
+ ' -H "Content-Type: application/json" \\',
114
+ ...(left === 'collect' ? [` -H "Idempotency-Key: ${COLLECT_RETRY_KEY}" \\`] : []),
115
+ ` -d '{ "card": "${FIRST_COLLECT.card}", "actor": { "id": "${TEST_PERSON}", "source": "anonymous" } }'`,
116
+ ]
117
+ return ['set -a; . ./.env; set +a', ...(left === 'collect' ? [] : put), ...collect]
118
+ }
119
+
120
+ const README_LEAD = {
121
+ none: `This app's set and one collect for \`${TEST_PERSON}\` are already on Pincast. To see them, install the app and start it:`,
122
+ collect: "This app's set is on Pincast. To see live cards, install the app, send its one collect, then start it:",
123
+ both: 'To see live cards, install the app, send its set and one collect, then start it:',
124
+ }
125
+
126
+ /**
127
+ * Adds this copy's own section to its README, saying what the first run left to do. The starter's Go
128
+ * live steps open by replacing .env, which here would erase the key. The claim link stays out, because
129
+ * a README gets committed.
130
+ */
131
+ export function addReadmeSection(target, { id, base, endsOn, left = 'both' }) {
132
+ const path = join(target, 'README.md')
133
+ const readme = readFileSync(path, 'utf8').replace(OLD_KEY_LINE, () => LIVE_KEY_LINE)
134
+ const anchor = '## Run it\n'
135
+ if (readme.split(anchor).length !== 2) throw new Error('README anchor not found')
136
+ const section = [
137
+ '## Your test app',
138
+ '',
139
+ `\`${COMMAND}\` made this copy and the test app \`${id}\` for it. The key and the claim link are in \`.env\`, which git ignores. A test key writes only to this app, and only for test people, whose ids start with \`test_\`.`,
140
+ '',
141
+ README_LEAD[left],
142
+ '',
143
+ '```sh',
144
+ 'npm ci',
145
+ ...firstRunCommands(base, left),
146
+ 'npm run dev',
147
+ '```',
148
+ '',
149
+ `The app ends on ${endsOn} unless you claim it. To keep it, open the \`PINCAST_CLAIM_URL\` from \`.env\` while signed in to Pincast.`,
150
+ '',
151
+ 'Save the Go live section below for when you have a live key. Its first step replaces `.env`, which would erase this key.',
152
+ '',
153
+ '',
154
+ ].join('\n')
155
+ writeFileSync(path, readme.replace(anchor, () => section + anchor))
156
+ }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Pincast, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,91 @@
1
+ # Lantern Walk
2
+
3
+ A small app that shows a person's cards on the [Placecard kit's](https://www.npmjs.com/package/@pincast/placecard-kit) card table. Clone it and you'll have cards on screen with no account and no key. Add a key, and the same table shows real collects.
4
+
5
+ It's built with Vite and Vue, and the only Pincast code in it is the public kit.
6
+
7
+ ## Run it
8
+
9
+ You need Node 20.12 or later.
10
+
11
+ ```sh
12
+ git clone <this repository>
13
+ cd pincast-kit-example
14
+ npm ci
15
+ npm run dev
16
+ ```
17
+
18
+ Open http://localhost:5173. Five sample cards are on the table, and three more wait under the Missing chip. With no key, the app never calls Pincast.
19
+
20
+ ## Go live
21
+
22
+ Every Pincast key is live, so anything you write with one is real. [Test and live](https://docs.pincast.fm/concepts/test-and-live) covers how to get a key and how to keep experiments out of sight.
23
+
24
+ 1. Copy the settings file, then put your key in `PINCAST_KEY`:
25
+
26
+ ```sh
27
+ cp .env.example .env
28
+ ```
29
+
30
+ 2. In `shared/app.js`, change `APP.id` to your app's id: the part of your key between `pck_` and the next underscore. The app refuses a key for any other app.
31
+
32
+ 3. Write the goldens again, because they carry the app id:
33
+
34
+ ```sh
35
+ UPDATE_GOLDENS=1 npm test
36
+ ```
37
+
38
+ 4. Register the set. Its `meta.listed` is `false`, which keeps it off the Placecard map and front page.
39
+
40
+ ```sh
41
+ set -a; . ./.env; set +a
42
+ curl -X PUT https://api.pincast.fm/v1/sets/old-town-lanterns \
43
+ -H "X-Pincast-Key: $PINCAST_KEY" \
44
+ -H "Content-Type: application/json" \
45
+ -d @data/set.json
46
+ ```
47
+
48
+ 5. Record one collect for a guest:
49
+
50
+ ```sh
51
+ curl -X POST https://api.pincast.fm/v1/sets/old-town-lanterns/collects \
52
+ -H "X-Pincast-Key: $PINCAST_KEY" \
53
+ -H "Content-Type: application/json" \
54
+ -d '{ "card": "lantern:ferry-steps", "actor": { "id": "guest-lantern-1", "source": "anonymous" } }'
55
+ ```
56
+
57
+ 6. Set `PINCAST_ACTOR=guest-lantern-1` in `.env` and run `npm run dev` again. The line at the top of the page reads "Live from the Pincast API."
58
+
59
+ ## Where things are
60
+
61
+ | File | What it does |
62
+ | :- | :- |
63
+ | `shared/app.js` | The app's id, name and color, its set, its face and its rarity words. |
64
+ | `data/set.json` | The set and its cards: the exact body of `PUT /v1/sets/:set`. The API doesn't send a set's cards back, so the app keeps this copy. |
65
+ | `data/sample-collects.json` | A saved answer of `GET /v1/sets/:set/collects`, used when there's no key. |
66
+ | `server/collection.js` | Turns the set and one person's collects into the kit's collection with `ttFromAppSet`. |
67
+ | `server/route.js` | `GET /api/collection`, the one route. It holds the key and calls the API. |
68
+ | `server/index.js` | The production server. In development, Vite mounts the same route. |
69
+ | `src/tabletop.js` | The table's config for `createTabletop`. |
70
+ | `src/lantern.css` | The theme, and the app's own face under `.ttf[data-face="lantern"]`. |
71
+ | `test/conformance.test.js` | The kit's `ttConformance` suite, run on this app's config. |
72
+ | `test/route.test.js` | The route against a local mock of the API. The tests never reach Pincast. |
73
+
74
+ ## Two rules this app follows
75
+
76
+ - **The key stays on the server.** Only `server/route.js` reads it. The page gets the finished collection.
77
+ - **The browser doesn't choose the person.** The route reads `PINCAST_ACTOR` from the server's settings, and a real app takes the person from its own sign-in session. If the browser could send the id, anyone could read anyone's cards through your key.
78
+
79
+ ## Check it
80
+
81
+ ```sh
82
+ npm test # the route tests and the kit's conformance suite
83
+ npm run build # builds the page into dist
84
+ npm start # serves dist and the route on http://127.0.0.1:4173
85
+ ```
86
+
87
+ A change to the theme or the face's markup changes the goldens in `test/goldens`. Read the difference, then write them again with `UPDATE_GOLDENS=1 npm test`.
88
+
89
+ ## License
90
+
91
+ MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,8 @@
1
+ # Leave PINCAST_KEY empty and the app shows its sample cards, with no network call.
2
+
3
+ # Your app's key. It stays on the server: only server/route.js reads it.
4
+ PINCAST_KEY=
5
+
6
+ # The person whose cards to show: the id your app sends in collects.
7
+ # In a real app this comes from your own sign-in session, never from the browser.
8
+ PINCAST_ACTOR=
@@ -0,0 +1,5 @@
1
+ node_modules
2
+ dist
3
+ .env
4
+ .env.*
5
+ !.env.example