Skip to content

Commit

Permalink
Open srcbook in browser on launch (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjreinhart authored Jul 24, 2024
1 parent 4fe6312 commit 83b0167
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 25 deletions.
115 changes: 94 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions srcbook/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import './lib/db/index.mjs';
*
*/
import readline from 'node:readline';
import express from 'express';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import { fileURLToPath } from 'url';
import path from 'path';
import { fileURLToPath } from 'node:url';
import express from 'express';
import { WebSocketServer as WsWebSocketServer } from 'ws';
import { wss, app, posthog } from './lib/index.mjs';
import chalk from 'chalk';
import open from 'open';

function clearScreen() {
const repeatCount = process.stdout.rows - 2;
Expand Down Expand Up @@ -56,11 +58,18 @@ app.get('*', (_req, res) => res.sendFile(INDEX_HTML));
console.log(chalk.green('Initialization complete.'));

const port = process.env.PORT || 2150;
const url = `http://localhost:${port}`;

posthog.capture({ event: 'user started Srcbook application' });

const packageJsonPath = path.join(__dirname, 'package.json');
const packageJson = fs.readFileSync(packageJsonPath, 'utf-8');
const { name, version } = JSON.parse(packageJson);

server.listen(port, () => {
console.log(`Running at http://localhost:${port}`);
console.log(`${name}@${version} running at ${url}`);
const noop = () => {};
open(url).catch(noop);
});

process.on('SIGINT', async function () {
Expand Down
1 change: 1 addition & 0 deletions srcbook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"drizzle-orm": "^0.31.2",
"express": "^4.19.2",
"marked": "catalog:",
"open": "^10.1.0",
"posthog-node": "^4.0.1",
"ws": "catalog:",
"zod": "catalog:"
Expand Down

0 comments on commit 83b0167

Please sign in to comment.