From 8e3c161b1e5d4ebacdd67a49aa513720d424bd13 Mon Sep 17 00:00:00 2001 From: Shikher Verma Date: Tue, 29 Oct 2024 00:43:54 +0530 Subject: [PATCH] Add a cli flag to configure base directory of srcbooks --- .changeset/itchy-crabs-repeat.md | 6 ++++++ packages/api/constants.mts | 2 +- srcbook/src/cli.mts | 14 +++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 .changeset/itchy-crabs-repeat.md diff --git a/.changeset/itchy-crabs-repeat.md b/.changeset/itchy-crabs-repeat.md new file mode 100644 index 00000000..fcfce451 --- /dev/null +++ b/.changeset/itchy-crabs-repeat.md @@ -0,0 +1,6 @@ +--- +'@srcbook/api': patch +'srcbook': patch +--- + +Add flag to change base dir of srcbooks diff --git a/packages/api/constants.mts b/packages/api/constants.mts index d742b559..82bdf1bc 100644 --- a/packages/api/constants.mts +++ b/packages/api/constants.mts @@ -8,7 +8,7 @@ import { fileURLToPath } from 'url'; 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(); 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'); diff --git a/srcbook/src/cli.mts b/srcbook/src/cli.mts index 25d4d419..4a7a8525 100755 --- a/srcbook/src/cli.mts +++ b/srcbook/src/cli.mts @@ -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'; @@ -10,7 +11,7 @@ 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'], @@ -18,6 +19,7 @@ function startServer(port: string, callback: () => void) { ...process.env, NODE_ENV: 'production', PORT: port, + BASE_DIR: baseDir, }, }); @@ -52,8 +54,9 @@ export default function program() { .command('start') .description('Start the Srcbook server') .option('-p, --port ', 'Port to run the server on', '2150') - .action(({ port }) => { - startServer(port, () => { + .option('-b, --base-dir ', 'Base directory containing srcbook/ dir', os.homedir()) + .action(({ port, baseDir }) => { + startServer(port, baseDir, () => { openInBrowser(`http://localhost:${port}`); }); }); @@ -62,15 +65,16 @@ export default function program() { .command('import') .description('Import a Srcbook') .option('-p, --port ', 'Port of the server', '2150') + .option('-b, --base-dir ', 'Base directory containing srcbook/ dir', os.homedir()) .argument('', '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); }); });