From ef2f2e6339407a243097c6b0fa1521635391b283 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 14 May 2024 13:15:55 +0200 Subject: [PATCH 1/7] feat: add an internal for now studio package --- packages/db/package.json | 3 +- .../db/src/core/cli/commands/execute/index.ts | 2 +- .../db/src/core/cli/commands/link/index.ts | 2 +- .../db/src/core/cli/commands/login/index.ts | 2 +- .../db/src/core/cli/commands/logout/index.ts | 2 +- .../db/src/core/cli/commands/push/index.ts | 2 +- .../db/src/core/cli/commands/shell/index.ts | 2 +- .../db/src/core/cli/commands/verify/index.ts | 2 +- packages/db/src/core/errors.ts | 11 +---- packages/db/src/core/integration/index.ts | 2 +- packages/studio/README.md | 34 +++++++++++++ packages/studio/package.json | 48 +++++++++++++++++++ packages/studio/src/core/errors.ts | 15 ++++++ packages/{db => studio}/src/core/tokens.ts | 3 +- packages/studio/src/core/utils.ts | 30 ++++++++++++ packages/studio/src/index.ts | 2 + packages/studio/tsconfig.json | 7 +++ pnpm-lock.yaml | 28 +++++++++++ 18 files changed, 176 insertions(+), 21 deletions(-) create mode 100644 packages/studio/README.md create mode 100644 packages/studio/package.json create mode 100644 packages/studio/src/core/errors.ts rename packages/{db => studio}/src/core/tokens.ts (97%) create mode 100644 packages/studio/src/core/utils.ts create mode 100644 packages/studio/src/index.ts create mode 100644 packages/studio/tsconfig.json diff --git a/packages/db/package.json b/packages/db/package.json index ec55fd457400..ab6b3b0eed72 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -83,7 +83,8 @@ "prompts": "^2.4.2", "strip-ansi": "^7.1.0", "yargs-parser": "^21.1.1", - "zod": "^3.23.8" + "zod": "^3.23.8", + "@astrojs/studio": "^0.1.0" }, "devDependencies": { "@types/chai": "^4.3.16", diff --git a/packages/db/src/core/cli/commands/execute/index.ts b/packages/db/src/core/cli/commands/execute/index.ts index b596d4c0d3b6..e9421d97d98a 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 9ac201e76415..faa45f9eae6b 100644 --- a/packages/db/src/core/cli/commands/link/index.ts +++ b/packages/db/src/core/cli/commands/link/index.ts @@ -7,7 +7,7 @@ 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 { PROJECT_ID_FILE, getSessionIdFromFile } from '@astrojs/studio'; import { type Result, getAstroStudioUrl } from '../../../utils.js'; export async function cmd() { diff --git a/packages/db/src/core/cli/commands/login/index.ts b/packages/db/src/core/cli/commands/login/index.ts index 02229d52f28d..1d2640c93726 100644 --- a/packages/db/src/core/cli/commands/login/index.ts +++ b/packages/db/src/core/cli/commands/login/index.ts @@ -7,7 +7,7 @@ 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 } from '@astrojs/studio'; import type { DBConfig } from '../../../types.js'; import { getAstroStudioUrl } from '../../../utils.js'; diff --git a/packages/db/src/core/cli/commands/logout/index.ts b/packages/db/src/core/cli/commands/logout/index.ts index fbc4ba78cd83..a69d83bbc121 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 377b3677f409..a5802a31f703 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 c93a127fa99c..4e0c0d976513 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 8c480344da86..b555f3d1229b 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 1dfbc4e03dae..4ed23715fc22 100644 --- a/packages/db/src/core/errors.ts +++ b/packages/db/src/core/errors.ts @@ -1,19 +1,10 @@ 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 ')}\n`; @@ -26,7 +17,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 99b69bed6551..a79abb136e0d 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/studio/README.md b/packages/studio/README.md new file mode 100644 index 000000000000..935c200a3ada --- /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 000000000000..5cef9cd8c144 --- /dev/null +++ b/packages/studio/package.json @@ -0,0 +1,48 @@ +{ + "name": "@astrojs/studio", + "version": "0.1.0", + "description": "Add libSQL and Astro Studio support to your Astro site", + "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": "./index.d.ts", + "main": "./dist/index.js", + "exports": { + ".": { + "types": "./index.d.ts", + "import": "./dist/index.js" + } + }, + "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\"", + "test": "mocha --exit --timeout 20000 \"test/*.js\" \"test/unit/**/*.js\"", + "test:match": "mocha --timeout 20000 \"test/*.js\" \"test/unit/*.js\" -g" + }, + "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 000000000000..92a3da6ef51f --- /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 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`; diff --git a/packages/db/src/core/tokens.ts b/packages/studio/src/core/tokens.ts similarity index 97% rename from packages/db/src/core/tokens.ts rename to packages/studio/src/core/tokens.ts index a49e81e36737..a3e4bf17a6a5 100644 --- a/packages/db/src/core/tokens.ts +++ b/packages/studio/src/core/tokens.ts @@ -5,13 +5,12 @@ 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, MISSING_SESSION_ID_ERROR, } from './errors.js'; -import { getAstroStudioEnv, getAstroStudioUrl } from './utils.js'; +import { getAstroStudioEnv, getAstroStudioUrl, safeFetch } from './utils.js'; export const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), '.astro', 'session-token')); export const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), '.astro', 'link')); diff --git a/packages/studio/src/core/utils.ts b/packages/studio/src/core/utils.ts new file mode 100644 index 000000000000..0ffc875195b5 --- /dev/null +++ b/packages/studio/src/core/utils.ts @@ -0,0 +1,30 @@ +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'; +} + +/** + * 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. + */ +export async function safeFetch( + url: Parameters[0], + options: Parameters[1] = {}, + onNotOK: (response: Response) => void | Promise = () => { + throw new Error(`Request to ${url} returned a non-OK status code.`); + } +): Promise { + const response = await fetch(url, options); + + if (!response.ok) { + await onNotOK(response); + } + + return response; +} diff --git a/packages/studio/src/index.ts b/packages/studio/src/index.ts new file mode 100644 index 000000000000..bd5f84563a12 --- /dev/null +++ b/packages/studio/src/index.ts @@ -0,0 +1,2 @@ +export * from './core/tokens.js'; +export * from './core/utils.js'; diff --git a/packages/studio/tsconfig.json b/packages/studio/tsconfig.json new file mode 100644 index 000000000000..18443cddf207 --- /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 1dbe15339718..4e5b7092110f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3943,6 +3943,9 @@ importers: packages/db: dependencies: + '@astrojs/studio': + specifier: ^0.1.0 + 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: From f040251f8e9dd9dd5920f019389a407ea11e0e68 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 14 May 2024 14:12:20 +0200 Subject: [PATCH 2/7] fix: remove test commands --- packages/studio/package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/studio/package.json b/packages/studio/package.json index 5cef9cd8c144..c3f8c337a815 100644 --- a/packages/studio/package.json +++ b/packages/studio/package.json @@ -30,9 +30,7 @@ "scripts": { "build": "astro-scripts build \"src/**/*.ts\" && tsc", "build:ci": "astro-scripts build \"src/**/*.ts\"", - "dev": "astro-scripts dev \"src/**/*.ts\"", - "test": "mocha --exit --timeout 20000 \"test/*.js\" \"test/unit/**/*.js\"", - "test:match": "mocha --timeout 20000 \"test/*.js\" \"test/unit/*.js\" -g" + "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { "ci-info": "^4.0.0", From 572a35e5333cd66ef88b00dab9e603f6e026b192 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Tue, 14 May 2024 18:59:31 +0200 Subject: [PATCH 3/7] fix: use getenvs from studio package --- packages/db/src/core/cli/commands/link/index.ts | 4 ++-- packages/db/src/core/cli/commands/login/index.ts | 3 +-- packages/db/src/core/utils.ts | 11 +---------- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/packages/db/src/core/cli/commands/link/index.ts b/packages/db/src/core/cli/commands/link/index.ts index faa45f9eae6b..f6de264dcaa5 100644 --- a/packages/db/src/core/cli/commands/link/index.ts +++ b/packages/db/src/core/cli/commands/link/index.ts @@ -7,8 +7,8 @@ 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 '@astrojs/studio'; -import { type Result, getAstroStudioUrl } from '../../../utils.js'; +import { PROJECT_ID_FILE, 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 1d2640c93726..e50afda04524 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 '@astrojs/studio'; +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/utils.ts b/packages/db/src/core/utils.ts index d9f1e032386e..6570bada74df 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['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); } From 25089faf098afd4410d5c22f88b721792dab5f8c Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 16 May 2024 17:27:38 +0200 Subject: [PATCH 4/7] fix: packagejson --- packages/studio/package.json | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/studio/package.json b/packages/studio/package.json index c3f8c337a815..303849216c0e 100644 --- a/packages/studio/package.json +++ b/packages/studio/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/studio", "version": "0.1.0", - "description": "Add libSQL and Astro Studio support to your Astro site", + "description": "Internal package powering integrations between Astro projects and Astro Studio", "license": "MIT", "repository": { "type": "git", @@ -12,13 +12,14 @@ "homepage": "https://docs.astro.build/en/guides/integrations-guide/studio/", "type": "module", "author": "withastro", - "types": "./index.d.ts", + "types": "./dist/index.js", "main": "./dist/index.js", "exports": { ".": { - "types": "./index.d.ts", + "types": "./dist/index.d.ts", "import": "./dist/index.js" - } + }, + "./package.json": "./package.json" }, "files": [ "dist" From 67ed6978fd8740f34e271b3e7559fed8101d4263 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 16 May 2024 17:41:45 +0200 Subject: [PATCH 5/7] chore: changeset --- .changeset/silent-socks-return.md | 5 +++++ .changeset/young-plums-doubt.md | 5 +++++ packages/studio/package.json | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/silent-socks-return.md create mode 100644 .changeset/young-plums-doubt.md diff --git a/.changeset/silent-socks-return.md b/.changeset/silent-socks-return.md new file mode 100644 index 000000000000..d0e7b9b16361 --- /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 000000000000..64be9968c45a --- /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/studio/package.json b/packages/studio/package.json index 303849216c0e..1a9b44e5c424 100644 --- a/packages/studio/package.json +++ b/packages/studio/package.json @@ -1,6 +1,6 @@ { "name": "@astrojs/studio", - "version": "0.1.0", + "version": "0.0.0", "description": "Internal package powering integrations between Astro projects and Astro Studio", "license": "MIT", "repository": { From 19768000cb157ae0f7ad95ff9d71ed31b946f6b8 Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 16 May 2024 17:48:04 +0200 Subject: [PATCH 6/7] fix: remove unused error --- .../db/src/core/cli/commands/link/index.ts | 3 +-- packages/db/src/core/errors.ts | 5 ----- packages/studio/src/core/errors.ts | 4 ++-- packages/studio/src/core/tokens.ts | 21 ++++++++++++++++++- packages/studio/src/core/utils.ts | 19 ----------------- packages/studio/src/index.ts | 1 + 6 files changed, 24 insertions(+), 29 deletions(-) diff --git a/packages/db/src/core/cli/commands/link/index.ts b/packages/db/src/core/cli/commands/link/index.ts index f6de264dcaa5..e0498f9c4b51 100644 --- a/packages/db/src/core/cli/commands/link/index.ts +++ b/packages/db/src/core/cli/commands/link/index.ts @@ -6,8 +6,7 @@ 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, getAstroStudioUrl, getSessionIdFromFile } from '@astrojs/studio'; +import { PROJECT_ID_FILE, MISSING_SESSION_ID_ERROR, getAstroStudioUrl, getSessionIdFromFile } from '@astrojs/studio'; import { type Result } from '../../../utils.js'; export async function cmd() { diff --git a/packages/db/src/core/errors.ts b/packages/db/src/core/errors.ts index 4ed23715fc22..5c8c196db9da 100644 --- a/packages/db/src/core/errors.ts +++ b/packages/db/src/core/errors.ts @@ -1,10 +1,5 @@ import { bold, cyan, red } from 'kleur/colors'; -export const MISSING_SESSION_ID_ERROR = `${red('▶ Login required!')} - - To authenticate with Astro Studio, run - ${cyan('astro db login')}\n`; - export const MISSING_EXECUTE_PATH_ERROR = `${red( '▶ No file path provided.' )} Provide a path by running ${cyan('astro db execute ')}\n`; diff --git a/packages/studio/src/core/errors.ts b/packages/studio/src/core/errors.ts index 92a3da6ef51f..9ffead3eeb4d 100644 --- a/packages/studio/src/core/errors.ts +++ b/packages/studio/src/core/errors.ts @@ -7,9 +7,9 @@ export const MISSING_SESSION_ID_CI_ERROR = `${red('▶ ASTRO_STUDIO_APP_TOKEN re export const MISSING_SESSION_ID_ERROR = `${red('▶ Login required!')} To authenticate with Astro Studio, run - ${cyan('astro db login')}\n`; + ${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 db link')}\n`; + ${cyan('astro link')}\n`; diff --git a/packages/studio/src/core/tokens.ts b/packages/studio/src/core/tokens.ts index a3e4bf17a6a5..cf343fb8f62c 100644 --- a/packages/studio/src/core/tokens.ts +++ b/packages/studio/src/core/tokens.ts @@ -10,7 +10,7 @@ import { MISSING_SESSION_ID_CI_ERROR, MISSING_SESSION_ID_ERROR, } from './errors.js'; -import { getAstroStudioEnv, getAstroStudioUrl, safeFetch } from './utils.js'; +import { getAstroStudioEnv, getAstroStudioUrl } from './utils.js'; export const SESSION_LOGIN_FILE = pathToFileURL(join(homedir(), '.astro', 'session-token')); export const PROJECT_ID_FILE = pathToFileURL(join(process.cwd(), '.astro', 'link')); @@ -208,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[0], + options: Parameters[1] = {}, + onNotOK: (response: Response) => void | Promise = () => { + throw new Error(`Request to ${url} returned a non-OK status code.`); + } +): Promise { + 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 index 0ffc875195b5..7cf40f75111e 100644 --- a/packages/studio/src/core/utils.ts +++ b/packages/studio/src/core/utils.ts @@ -9,22 +9,3 @@ export function getAstroStudioUrl(): string { const env = getAstroStudioEnv(); return env.ASTRO_STUDIO_URL || 'https://studio.astro.build'; } - -/** - * 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. - */ -export async function safeFetch( - url: Parameters[0], - options: Parameters[1] = {}, - onNotOK: (response: Response) => void | Promise = () => { - throw new Error(`Request to ${url} returned a non-OK status code.`); - } -): Promise { - const response = await fetch(url, options); - - if (!response.ok) { - await onNotOK(response); - } - - return response; -} diff --git a/packages/studio/src/index.ts b/packages/studio/src/index.ts index bd5f84563a12..e97d53dfc12b 100644 --- a/packages/studio/src/index.ts +++ b/packages/studio/src/index.ts @@ -1,2 +1,3 @@ export * from './core/tokens.js'; export * from './core/utils.js'; +export * from './core/errors.js'; From 0cf5276bb19e43305d8b70414b2095d7e12eb31e Mon Sep 17 00:00:00 2001 From: Princesseuh <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 16 May 2024 18:02:59 +0200 Subject: [PATCH 7/7] fix: of course Bjorn was right all along --- packages/db/package.json | 4 ++-- packages/studio/package.json | 2 +- pnpm-lock.yaml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/db/package.json b/packages/db/package.json index f68e7fc1ddef..9124166583ea 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", @@ -83,8 +84,7 @@ "prompts": "^2.4.2", "strip-ansi": "^7.1.0", "yargs-parser": "^21.1.1", - "zod": "^3.23.8", - "@astrojs/studio": "^0.1.0" + "zod": "^3.23.8" }, "devDependencies": { "@types/chai": "^4.3.16", diff --git a/packages/studio/package.json b/packages/studio/package.json index 1a9b44e5c424..6f8e34ebb064 100644 --- a/packages/studio/package.json +++ b/packages/studio/package.json @@ -1,6 +1,6 @@ { "name": "@astrojs/studio", - "version": "0.0.0", + "version": "0.0.1", "description": "Internal package powering integrations between Astro projects and Astro Studio", "license": "MIT", "repository": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 02deaf39250a..98e1b9d2910c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3944,7 +3944,7 @@ importers: packages/db: dependencies: '@astrojs/studio': - specifier: ^0.1.0 + specifier: workspace:* version: link:../studio '@libsql/client': specifier: ^0.6.0