From b8d19f93a179fd37ab263fdffc1e86c21036af41 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 18 Jan 2024 15:51:30 -0600 Subject: [PATCH] feat(db): add support for pass-through `astro db` command --- packages/astro/package.json | 1 + packages/astro/src/cli/index.ts | 1 + packages/astro/src/cli/install-package.ts | 3 ++- packages/db/package.json | 6 +++--- packages/db/src/cli/bin.ts | 9 +++++++++ packages/db/src/cli/index.ts | 11 +++-------- packages/db/src/index.ts | 1 + 7 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 packages/db/src/cli/bin.ts diff --git a/packages/astro/package.json b/packages/astro/package.json index ce8dae159b..904bc46b7b 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -183,6 +183,7 @@ }, "devDependencies": { "@astrojs/check": "^0.3.1", + "@astrojs/db": "workspace:*", "@playwright/test": "1.40.0", "@types/aria-query": "^5.0.4", "@types/babel__generator": "^7.6.7", diff --git a/packages/astro/src/cli/index.ts b/packages/astro/src/cli/index.ts index a6ca88400d..77169f3806 100644 --- a/packages/astro/src/cli/index.ts +++ b/packages/astro/src/cli/index.ts @@ -30,6 +30,7 @@ async function printAstroHelp() { ['add', 'Add an integration.'], ['build', 'Build your project and write it to disk.'], ['check', 'Check your project for errors.'], + ['db', 'Manage your Astro database.'], ['dev', 'Start the development server.'], ['docs', 'Open documentation in your web browser.'], ['info', 'List info about your current Astro setup.'], diff --git a/packages/astro/src/cli/install-package.ts b/packages/astro/src/cli/install-package.ts index 667037a0cb..4c863098f6 100644 --- a/packages/astro/src/cli/install-package.ts +++ b/packages/astro/src/cli/install-package.ts @@ -20,7 +20,8 @@ export async function getPackage( ): Promise { let packageImport; try { - await tryResolve(packageName, options.cwd ?? process.cwd()); + // TODO: uncomment this when `@astrojs/db` works + // await tryResolve(packageName, options.cwd ?? process.cwd()); // The `require.resolve` is required as to avoid Node caching the failed `import` packageImport = await import(packageName); diff --git a/packages/db/package.json b/packages/db/package.json index 8cad1ede06..ac922e81bd 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -31,14 +31,14 @@ ] } }, + "bin": { + "studio": "./dist/cli/bin.js" + }, "files": [ "index.d.ts", "dist", "components" ], - "bin": { - "studio": "./dist/cli/index.js" - }, "scripts": { "build": "astro-scripts build \"src/**/*.ts\" && tsc", "build:ci": "astro-scripts build \"src/**/*.ts\"", diff --git a/packages/db/src/cli/bin.ts b/packages/db/src/cli/bin.ts new file mode 100644 index 0000000000..156ea389d7 --- /dev/null +++ b/packages/db/src/cli/bin.ts @@ -0,0 +1,9 @@ +#!/usr/bin/env node +import { cli } from './index.js'; + +async function main() { + const [command, ...args] = process.argv.slice(2); + await cli(command, args); +} + +await main(); diff --git a/packages/db/src/cli/index.ts b/packages/db/src/cli/index.ts index e5a0d70068..3bbaf46388 100644 --- a/packages/db/src/cli/index.ts +++ b/packages/db/src/cli/index.ts @@ -1,13 +1,10 @@ -#!/usr/bin/env node import { red } from 'kleur/colors'; import { astroConfigWithDbSchema } from '../config.js'; import { errorMap } from '../error-map.js'; import { loadAstroConfig } from '../load-astro-config.js'; -async function main() { - const cmd = process.argv[2]; - - switch (cmd) { +export async function cli(command: string, _args: string[] = []) { + switch (command) { case 'sync': { const { sync } = await import('./sync/index.js'); const astroConfig = await getAstroConfigOrExit(); @@ -18,7 +15,7 @@ async function main() { } default: { // eslint-disable-next-line no-console - console.error(`Unknown command: ${cmd}`); + console.error(`Unknown command: ${command}`); return; } } @@ -37,5 +34,3 @@ async function getAstroConfigOrExit(root: string = process.cwd()) { ); process.exit(0); } - -await main(); diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index f59a4ef427..96c54fc6e9 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -1,3 +1,4 @@ export { defineCollection, defineWritableCollection, field } from './config.js'; +export { cli } from './cli/index.js'; export { integration as default } from './integration.js';