Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an internal for now studio package #11037

Merged
merged 9 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-socks-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Internal refactor, this change should have no visible effect
5 changes: 5 additions & 0 deletions .changeset/young-plums-doubt.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/cli/commands/execute/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 2 additions & 3 deletions packages/db/src/core/cli/commands/link/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions packages/db/src/core/cli/commands/login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/cli/commands/logout/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/cli/commands/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/cli/commands/shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/cli/commands/verify/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
16 changes: 1 addition & 15 deletions packages/db/src/core/errors.ts
Original file line number Diff line number Diff line change
@@ -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`;
Expand All @@ -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.`
);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
11 changes: 1 addition & 10 deletions packages/db/src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand Down
34 changes: 34 additions & 0 deletions packages/studio/README.md
Original file line number Diff line number Diff line change
@@ -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/
47 changes: 47 additions & 0 deletions packages/studio/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
15 changes: 15 additions & 0 deletions packages/studio/src/core/errors.ts
Original file line number Diff line number Diff line change
@@ -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`;
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
11 changes: 11 additions & 0 deletions packages/studio/src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
}
3 changes: 3 additions & 0 deletions packages/studio/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './core/tokens.js';
export * from './core/utils.js';
export * from './core/errors.js';
7 changes: 7 additions & 0 deletions packages/studio/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src"],
"compilerOptions": {
"outDir": "./dist"
}
}
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading