From 9332bb1c1f237f5666ded09532ccd651837b94e5 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Thu, 16 May 2024 18:10:08 +0200 Subject: [PATCH] feat: add an internal for now studio package (#11037) * feat: add an internal for now studio package * fix: remove test commands * fix: use getenvs from studio package * fix: packagejson * chore: changeset * fix: remove unused error * fix: of course Bjorn was right all along --- .changeset/silent-socks-return.md | 5 ++ .changeset/young-plums-doubt.md | 5 ++ packages/db/package.json | 1 + .../db/src/core/cli/commands/execute/index.ts | 2 +- .../db/src/core/cli/commands/link/index.ts | 5 +- .../db/src/core/cli/commands/login/index.ts | 3 +- .../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 | 16 +------ packages/db/src/core/integration/index.ts | 2 +- packages/db/src/core/utils.ts | 11 +---- packages/studio/README.md | 34 ++++++++++++++ packages/studio/package.json | 47 +++++++++++++++++++ packages/studio/src/core/errors.ts | 15 ++++++ packages/{db => studio}/src/core/tokens.ts | 20 +++++++- packages/studio/src/core/utils.ts | 11 +++++ packages/studio/src/index.ts | 3 ++ packages/studio/tsconfig.json | 7 +++ pnpm-lock.yaml | 28 +++++++++++ 21 files changed, 186 insertions(+), 37 deletions(-) create mode 100644 .changeset/silent-socks-return.md create mode 100644 .changeset/young-plums-doubt.md 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 (91%) 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/.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/db/package.json b/packages/db/package.json index 81fcd76b2720..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", 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..e0498f9c4b51 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 02229d52f28d..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 '../../../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 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..5c8c196db9da 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 ')}\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 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/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); } 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..6f8e34ebb064 --- /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 000000000000..9ffead3eeb4d --- /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 similarity index 91% rename from packages/db/src/core/tokens.ts rename to packages/studio/src/core/tokens.ts index a49e81e36737..cf343fb8f62c 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[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 new file mode 100644 index 000000000000..7cf40f75111e --- /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 000000000000..e97d53dfc12b --- /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 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 8ddf64b741b1..98e1b9d2910c 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: