0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

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
This commit is contained in:
Erika 2024-05-16 18:10:08 +02:00 committed by GitHub
parent 3830e5d416
commit 9332bb1c1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 186 additions and 37 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---
Internal refactor, this change should have no visible effect

View file

@ -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

View file

@ -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",

View file

@ -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({

View file

@ -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();

View file

@ -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

View file

@ -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);

View file

@ -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 {

View file

@ -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';

View file

@ -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,

View file

@ -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.`
);
};

View file

@ -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';

View file

@ -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);
}

34
packages/studio/README.md Normal file
View file

@ -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) 2023present [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/

View file

@ -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"
}
}

View file

@ -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`;

View file

@ -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;
}

View file

@ -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';
}

View file

@ -0,0 +1,3 @@
export * from './core/tokens.js';
export * from './core/utils.js';
export * from './core/errors.js';

View file

@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src"],
"compilerOptions": {
"outDir": "./dist"
}
}

View file

@ -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: