Skip to content

Commit 734c00a

Browse files
committed
feat(cli): add a few keys you can press to do stuff
1 parent 29ff902 commit 734c00a

3 files changed

Lines changed: 57 additions & 9 deletions

File tree

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/workshop-app/bin/epicshop.js

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import fs from 'fs'
66
import http from 'http'
77
import path from 'path'
88
import { fileURLToPath } from 'url'
9+
import chalk from 'chalk'
910
import closeWithGrace from 'close-with-grace'
1011
import getPort from 'get-port'
12+
import open from 'open'
1113

1214
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1315
const isPublished = !fs.existsSync(path.join(__dirname, '..', 'app'))
@@ -60,9 +62,13 @@ async function start() {
6062
let server = null
6163
let child = null
6264
let restarting = false
65+
let childPort = null
6366
let childPortPromiseResolve = null
64-
const childPort = new Promise((resolve) => {
67+
const childPortPromise = new Promise((resolve) => {
6568
childPortPromiseResolve = resolve
69+
}).then((port) => {
70+
childPort = port
71+
return port
6672
})
6773

6874
function parsePortFromLine(line) {
@@ -74,7 +80,7 @@ async function start() {
7480
}
7581

7682
async function waitForChildReady() {
77-
const port = await childPort
83+
const port = await childPortPromise
7884
const url = `http://localhost:${port}/`
7985
const maxAttempts = 40 // 20s max (500ms interval)
8086
for (let i = 0; i < maxAttempts; i++) {
@@ -187,15 +193,13 @@ async function start() {
187193
if (child.stdout) {
188194
child.stdout.on('data', (data) => {
189195
process.stdout.write(data)
190-
191-
if (childPortPromiseResolve) {
196+
if (!childPort) {
192197
const str = data.toString('utf8')
193198
const lines = str.split(/\r?\n/)
194199
for (const line of lines) {
195200
const port = parsePortFromLine(line)
196-
if (port && childPortPromiseResolve) {
197-
childPortPromiseResolve(port)
198-
childPortPromiseResolve = null
201+
if (port) {
202+
childPortPromiseResolve?.(port)
199203
}
200204
}
201205
}
@@ -213,8 +217,8 @@ async function start() {
213217

214218
spawnChild()
215219

216-
// Listen for 'u' key to update and restart
217-
if (process.stdin.isTTY) {
220+
if (process.stdin.isTTY && !isDeployed) {
221+
printSupportedKeys()
218222
process.stdin.setRawMode(true)
219223
process.stdin.resume()
220224
process.stdin.setEncoding('utf8')
@@ -224,6 +228,38 @@ async function start() {
224228
'\n🔄 Update requested from terminal. Running update and restarting app process...',
225229
)
226230
await doUpdateAndRestart()
231+
} else if (key === 'o') {
232+
if (childPort) {
233+
console.log(
234+
chalk.blue(
235+
`\n🌐 Opening browser to http://localhost:${childPort} ...`,
236+
),
237+
)
238+
await open(`http://localhost:${childPort}`)
239+
} else {
240+
console.log(chalk.red('Local server URL not available yet.'))
241+
}
242+
} else if (key === 'q') {
243+
console.log(chalk.yellow('\n👋 Exiting...'))
244+
await cleanupBeforeExit()
245+
process.exit(0)
246+
} else if (key === 'r') {
247+
console.log(chalk.magenta('\n🔄 Restarting app process...'))
248+
restarting = true
249+
await killChild(child)
250+
restarting = false
251+
spawnChild()
252+
} else if (key === 'k') {
253+
const messages = [
254+
chalk.bgCyan.black('🐨 Kody says: You are koalafied for greatness!'),
255+
chalk.bgGreen.black('🐨 Kody says: Keep going, you are pawsome!'),
256+
chalk.bgMagenta.white('🐨 Kody says: Eucalyptus up and code on!'),
257+
chalk.bgYellow.black('🐨 Kody says: You can do it, fur real!'),
258+
chalk.bgBlue.white('🐨 Kody says: Stay curious, stay cuddly!'),
259+
chalk.bgRed.white("🐨 Kody says: Don't leaf your dreams behind!"),
260+
]
261+
const msg = messages[Math.floor(Math.random() * messages.length)]
262+
console.log('\n' + msg + '\n')
227263
} else if (key === '\u0003') {
228264
// Ctrl+C
229265
await cleanupBeforeExit()
@@ -251,3 +287,13 @@ async function killChild(child) {
251287
child.kill()
252288
})
253289
}
290+
291+
function printSupportedKeys() {
292+
console.log(chalk.bold.cyan('\nSupported keys:'))
293+
console.log(` ${chalk.green('u')} - update repo`)
294+
console.log(` ${chalk.blue('o')} - open browser`)
295+
console.log(` ${chalk.yellow('q')} - exit`)
296+
console.log(` ${chalk.magenta('r')} - restart`)
297+
console.log(` ${chalk.cyan('k')} - Kody the Koala encouragement 🐨`)
298+
console.log(` ${chalk.gray('Ctrl+C')} - exit`)
299+
}

packages/workshop-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"mime-types": "^2.1.35",
9393
"morgan": "^1.10.0",
9494
"msw": "^2.4.9",
95+
"open": "^8.4.2",
9596
"openid-client": "^6.1.7",
9697
"p-queue": "^8.0.1",
9798
"partysocket": "^1.0.2",

0 commit comments

Comments
 (0)