summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/silent-socks-return.md5
-rw-r--r--.changeset/young-plums-doubt.md5
-rw-r--r--packages/db/package.json1
-rw-r--r--packages/db/src/core/cli/commands/execute/index.ts2
-rw-r--r--packages/db/src/core/cli/commands/link/index.ts5
-rw-r--r--packages/db/src/core/cli/commands/login/index.ts3
-rw-r--r--packages/db/src/core/cli/commands/logout/index.ts2
-rw-r--r--packages/db/src/core/cli/commands/push/index.ts2
-rw-r--r--packages/db/src/core/cli/commands/shell/index.ts2
-rw-r--r--packages/db/src/core/cli/commands/verify/index.ts2
-rw-r--r--packages/db/src/core/errors.ts16
-rw-r--r--packages/db/src/core/integration/index.ts2
-rw-r--r--packages/db/src/core/utils.ts11
-rw-r--r--packages/studio/README.md34
-rw-r--r--packages/studio/package.json47
-rw-r--r--packages/studio/src/core/errors.ts15
-rw-r--r--packages/studio/src/core/tokens.ts (renamed from packages/db/src/core/tokens.ts)20
-rw-r--r--packages/studio/src/core/utils.ts11
-rw-r--r--packages/studio/src/index.ts3
-rw-r--r--packages/studio/tsconfig.json7
-rw-r--r--pnpm-lock.yaml28
21 files changed, 186 insertions, 37 deletions
diff --git a/.changeset/silent-socks-return.md b/.changeset/silent-socks-return.md
new file mode 100644
index 000000000..d0e7b9b16
--- /dev/null
+++ b/.changeset/silent-socks-return.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/db": patch
+---
+
+Internal refactor, this change should have no visible effect
diff --git a/.changeset/young-plums-doubt.md b/.changeset/young-plums-doubt.md
new file mode 100644
index 000000000..64be9968c
--- /dev/null
+++ b/.changeset/young-plums-doubt.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/studio": minor
+---
+
+Created package. This package contain shared code between integrations that interact with Astro Studio and is not intended to be used by end-users at this time
diff --git a/packages/db/package.json b/packages/db/package.json
index 81fcd76b2..912416658 100644
--- a/packages/db/package.json
+++ b/packages/db/package.json
@@ -70,6 +70,7 @@
"test:match": "mocha --timeout 20000 \"test/*.js\" \"test/unit/*.js\" -g"
},
"dependencies": {
+ "@astrojs/studio": "workspace:*",
"@libsql/client": "^0.6.0",
"async-listen": "^3.0.1",
"ci-info": "^4.0.0",
diff --git a/packages/db/src/core/cli/commands/execute/index.ts b/packages/db/src/core/cli/commands/execute/index.ts
index b596d4c0d..e9421d97d 100644
--- a/packages/db/src/core/cli/commands/execute/index.ts
+++ b/packages/db/src/core/cli/commands/execute/index.ts
@@ -14,7 +14,7 @@ import {
getStudioVirtualModContents,
} from '../../../integration/vite-plugin-db.js';
import { bundleFile, importBundledFile } from '../../../load-file.js';
-import { getManagedAppTokenOrExit } from '../../../tokens.js';
+import { getManagedAppTokenOrExit } from '@astrojs/studio';
import type { DBConfig } from '../../../types.js';
export async function cmd({
diff --git a/packages/db/src/core/cli/commands/link/index.ts b/packages/db/src/core/cli/commands/link/index.ts
index 9ac201e76..e0498f9c4 100644
--- a/packages/db/src/core/cli/commands/link/index.ts
+++ b/packages/db/src/core/cli/commands/link/index.ts
@@ -6,9 +6,8 @@ import { bgRed, cyan } from 'kleur/colors';
import ora from 'ora';
import prompts from 'prompts';
import { safeFetch } from '../../../../runtime/utils.js';
-import { MISSING_SESSION_ID_ERROR } from '../../../errors.js';
-import { PROJECT_ID_FILE, getSessionIdFromFile } from '../../../tokens.js';
-import { type Result, getAstroStudioUrl } from '../../../utils.js';
+import { PROJECT_ID_FILE, MISSING_SESSION_ID_ERROR, getAstroStudioUrl, getSessionIdFromFile } from '@astrojs/studio';
+import { type Result } from '../../../utils.js';
export async function cmd() {
const sessionToken = await getSessionIdFromFile();
diff --git a/packages/db/src/core/cli/commands/login/index.ts b/packages/db/src/core/cli/commands/login/index.ts
index 02229d52f..e50afda04 100644
--- a/packages/db/src/core/cli/commands/login/index.ts
+++ b/packages/db/src/core/cli/commands/login/index.ts
@@ -7,9 +7,8 @@ import open from 'open';
import ora from 'ora';
import prompt from 'prompts';
import type { Arguments } from 'yargs-parser';
-import { SESSION_LOGIN_FILE } from '../../../tokens.js';
+import { SESSION_LOGIN_FILE, getAstroStudioUrl } from '@astrojs/studio';
import type { DBConfig } from '../../../types.js';
-import { getAstroStudioUrl } from '../../../utils.js';
const isWebContainer =
// Stackblitz heuristic
diff --git a/packages/db/src/core/cli/commands/logout/index.ts b/packages/db/src/core/cli/commands/logout/index.ts
index fbc4ba78c..a69d83bbc 100644
--- a/packages/db/src/core/cli/commands/logout/index.ts
+++ b/packages/db/src/core/cli/commands/logout/index.ts
@@ -1,5 +1,5 @@
import { unlink } from 'node:fs/promises';
-import { SESSION_LOGIN_FILE } from '../../../tokens.js';
+import { SESSION_LOGIN_FILE } from '@astrojs/studio'
export async function cmd() {
await unlink(SESSION_LOGIN_FILE);
diff --git a/packages/db/src/core/cli/commands/push/index.ts b/packages/db/src/core/cli/commands/push/index.ts
index 377b3677f..a5802a31f 100644
--- a/packages/db/src/core/cli/commands/push/index.ts
+++ b/packages/db/src/core/cli/commands/push/index.ts
@@ -3,7 +3,7 @@ import prompts from 'prompts';
import type { Arguments } from 'yargs-parser';
import { safeFetch } from '../../../../runtime/utils.js';
import { MIGRATION_VERSION } from '../../../consts.js';
-import { getManagedAppTokenOrExit } from '../../../tokens.js';
+import { getManagedAppTokenOrExit } from '@astrojs/studio';
import { type DBConfig, type DBSnapshot } from '../../../types.js';
import { type Result, getRemoteDatabaseUrl } from '../../../utils.js';
import {
diff --git a/packages/db/src/core/cli/commands/shell/index.ts b/packages/db/src/core/cli/commands/shell/index.ts
index c93a127fa..4e0c0d976 100644
--- a/packages/db/src/core/cli/commands/shell/index.ts
+++ b/packages/db/src/core/cli/commands/shell/index.ts
@@ -8,7 +8,7 @@ import {
import { normalizeDatabaseUrl } from '../../../../runtime/index.js';
import { DB_PATH } from '../../../consts.js';
import { SHELL_QUERY_MISSING_ERROR } from '../../../errors.js';
-import { getManagedAppTokenOrExit } from '../../../tokens.js';
+import { getManagedAppTokenOrExit } from '@astrojs/studio';
import type { DBConfigInput } from '../../../types.js';
import { getAstroEnv, getRemoteDatabaseUrl } from '../../../utils.js';
diff --git a/packages/db/src/core/cli/commands/verify/index.ts b/packages/db/src/core/cli/commands/verify/index.ts
index 8c480344d..b555f3d12 100644
--- a/packages/db/src/core/cli/commands/verify/index.ts
+++ b/packages/db/src/core/cli/commands/verify/index.ts
@@ -1,6 +1,6 @@
import type { AstroConfig } from 'astro';
import type { Arguments } from 'yargs-parser';
-import { getManagedAppTokenOrExit } from '../../../tokens.js';
+import { getManagedAppTokenOrExit } from '@astrojs/studio';
import type { DBConfig } from '../../../types.js';
import {
createCurrentSnapshot,
diff --git a/packages/db/src/core/errors.ts b/packages/db/src/core/errors.ts
index 1dfbc4e03..5c8c196db 100644
--- a/packages/db/src/core/errors.ts
+++ b/packages/db/src/core/errors.ts
@@ -1,19 +1,5 @@
import { bold, cyan, red } from 'kleur/colors';
-export const MISSING_SESSION_ID_CI_ERROR = `${red('▶ ASTRO_STUDIO_APP_TOKEN required')}
-
- To authenticate with Astro Studio add the token to your CI's environment variables.\n`;
-
-export const MISSING_SESSION_ID_ERROR = `${red('▶ Login required!')}
-
- To authenticate with Astro Studio, run
- ${cyan('astro db login')}\n`;
-
-export const MISSING_PROJECT_ID_ERROR = `${red('▶ Directory not linked.')}
-
- To link this directory to an Astro Studio project, run
- ${cyan('astro db link')}\n`;
-
export const MISSING_EXECUTE_PATH_ERROR = `${red(
'▶ No file path provided.'
)} Provide a path by running ${cyan('astro db execute <path>')}\n`;
@@ -26,7 +12,7 @@ export const RENAME_TABLE_ERROR = (oldTable: string, newTable: string) => {
1. Use "deprecated: true" to deprecate a table before renaming.
2. Use "--force-reset" to ignore this warning and reset the database (deleting all of your data).
-
+
Visit https://docs.astro.build/en/guides/astro-db/#renaming-tables to learn more.`
);
};
diff --git a/packages/db/src/core/integration/index.ts b/packages/db/src/core/integration/index.ts
index 99b69bed6..a79abb136 100644
--- a/packages/db/src/core/integration/index.ts
+++ b/packages/db/src/core/integration/index.ts
@@ -19,7 +19,7 @@ import { CONFIG_FILE_NAMES, DB_PATH } from '../consts.js';
import { EXEC_DEFAULT_EXPORT_ERROR, EXEC_ERROR } from '../errors.js';
import { resolveDbConfig } from '../load-file.js';
import { SEED_DEV_FILE_NAME } from '../queries.js';
-import { type ManagedAppToken, getManagedAppTokenOrExit } from '../tokens.js';
+import { type ManagedAppToken, getManagedAppTokenOrExit } from '@astrojs/studio';
import { type VitePlugin, getDbDirectoryUrl } from '../utils.js';
import { fileURLIntegration } from './file-url.js';
import { typegenInternal } from './typegen.js';
diff --git a/packages/db/src/core/utils.ts b/packages/db/src/core/utils.ts
index d9f1e0323..6570bada7 100644
--- a/packages/db/src/core/utils.ts
+++ b/packages/db/src/core/utils.ts
@@ -1,14 +1,10 @@
import type { AstroConfig, AstroIntegration } from 'astro';
import { loadEnv } from 'vite';
import type { AstroDbIntegration } from './types.js';
+import { getAstroStudioEnv } from '@astrojs/studio';
export type VitePlugin = Required<AstroConfig['vite']>['plugins'][number];
-export function getAstroStudioEnv(envMode = ''): Record<`ASTRO_STUDIO_${string}`, string> {
- const env = loadEnv(envMode, process.cwd(), 'ASTRO_STUDIO_');
- return env;
-}
-
export function getAstroEnv(envMode = ''): Record<`ASTRO_${string}`, string> {
const env = loadEnv(envMode, process.cwd(), 'ASTRO_');
return env;
@@ -19,11 +15,6 @@ export function getRemoteDatabaseUrl(): string {
return env.ASTRO_STUDIO_REMOTE_DB_URL || 'https://db.services.astro.build';
}
-export function getAstroStudioUrl(): string {
- const env = getAstroStudioEnv();
- return env.ASTRO_STUDIO_URL || 'https://studio.astro.build';
-}
-
export function getDbDirectoryUrl(root: URL | string) {
return new URL('db/', root);
}
diff --git a/packages/studio/README.md b/packages/studio/README.md
new file mode 100644
index 000000000..935c200a3
--- /dev/null
+++ b/packages/studio/README.md
@@ -0,0 +1,34 @@
+# @astrojs/studio
+
+This package manages the connection between a local Astro project and [Astro Studio](studio). At this time, this package is not intended for direct use by end users, but rather as a dependency of other Astro packages.
+
+## Support
+
+- Get help in the [Astro Discord][discord]. Post questions in our `#support` forum, or visit our dedicated `#dev` channel to discuss current development and more!
+
+- Check our [Astro Integration Documentation][astro-integration] for more on integrations.
+
+- Submit bug reports and feature requests as [GitHub issues][issues].
+
+## Contributing
+
+This package is maintained by Astro's Core team. You're welcome to submit an issue or PR! These links will help you get started:
+
+- [Contributor Manual][contributing]
+- [Code of Conduct][coc]
+- [Community Guide][community]
+
+## License
+
+MIT
+
+Copyright (c) 2023–present [Astro][astro]
+
+[astro]: https://astro.build/
+[contributing]: https://github.com/withastro/astro/blob/main/CONTRIBUTING.md
+[coc]: https://github.com/withastro/.github/blob/main/CODE_OF_CONDUCT.md
+[community]: https://github.com/withastro/.github/blob/main/COMMUNITY_GUIDE.md
+[discord]: https://astro.build/chat/
+[issues]: https://github.com/withastro/astro/issues
+[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
+[studio]: https://studio.astro.build/
diff --git a/packages/studio/package.json b/packages/studio/package.json
new file mode 100644
index 000000000..6f8e34ebb
--- /dev/null
+++ b/packages/studio/package.json
@@ -0,0 +1,47 @@
+{
+ "name": "@astrojs/studio",
+ "version": "0.0.1",
+ "description": "Internal package powering integrations between Astro projects and Astro Studio",
+ "license": "MIT",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/withastro/astro.git",
+ "directory": "packages/studio"
+ },
+ "bugs": "https://github.com/withastro/astro/issues",
+ "homepage": "https://docs.astro.build/en/guides/integrations-guide/studio/",
+ "type": "module",
+ "author": "withastro",
+ "types": "./dist/index.js",
+ "main": "./dist/index.js",
+ "exports": {
+ ".": {
+ "types": "./dist/index.d.ts",
+ "import": "./dist/index.js"
+ },
+ "./package.json": "./package.json"
+ },
+ "files": [
+ "dist"
+ ],
+ "keywords": [
+ "withastro",
+ "astro-integration"
+ ],
+ "scripts": {
+ "build": "astro-scripts build \"src/**/*.ts\" && tsc",
+ "build:ci": "astro-scripts build \"src/**/*.ts\"",
+ "dev": "astro-scripts dev \"src/**/*.ts\""
+ },
+ "dependencies": {
+ "ci-info": "^4.0.0",
+ "kleur": "^4.1.5",
+ "ora": "^8.0.1"
+ },
+ "devDependencies": {
+ "astro": "workspace:*",
+ "astro-scripts": "workspace:*",
+ "typescript": "^5.4.5",
+ "vite": "^5.2.11"
+ }
+}
diff --git a/packages/studio/src/core/errors.ts b/packages/studio/src/core/errors.ts
new file mode 100644
index 000000000..9ffead3ee
--- /dev/null
+++ b/packages/studio/src/core/errors.ts
@@ -0,0 +1,15 @@
+import { cyan, red } from 'kleur/colors';
+
+export const MISSING_SESSION_ID_CI_ERROR = `${red('▶ ASTRO_STUDIO_APP_TOKEN required')}
+
+ To authenticate with Astro Studio add the token to your CI's environment variables.\n`;
+
+export const MISSING_SESSION_ID_ERROR = `${red('▶ Login required!')}
+
+ To authenticate with Astro Studio, run
+ ${cyan('astro login')}\n`;
+
+export const MISSING_PROJECT_ID_ERROR = `${red('▶ Directory not linked.')}
+
+ To link this directory to an Astro Studio project, run
+ ${cyan('astro link')}\n`;
diff --git a/packages/db/src/core/tokens.ts b/packages/studio/src/core/tokens.ts
index a49e81e36..cf343fb8f 100644
--- a/packages/db/src/core/tokens.ts
+++ b/packages/studio/src/core/tokens.ts
@@ -5,7 +5,6 @@ import { pathToFileURL } from 'node:url';
import ci from 'ci-info';
import { green } from 'kleur/colors';
import ora from 'ora';
-import { safeFetch } from '../runtime/utils.js';
import {
MISSING_PROJECT_ID_ERROR,
MISSING_SESSION_ID_CI_ERROR,
@@ -209,3 +208,22 @@ function getExpiresFromTtl(ttl: number): Date {
// ttl is in minutes
return new Date(Date.now() + ttl * 60 * 1000);
}
+
+/**
+ * Small wrapper around fetch that throws an error if the response is not OK. Allows for custom error handling as well through the onNotOK callback.
+ */
+async function safeFetch(
+ url: Parameters<typeof fetch>[0],
+ options: Parameters<typeof fetch>[1] = {},
+ onNotOK: (response: Response) => void | Promise<void> = () => {
+ throw new Error(`Request to ${url} returned a non-OK status code.`);
+ }
+): Promise<Response> {
+ const response = await fetch(url, options);
+
+ if (!response.ok) {
+ await onNotOK(response);
+ }
+
+ return response;
+}
diff --git a/packages/studio/src/core/utils.ts b/packages/studio/src/core/utils.ts
new file mode 100644
index 000000000..7cf40f751
--- /dev/null
+++ b/packages/studio/src/core/utils.ts
@@ -0,0 +1,11 @@
+import { loadEnv } from 'vite';
+
+export function getAstroStudioEnv(envMode = ''): Record<`ASTRO_STUDIO_${string}`, string> {
+ const env = loadEnv(envMode, process.cwd(), 'ASTRO_STUDIO_');
+ return env;
+}
+
+export function getAstroStudioUrl(): string {
+ const env = getAstroStudioEnv();
+ return env.ASTRO_STUDIO_URL || 'https://studio.astro.build';
+}
diff --git a/packages/studio/src/index.ts b/packages/studio/src/index.ts
new file mode 100644
index 000000000..e97d53dfc
--- /dev/null
+++ b/packages/studio/src/index.ts
@@ -0,0 +1,3 @@
+export * from './core/tokens.js';
+export * from './core/utils.js';
+export * from './core/errors.js';
diff --git a/packages/studio/tsconfig.json b/packages/studio/tsconfig.json
new file mode 100644
index 000000000..18443cddf
--- /dev/null
+++ b/packages/studio/tsconfig.json
@@ -0,0 +1,7 @@
+{
+ "extends": "../../tsconfig.base.json",
+ "include": ["src"],
+ "compilerOptions": {
+ "outDir": "./dist"
+ }
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 8ddf64b74..98e1b9d29 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -3943,6 +3943,9 @@ importers:
packages/db:
dependencies:
+ '@astrojs/studio':
+ specifier: workspace:*
+ version: link:../studio
'@libsql/client':
specifier: ^0.6.0
version: 0.6.0
@@ -5533,6 +5536,31 @@ importers:
specifier: ^2.0.0
version: 2.0.0
+ packages/studio:
+ dependencies:
+ ci-info:
+ specifier: ^4.0.0
+ version: 4.0.0
+ kleur:
+ specifier: ^4.1.5
+ version: 4.1.5
+ ora:
+ specifier: ^8.0.1
+ version: 8.0.1
+ devDependencies:
+ astro:
+ specifier: workspace:*
+ version: link:../astro
+ astro-scripts:
+ specifier: workspace:*
+ version: link:../../scripts
+ typescript:
+ specifier: ^5.4.5
+ version: 5.4.5
+ vite:
+ specifier: ^5.2.11
+ version: 5.2.11(@types/node@18.19.31)(sass@1.77.1)
+
packages/telemetry:
dependencies:
ci-info: