create-ifloom 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.
Files changed (132) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +18 -2
  3. package/bin.js +4 -0
  4. package/dist/bin.js +391 -0
  5. package/dist/bin.js.map +1 -0
  6. package/dist/examples/cafe/index.html +12 -0
  7. package/dist/examples/cafe/package.json +24 -0
  8. package/dist/examples/cafe/src/game.ts +41 -0
  9. package/dist/examples/cafe/src/layout.tsx +49 -0
  10. package/dist/examples/cafe/src/main.ts +7 -0
  11. package/dist/examples/cafe/src/rules.ts +38 -0
  12. package/dist/examples/cafe/src/story/hiring.ts +25 -0
  13. package/dist/examples/cafe/src/story/opening.ts +19 -0
  14. package/dist/examples/cafe/src/styles.css +7 -0
  15. package/dist/examples/cafe/src/world/actions.ts +40 -0
  16. package/dist/examples/cafe/src/world/menu.ts +14 -0
  17. package/dist/examples/cafe/src/world/staff.ts +12 -0
  18. package/dist/examples/cafe/src/world/stats.ts +10 -0
  19. package/dist/examples/cafe/src/world/triggers.ts +18 -0
  20. package/dist/examples/cafe/tsconfig.json +9 -0
  21. package/dist/examples/cafe/vite.config.ts +7 -0
  22. package/dist/examples/novel/index.html +12 -0
  23. package/dist/examples/novel/package.json +24 -0
  24. package/dist/examples/novel/src/game.ts +49 -0
  25. package/dist/examples/novel/src/layout.tsx +25 -0
  26. package/dist/examples/novel/src/main.ts +8 -0
  27. package/dist/examples/novel/src/story/ch1.ts +36 -0
  28. package/dist/examples/novel/src/story/ch2.ts +42 -0
  29. package/dist/examples/novel/src/story/ch3.ts +41 -0
  30. package/dist/examples/novel/src/styles.css +7 -0
  31. package/dist/examples/novel/src/world/stats.ts +6 -0
  32. package/dist/examples/novel/tsconfig.json +9 -0
  33. package/dist/examples/novel/vite.config.ts +7 -0
  34. package/dist/examples/showcase/.env +4 -0
  35. package/dist/examples/showcase/index.html +12 -0
  36. package/dist/examples/showcase/package.json +43 -0
  37. package/dist/examples/showcase/public/art/character/ava.svg +1 -0
  38. package/dist/examples/showcase/public/art/character/sam.svg +1 -0
  39. package/dist/examples/showcase/public/art/photo/ava.svg +1 -0
  40. package/dist/examples/showcase/public/art/photo/sam.svg +1 -0
  41. package/dist/examples/showcase/src/game.ts +168 -0
  42. package/dist/examples/showcase/src/layout.tsx +84 -0
  43. package/dist/examples/showcase/src/main.tsx +55 -0
  44. package/dist/examples/showcase/src/place.ts +8 -0
  45. package/dist/examples/showcase/src/rules.ts +51 -0
  46. package/dist/examples/showcase/src/story/ava.prose.ts +12 -0
  47. package/dist/examples/showcase/src/story/ava.ts +199 -0
  48. package/dist/examples/showcase/src/story/cafe.ts +157 -0
  49. package/dist/examples/showcase/src/story/flat.ts +90 -0
  50. package/dist/examples/showcase/src/story/high-street.ts +37 -0
  51. package/dist/examples/showcase/src/story/moving-day.ts +74 -0
  52. package/dist/examples/showcase/src/story/office.ts +26 -0
  53. package/dist/examples/showcase/src/story/park.ts +172 -0
  54. package/dist/examples/showcase/src/story/sam.ts +27 -0
  55. package/dist/examples/showcase/src/styles.css +8 -0
  56. package/dist/examples/showcase/src/ui.ts +44 -0
  57. package/dist/examples/showcase/src/unlocks/supporter.ts +40 -0
  58. package/dist/examples/showcase/src/world/achievements.ts +13 -0
  59. package/dist/examples/showcase/src/world/actions.ts +13 -0
  60. package/dist/examples/showcase/src/world/cast.ts +46 -0
  61. package/dist/examples/showcase/src/world/cheats.ts +33 -0
  62. package/dist/examples/showcase/src/world/phone.ts +88 -0
  63. package/dist/examples/showcase/src/world/settings.ts +24 -0
  64. package/dist/examples/showcase/src/world/stats.ts +24 -0
  65. package/dist/examples/showcase/src/world/strings.ts +7 -0
  66. package/dist/examples/showcase/src/world/things.ts +59 -0
  67. package/dist/examples/showcase/tsconfig.json +9 -0
  68. package/dist/examples/showcase/vite.config.ts +7 -0
  69. package/dist/templates/next/AGENTS.md +60 -0
  70. package/dist/templates/next/README.md +33 -0
  71. package/dist/templates/next/app/Game.tsx +46 -0
  72. package/dist/templates/next/app/layout.tsx +13 -0
  73. package/dist/templates/next/app/page.tsx +6 -0
  74. package/dist/templates/next/gitignore +6 -0
  75. package/dist/templates/next/next.config.ts +6 -0
  76. package/dist/templates/next/package.json +32 -0
  77. package/dist/templates/next/postcss.config.mjs +6 -0
  78. package/dist/templates/next/src/layout.tsx +73 -0
  79. package/dist/templates/next/src/styles.css +16 -0
  80. package/dist/templates/next/tsconfig.json +21 -0
  81. package/dist/templates/plugin/README.md +81 -0
  82. package/dist/templates/plugin/gitignore +2 -0
  83. package/dist/templates/plugin/package.json +64 -0
  84. package/dist/templates/plugin/src/index.ts +162 -0
  85. package/dist/templates/plugin/src/react/index.tsx +95 -0
  86. package/dist/templates/plugin/tests/contracts.test.ts +28 -0
  87. package/dist/templates/plugin/tests/plugin.test.ts +59 -0
  88. package/dist/templates/plugin/tests/react.test.tsx +47 -0
  89. package/dist/templates/plugin/tsconfig.json +21 -0
  90. package/dist/templates/plugin/tsdown.config.ts +14 -0
  91. package/dist/templates/react/package.json +28 -0
  92. package/dist/templates/react/src/layout.tsx +69 -0
  93. package/dist/templates/react/src/main.ts +11 -0
  94. package/dist/templates/react/src/styles.css +16 -0
  95. package/dist/templates/react/vite.config.ts +7 -0
  96. package/dist/templates/shared/AGENTS.md +58 -0
  97. package/dist/templates/shared/gitignore +3 -0
  98. package/dist/templates/shared/index.html +12 -0
  99. package/dist/templates/shared/src/game.ts +23 -0
  100. package/dist/templates/shared/src/story/opening.ts +34 -0
  101. package/dist/templates/shared/src/styles.css +228 -0
  102. package/dist/templates/shared/src/world/stats.ts +6 -0
  103. package/dist/templates/shared/tsconfig.json +9 -0
  104. package/dist/templates/shared/vite.config.ts +5 -0
  105. package/dist/templates/svelte/package.json +25 -0
  106. package/dist/templates/svelte/src/App.svelte +11 -0
  107. package/dist/templates/svelte/src/Beat.svelte +31 -0
  108. package/dist/templates/svelte/src/InputField.svelte +36 -0
  109. package/dist/templates/svelte/src/Layout.svelte +84 -0
  110. package/dist/templates/svelte/src/Line.svelte +26 -0
  111. package/dist/templates/svelte/src/Minigame.svelte +21 -0
  112. package/dist/templates/svelte/src/Panel.svelte +27 -0
  113. package/dist/templates/svelte/src/main.ts +5 -0
  114. package/dist/templates/svelte/src/runner.ts +29 -0
  115. package/dist/templates/svelte/vite.config.ts +5 -0
  116. package/dist/templates/vanilla/package.json +22 -0
  117. package/dist/templates/vanilla/src/layout.ts +35 -0
  118. package/dist/templates/vanilla/src/main.ts +253 -0
  119. package/dist/templates/vue/package.json +25 -0
  120. package/dist/templates/vue/src/App.vue +12 -0
  121. package/dist/templates/vue/src/Beat.vue +28 -0
  122. package/dist/templates/vue/src/InputField.vue +34 -0
  123. package/dist/templates/vue/src/Layout.vue +84 -0
  124. package/dist/templates/vue/src/Line.vue +41 -0
  125. package/dist/templates/vue/src/Minigame.vue +19 -0
  126. package/dist/templates/vue/src/Panel.vue +30 -0
  127. package/dist/templates/vue/src/main.ts +5 -0
  128. package/dist/templates/vue/src/runner.ts +32 -0
  129. package/dist/templates/vue/vite.config.ts +5 -0
  130. package/package.json +48 -4
  131. package/src/bin.ts +18 -0
  132. package/src/create.ts +89 -0
@@ -0,0 +1,84 @@
1
+ <script setup lang="ts">
2
+ import { ref } from 'vue'
3
+ import { runner, view } from './runner.ts'
4
+ import Beat from './Beat.vue'
5
+ import InputField from './InputField.vue'
6
+ import Minigame from './Minigame.vue'
7
+
8
+ // The page, in three parts: the side column, the story, and the bottom of the
9
+ // story with the buttons for where you are. Move, add or remove anything; a
10
+ // section is a <details>, so it folds without any code.
11
+
12
+ // One save slot, and what happened the last time it was used.
13
+ const said = ref('')
14
+ async function save() {
15
+ const saved = await runner.save('quick')
16
+ said.value = saved.ok ? 'Saved.' : saved.reason
17
+ }
18
+ async function load() {
19
+ const loaded = await runner.load('quick')
20
+ said.value = loaded.ok ? 'Loaded.' : loaded.reason
21
+ }
22
+ </script>
23
+
24
+ <template>
25
+ <div class="page">
26
+ <aside class="sidebar">
27
+ <h1>{{ view.title }}</h1>
28
+ <!-- Where the game is in time, if a plugin says. -->
29
+ <p v-if="view.when" class="clock">{{ view.when }}</p>
30
+ <details v-if="view.stats.length" class="section" open>
31
+ <summary>Stats</summary>
32
+ <dl class="stats">
33
+ <template v-for="stat in view.stats" :key="stat.key">
34
+ <dt>{{ stat.label }}</dt>
35
+ <dd>{{ stat.text }}</dd>
36
+ </template>
37
+ </dl>
38
+ </details>
39
+ <!-- Saving. While the game's `saveable` refuses it, Save is greyed out with why (`hide` leaves it off). -->
40
+ <details class="section" open>
41
+ <summary>Saves</summary>
42
+ <div class="saves">
43
+ <button v-if="view.saving.visible" :disabled="!view.saving.allowed" :title="view.saving.reason ?? undefined" @click="save">Save</button>
44
+ <button @click="load">Load</button>
45
+ </div>
46
+ <p v-if="view.saving.visible && view.saving.reason" class="reason">{{ view.saving.reason }}</p>
47
+ <p v-if="said" class="said">{{ said }}</p>
48
+ </details>
49
+ </aside>
50
+
51
+ <main class="story">
52
+ <!-- While a minigame runs, the scene's title and beats are the minigame's. -->
53
+ <h2 v-if="view.scene.title">{{ view.scene.title }}</h2>
54
+ <div class="prose">
55
+ <Beat v-for="(beat, at) in view.scene.beats" :key="at" :beat="beat" />
56
+ </div>
57
+ <Minigame v-if="view.minigame" :minigame="view.minigame" />
58
+
59
+ <div class="inputs">
60
+ <InputField v-for="input in view.inputs" :key="input.id" :input="input" />
61
+ </div>
62
+
63
+ <div class="choices">
64
+ <template v-for="(choice, at) in view.scene.choices" :key="at">
65
+ <button :disabled="Boolean(choice.disabled)" :title="choice.disabled" @click="runner.apply(choice.command)">
66
+ {{ choice.label }}
67
+ </button>
68
+ <!-- Why it can't be pressed, such as "You need 4 coins." -->
69
+ <p v-if="choice.disabled" class="reason">{{ choice.disabled }}</p>
70
+ </template>
71
+ </div>
72
+
73
+ <!-- The bottom of the story: the game's own buttons and its plugins', such as a place's exits and things to do. -->
74
+ <div v-if="view.actions.length" class="bottom choices">
75
+ <template v-for="(action, at) in view.actions" :key="at">
76
+ <button :disabled="Boolean(action.disabled)" :title="action.disabled" @click="runner.apply(action.command)">
77
+ {{ action.label }}
78
+ </button>
79
+ <p v-if="action.disabled" class="reason">{{ action.disabled }}</p>
80
+ </template>
81
+ </div>
82
+ </main>
83
+ </div>
84
+ </template>
@@ -0,0 +1,41 @@
1
+ <script setup lang="ts">
2
+ import { ref, useId } from 'vue'
3
+ import type { InlineSpan } from '@ifloom/engine'
4
+ import { runner } from './runner.ts'
5
+
6
+ // A line's words, with bold and italics, and its inline actions and tooltips as buttons in it. A tooltip, or a
7
+ // greyed-out action's reason, is a note after the button that shows on hover, on focus
8
+ // and on a tap (see `.note` in styles.css). A UI piece (`[[ui:…]]`) shows its words, styled by its name.
9
+ defineProps<{ text: string; spans?: InlineSpan[] }>()
10
+ const id = useId()
11
+ const open = ref<number | null>(null)
12
+
13
+ function press(span: InlineSpan, at: number) {
14
+ if (span.type === 'action' && !span.choice.disabled) runner.apply(span.choice.command)
15
+ else open.value = open.value === at ? null : at
16
+ }
17
+ const note = (span: InlineSpan) => (span.type === 'tip' ? span.tip : span.type === 'action' ? span.choice.disabled : undefined)
18
+ </script>
19
+
20
+ <template>
21
+ <template v-if="!spans">{{ text }}</template>
22
+ <template v-for="(span, at) in spans" v-else :key="at">
23
+ <template v-if="span.type === 'text'">{{ span.text }}</template>
24
+ <strong v-else-if="span.type === 'bold'">{{ span.text }}</strong>
25
+ <em v-else-if="span.type === 'italic'">{{ span.text }}</em>
26
+ <span v-else-if="span.type === 'ui'" class="ui" :data-ui="span.name">{{ span.text }}</span>
27
+ <template v-else>
28
+ <button
29
+ type="button"
30
+ :class="span.type === 'tip' ? 'tip' : 'inline-action'"
31
+ :aria-disabled="span.type === 'action' && span.choice.disabled ? 'true' : undefined"
32
+ :aria-describedby="note(span) ? `${id}-${at}` : undefined"
33
+ :aria-expanded="note(span) ? open === at : undefined"
34
+ @click="press(span, at)"
35
+ >
36
+ {{ span.text }}
37
+ </button>
38
+ <span v-if="note(span)" :id="`${id}-${at}`" role="tooltip" class="note">{{ note(span) }}</span>
39
+ </template>
40
+ </template>
41
+ </template>
@@ -0,0 +1,19 @@
1
+ <script setup lang="ts">
2
+ import type { MinigameView } from '@ifloom/engine'
3
+ import { runner } from './runner.ts'
4
+
5
+ // The minigame being played: a bar counting down its timer, and a button for each input that does something now,
6
+ // in the words the game's `minigame.<input>` strings give it.
7
+ defineProps<{ minigame: MinigameView }>()
8
+ </script>
9
+
10
+ <template>
11
+ <section class="minigame" :data-state="minigame.state">
12
+ <div v-if="minigame.timeLeft !== null && minigame.timeTotal" class="timer" role="progressbar" aria-label="Time left">
13
+ <span :style="{ transform: `scaleX(${minigame.timeLeft / minigame.timeTotal})` }" />
14
+ </div>
15
+ <div class="choices">
16
+ <button v-for="input in minigame.inputs" :key="input.id" @click="runner.apply(input.command)">{{ input.label }}</button>
17
+ </div>
18
+ </section>
19
+ </template>
@@ -0,0 +1,30 @@
1
+ <script setup lang="ts">
2
+ import { onBeforeUnmount, onMounted, useTemplateRef } from 'vue'
3
+ import type { OpenPanel } from '@ifloom/engine'
4
+ import { runner } from './runner.ts'
5
+
6
+ // The panel open over the story, `view.panel`: a shop's counter, a job board. A `panel` effect opens one, and so does a scene's `panel` while it's open.
7
+ // Draw each one your game opens by its name, from what it was opened with. Closing it sends `closePanel`; a scene's own panel has no close button, since it stays while the scene is open.
8
+ const props = defineProps<{ panel: OpenPanel }>()
9
+
10
+ // A modal <dialog>: the browser keeps focus inside it and the page behind it out of reach while it's open.
11
+ const dialog = useTemplateRef<HTMLDialogElement>('dialog')
12
+ onMounted(() => {
13
+ if (dialog.value && !dialog.value.open) dialog.value.showModal()
14
+ })
15
+ onBeforeUnmount(() => dialog.value?.close())
16
+
17
+ // Escape closes it, as Close does. A scene's own panel stays.
18
+ function cancel(event: Event) {
19
+ event.preventDefault()
20
+ if (!props.panel.scene) runner.apply({ type: 'closePanel' })
21
+ }
22
+ </script>
23
+
24
+ <template>
25
+ <dialog ref="dialog" class="panel" :aria-label="panel.panel" :data-panel="panel.panel" @cancel="cancel">
26
+ <h2>{{ panel.panel }}</h2>
27
+ <pre>{{ JSON.stringify(panel.with, null, 2) }}</pre>
28
+ <button v-if="!panel.scene" @click="runner.apply({ type: 'closePanel' })">Close</button>
29
+ </dialog>
30
+ </template>
@@ -0,0 +1,5 @@
1
+ import { createApp } from 'vue'
2
+ import App from './App.vue'
3
+ import './styles.css'
4
+
5
+ createApp(App).mount('#root')
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The game's runner, and its snapshot as Vue state. Components read `view`
3
+ * (or the whole `snapshot`: settings, backlog, undo, saves) and send
4
+ * commands with `runner.apply`; nothing else talks to the engine.
5
+ */
6
+ import { computed, shallowRef } from 'vue'
7
+ import { startAudio } from '@ifloom/audio'
8
+ import { browserRunner, startClock, startKeys } from '@ifloom/browser'
9
+ import module from './game.ts'
10
+
11
+ // The game module builds the game, and says what the loader found wrong with it.
12
+ const { game, problems } = module.build()
13
+ if (problems.length) console.warn(problems)
14
+
15
+ // A new game with a random seed, undo, saves in localStorage and an autosave each turn.
16
+ export const runner = await browserRunner(game)
17
+
18
+ // The runner hands every listener a new snapshot whenever anything in it changes, so a
19
+ // shallowRef is enough: Vue only needs to know that it changed, not to look inside it.
20
+ export const snapshot = shallowRef(runner.snapshot())
21
+ runner.subscribe((next) => {
22
+ snapshot.value = next
23
+ })
24
+ // The view, which is most of what the components draw.
25
+ export const view = computed(() => snapshot.value.view)
26
+
27
+ // Plays what the game's `audio` asks for, from the first click or key press.
28
+ // Its `play` plays a file on demand, such as a voice line again.
29
+ export const player = startAudio(runner)
30
+ // Ticks the game's timers and a minigame's, and maps the keyboard: 1 to 9 pick the choices, and a minigame's keys press its inputs.
31
+ startClock(runner)
32
+ startKeys(runner)
@@ -0,0 +1,5 @@
1
+ import vue from '@vitejs/plugin-vue'
2
+ import { ifloom } from '@ifloom/cli/vite'
3
+
4
+ // The preset knows no framework, so the game adds Vue's plugin.
5
+ export default ifloom({ vite: { plugins: [vue()] } })
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "create-ifloom",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "description": "Start a new IFLoom game: npm create ifloom@latest my-game.",
6
+ "keywords": [
7
+ "ifloom",
8
+ "interactive-fiction",
9
+ "create",
10
+ "scaffold",
11
+ "template"
12
+ ],
13
+ "homepage": "https://ifloom.dev/docs/how-to/create-a-game",
14
+ "bugs": {
15
+ "url": "https://github.com/IFLoom/ifloom/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/IFLoom/ifloom.git",
20
+ "directory": "packages/create-ifloom"
21
+ },
22
+ "type": "module",
23
+ "bin": {
24
+ "create-ifloom": "bin.js"
25
+ },
26
+ "imports": {
27
+ "#bin": {
28
+ "ifloom-source": "./src/bin.ts",
29
+ "default": "./dist/bin.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "bin.js",
34
+ "dist",
35
+ "src"
36
+ ],
37
+ "scripts": {
38
+ "build": "tsdown"
39
+ },
40
+ "engines": {
41
+ "node": "^20.19.0 || >=22.12.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public",
45
+ "provenance": true
46
+ },
47
+ "devDependencies": {
48
+ "@ifloom/cli": "^0.1.0"
49
+ }
50
+ }
package/src/bin.ts ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * create-ifloom — a new IFLoom game, for `npm create ifloom`:
4
+ *
5
+ * npm create ifloom@latest my-game
6
+ * npm create ifloom@latest my-game -- --template vue
7
+ * pnpm create ifloom my-game --theme paper
8
+ *
9
+ * Makes the game in ./my-game and prints what to run next. See create.ts.
10
+ */
11
+ import { create } from './create.ts'
12
+
13
+ process.exitCode = create(process.argv.slice(2), {
14
+ cwd: process.cwd(),
15
+ userAgent: process.env.npm_config_user_agent,
16
+ log: (line) => console.log(line),
17
+ error: (line) => console.error(line),
18
+ })
package/src/create.ts ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * create-ifloom: what `npm create ifloom@latest my-game` runs. It makes a new
3
+ * game in the folder `my-game` with the CLI's `newGame`, the same as `ifloom
4
+ * new`, with the same `--template` and `--theme`, and says what to run next
5
+ * with the package manager that ran it. It installs nothing.
6
+ */
7
+ import { writeFileSync } from 'node:fs'
8
+ import { join, relative, resolve } from 'node:path'
9
+ import { TEMPLATES, THEMES, newGame, type TemplateName, type ThemeName } from '@ifloom/cli'
10
+
11
+ export const USAGE = `npm create ifloom@latest <name> -- [--template ${TEMPLATES.join('|')}] [--theme ${THEMES.join('|')}|none]`
12
+
13
+ export type Args = { name?: string; template?: string; theme?: string; help: boolean }
14
+
15
+ /** The arguments after the command: a name, and `--template` and `--theme` with a value each, as `--template vue` or `--template=vue`. */
16
+ export function parseArgs(argv: string[]): Args | { error: string } {
17
+ const args: Args = { help: false }
18
+ for (let at = 0; at < argv.length; at++) {
19
+ const arg = argv[at]
20
+ if (arg === '--help' || arg === '-h') {
21
+ args.help = true
22
+ } else if (arg.startsWith('--')) {
23
+ const [flag, inline] = arg.slice(2).split(/=(.*)/s)
24
+ if (flag !== 'template' && flag !== 'theme') return { error: `There is no option --${flag}. ${USAGE}` }
25
+ const value = inline ?? argv[++at]
26
+ if (!value || value.startsWith('--')) return { error: `--${flag} needs a value. ${USAGE}` }
27
+ args[flag] = value
28
+ } else if (args.name === undefined) {
29
+ args.name = arg
30
+ } else {
31
+ return { error: `One name only: "${args.name}" or "${arg}". ${USAGE}` }
32
+ }
33
+ }
34
+ return args
35
+ }
36
+
37
+ export type PackageManager = { name: 'npm' | 'pnpm' | 'yarn' | 'bun'; major: number }
38
+
39
+ /** The package manager that ran the command, from the `npm_config_user_agent` each of them sets, such as `pnpm/10.4.1 npm/? node/v24.1.0 linux x64`. npm if it's none of them. */
40
+ export function packageManager(userAgent: string | undefined): PackageManager {
41
+ const [name, version = ''] = (userAgent ?? '').split(' ')[0].split('/')
42
+ const major = Number.parseInt(version, 10) || 0
43
+ return name === 'pnpm' || name === 'yarn' || name === 'bun' ? { name, major } : { name: 'npm', major }
44
+ }
45
+
46
+ /** What to type next: into the folder, install, then play the game (or test the plugin). */
47
+ export function nextSteps(pm: PackageManager['name'], folder: string, template: TemplateName): string[] {
48
+ // `yarn check` and `bun test` are those tools' own commands, so a script always goes through `run` there.
49
+ const run = (script: string) => (pm === 'npm' ? `npm run ${script}` : pm === 'pnpm' ? `pnpm ${script}` : `${pm} run ${script}`)
50
+ return [...(folder === '.' ? [] : [`cd ${folder}`]), `${pm} install`, template === 'plugin' ? (pm === 'npm' ? 'npm test' : run('test')) : run('dev')]
51
+ }
52
+
53
+ export type Io = { cwd: string; userAgent: string | undefined; log: (line: string) => void; error: (line: string) => void }
54
+
55
+ /** Make the game, and print what was made and what to type next. Returns the exit code. */
56
+ export function create(argv: string[], io: Io): number {
57
+ const args = parseArgs(argv)
58
+ if ('error' in args) {
59
+ io.error(args.error)
60
+ return 1
61
+ }
62
+ if (args.help) {
63
+ io.log(USAGE)
64
+ return 0
65
+ }
66
+ if (!args.name) {
67
+ io.error(`Give the new game a name: ${USAGE}`)
68
+ return 1
69
+ }
70
+ const pm = packageManager(io.userAgent)
71
+ const template = (args.template ?? 'react') as TemplateName
72
+ try {
73
+ const made = newGame({ name: args.name, template, theme: args.theme as ThemeName | 'none' | undefined, into: resolve(io.cwd, args.name) })
74
+ // Yarn 2 and later install with Plug'n'Play unless told otherwise, and Vite needs a node_modules folder.
75
+ if (pm.name === 'yarn' && pm.major >= 2) writeFileSync(join(made.dir, '.yarnrc.yml'), 'nodeLinker: node-modules\n')
76
+ const look = made.theme ? `, with the ${made.theme} theme` : args.theme === 'none' ? ', with no theme preset' : ''
77
+ io.log(`Made ${args.name} from the ${template} template${look}. Next:\n`)
78
+ for (const line of nextSteps(pm.name, relative(io.cwd, made.dir) || '.', template)) io.log(` ${line}`)
79
+ io.log(
80
+ template === 'plugin'
81
+ ? '\nWriting a plugin: https://ifloom.dev/docs/how-to/write-a-plugin'
82
+ : '\nYour first story: https://ifloom.dev/docs/tutorial/story/start-a-story',
83
+ )
84
+ return 0
85
+ } catch (error) {
86
+ io.error((error as Error).message)
87
+ return 1
88
+ }
89
+ }