Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cli flag to configure base directory of srcbooks #429

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/itchy-crabs-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@srcbook/api': patch
'srcbook': patch
---

Add flag to change base dir of srcbooks
2 changes: 1 addition & 1 deletion packages/api/constants.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const _filename = fileURLToPath(import.meta.url);
const _dirname = path.dirname(_filename);

export const HOME_DIR = os.homedir();
export const HOME_DIR = process.env.BASE_DIR || os.homedir();

Check failure on line 11 in packages/api/constants.mts

View workflow job for this annotation

GitHub Actions / Build and Test (18.x)

BASE_DIR is not listed as a dependency in turbo.json

Check failure on line 11 in packages/api/constants.mts

View workflow job for this annotation

GitHub Actions / Build and Test (22.x)

BASE_DIR is not listed as a dependency in turbo.json
export const SRCBOOK_DIR = path.join(HOME_DIR, '.srcbook');
export const SRCBOOKS_DIR = path.join(SRCBOOK_DIR, 'srcbooks');
export const APPS_DIR = path.join(SRCBOOK_DIR, 'apps');
Expand Down
14 changes: 9 additions & 5 deletions srcbook/src/cli.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os from 'node:os';
import { spawn } from 'node:child_process';
import { Command } from 'commander';
import { pathTo, getPackageJson, isPortAvailable } from './utils.mjs';
Expand All @@ -10,14 +11,15 @@ function openInBrowser(url: string) {
);
}

function startServer(port: string, callback: () => void) {
function startServer(port: string, baseDir: string, callback: () => void) {
const server = spawn('node', [pathTo('dist', 'src', 'server.mjs')], {
// Inherit stdio configurations from CLI (parent) process and allow IPC
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env: {
...process.env,
NODE_ENV: 'production',
PORT: port,
BASE_DIR: baseDir,
},
});

Expand Down Expand Up @@ -52,8 +54,9 @@ export default function program() {
.command('start')
.description('Start the Srcbook server')
.option('-p, --port <port>', 'Port to run the server on', '2150')
.action(({ port }) => {
startServer(port, () => {
.option('-b, --base-dir <dir>', 'Base directory containing srcbook/ dir', os.homedir())
.action(({ port, baseDir }) => {
startServer(port, baseDir, () => {
openInBrowser(`http://localhost:${port}`);
});
});
Expand All @@ -62,15 +65,16 @@ export default function program() {
.command('import')
.description('Import a Srcbook')
.option('-p, --port <port>', 'Port of the server', '2150')
.option('-b, --base-dir <dir>', 'Base directory containing srcbook/ dir', os.homedir())
.argument('<specifier>', 'An identifier of a Srcbook on hub.srcbook.com')
.action(async (specifier, { port }) => {
.action(async (specifier, { port, baseDir }) => {
const portAvailable = await isPortAvailable('localhost', port);

if (portAvailable) {
return doImport(specifier, port);
}

startServer(port, () => {
startServer(port, baseDir, () => {
doImport(specifier, port);
});
});
Expand Down
Loading