diff --git a/deploy/apps.ts b/deploy/apps.ts index ce2e109..43f2eac 100644 --- a/deploy/apps.ts +++ b/deploy/apps.ts @@ -17,12 +17,17 @@ interface AppItem { layers: Array<{ slug: string }>; } -interface AppDetail { +/** The app row `apps.get` returns, as far as the CLI reads it. */ +export interface AppDetail { id: string; slug: string; created_at: Date; updated_at: Date; - build_config: { frameworkPreset?: string | null } | null; + build_config: { + frameworkPreset?: string | null; + /** The app directory, relative to the repository or deploy root. */ + buildDirectory?: string; + } | null; } interface TimelineEntry { diff --git a/deploy/create/flow.ts b/deploy/create/flow.ts index 612dbd1..759f80e 100644 --- a/deploy/create/flow.ts +++ b/deploy/create/flow.ts @@ -1,4 +1,5 @@ import { createTrpcClient, type TRPCClient } from "../../auth.ts"; +import { resolve } from "@std/path"; import { green } from "@std/fmt/colors"; import { error } from "../../util.ts"; import { @@ -7,7 +8,9 @@ import { } from "@std/cli/unstable-prompt-select"; import { type BuildConfig, + detectBuildConfig, type DetectedBuildConfig, + detectPackageManager, detectWorkspace, FrameworkFileSystemReader, SUPPORTED_FRAMEWORK_PRESETS, @@ -44,6 +47,33 @@ export function parseBuildTimeoutFlag(value: string): number | null { return AVAILABLE_BUILD_TIMEOUTS.includes(minutes) ? minutes : null; } +/** + * The build config detected in a local app directory that is not a detected + * workspace member: a deploy still applies its deno.json `deploy` section, so + * `create` must know about it. Not attempted for a GitHub repo, where it would + * need a request whose failure exits the CLI, for what is only a warning. + */ +export async function customDirectoryBuildConfig( + rootPath: string, + repo: Repo, + path: string, +): Promise { + if (repo !== undefined) return null; + try { + // As `detectWorkspace` does: a lockfile at the root decides the package + // manager of a nested app. + const packageManager = await detectPackageManager( + new FrameworkFileSystemReader(rootPath), + ); + return await detectBuildConfig( + new FrameworkFileSystemReader(resolve(rootPath, path)), + packageManager, + ); + } catch { + return null; + } +} + /** * The build timeout a detected build config asks for (deno.json * `deploy.buildTimeout`), as the largest available step not above it or @@ -85,6 +115,16 @@ export async function detectedBuildTimeoutForOrg( return detectedBuildTimeout(buildConfig, maxBuildTimeout); } +/** + * The `deploy` section of deno.json takes precedence over the app's stored + * build configuration on every deploy, so settings given to `create` that + * differ from it would never be used. + */ +export const DENO_JSON_PRECEDENCE_WARNING = + "This app's deno.json has a `deploy` section, which takes precedence over " + + "the build configuration given here on every deploy. To change the build " + + "configuration, edit deno.json instead."; + const NA = "(n/a)"; const TITLES = { organization: "organization", @@ -221,7 +261,12 @@ export async function createFlow( if (typeof selectedAppDirectory === "string") { buildConfig = appDirectories.members.find((member) => member.path === selectedAppDirectory - )?.buildConfig ?? null; + )?.buildConfig ?? + await customDirectoryBuildConfig( + rootPath, + repo, + selectedAppDirectory, + ); } else { buildConfig = selectedAppDirectory.buildConfig; } @@ -244,6 +289,9 @@ export async function createFlow( clearPreviousLines(renderedBuildConfigLines + 1); if (!useDetected) { + if (buildConfig.from === "deno.json") { + console.warn(DENO_JSON_PRECEDENCE_WARNING); + } finalBuildConfig = getBuildConfig(context, buildConfig); } else { finalBuildConfig = buildConfig; @@ -268,11 +316,25 @@ export async function createFlow( finalBuildConfig = getBuildConfig(context, buildConfig); } + if ( + explicitBuildTimeout !== undefined && finalBuildConfig === buildConfig && + buildConfig?.from === "deno.json" + ) { + console.warn(DENO_JSON_PRECEDENCE_WARNING); + } let buildTimeout = explicitBuildTimeout ?? await detectedBuildTimeoutForOrg( trpcClient, org, finalBuildConfig, ); + // A deno.json `deploy` section decides the timeout on every deploy, an + // omitted one meaning the default, so there is nothing to ask for. + if ( + buildTimeout === undefined && buildConfig?.from === "deno.json" && + finalBuildConfig === buildConfig + ) { + buildTimeout = AVAILABLE_BUILD_TIMEOUTS[0]; + } if (buildTimeout === undefined) { // TODO: check pro const selectedBuildTimeout = promptSelect( diff --git a/deploy/create/mod.ts b/deploy/create/mod.ts index 54e8216..4b61c57 100644 --- a/deploy/create/mod.ts +++ b/deploy/create/mod.ts @@ -5,6 +5,8 @@ import { AVAILABLE_BUILD_MEMORY_LIMITS, AVAILABLE_BUILD_TIMEOUTS, createFlow, + customDirectoryBuildConfig, + DENO_JSON_PRECEDENCE_WARNING, detectedBuildTimeoutForOrg, parseBuildTimeoutFlag, REGIONS, @@ -250,6 +252,25 @@ export const createCommand = new Command() buildDirectory = options.appDirectory || ""; } + // Build settings given here are overridden by a deno.json `deploy` + // section: those passed as flags, or all of them for a custom app + // directory, whose config is synthesized from the flags below. + if ( + !options.json && + (options.doNotUseDetectedBuildConfig || + options.buildTimeout !== undefined || member === undefined) + ) { + const detected = member?.buildConfig ?? + await customDirectoryBuildConfig( + rootPath, + repo, + buildDirectory, + ); + if (detected?.from === "deno.json") { + console.warn(DENO_JSON_PRECEDENCE_WARNING); + } + } + let buildConfig; if (!options.doNotUseDetectedBuildConfig) { if (member?.buildConfig) { diff --git a/deploy/publish.ts b/deploy/publish.ts index 0bb66d6..a791c57 100644 --- a/deploy/publish.ts +++ b/deploy/publish.ts @@ -1,7 +1,7 @@ import { TarStream, type TarStreamFile } from "@std/tar"; import { ProgressBar } from "@std/cli/unstable-progress-bar"; import { Spinner } from "@std/cli/unstable-spinner"; -import { join, relative, resolve, SEPARATOR } from "@std/path"; +import { isAbsolute, join, relative, resolve, SEPARATOR } from "@std/path"; import { green, red, yellow } from "@std/fmt/colors"; import { authedFetch, createTrpcClient } from "../auth.ts"; import { @@ -12,6 +12,7 @@ import { } from "../util.ts"; import type { GlobalContext } from "../main.ts"; import type { ConfigContext } from "../config.ts"; +import type { AppDetail } from "./apps.ts"; interface Revision { labels: Record; @@ -26,6 +27,93 @@ type Chunk = { data: Uint8Array; }; +const DENO_JSON_NAMES = new Set(["deno.json", "deno.jsonc"]); + +/** + * Normalizes an app's stored build directory as the console does: `.` and + * empty segments are dropped, and a path leaving the deploy root (which the + * console rejects) becomes the root, "". + */ +export function normalizeBuildDirectory(buildDirectory: string): string { + const segments = buildDirectory.split(/[\\/]/).filter((p) => + p !== "" && p !== "." + ); + return segments.includes("..") ? "" : segments.join("/"); +} + +/** + * The deno.json files to send with a CLI revision. The console reads the + * `deploy` section of the deno.json in the app's build directory, as it does + * for GitHub deployments, so it applies on every deploy rather than only when + * the app is created; it reads no other config, so only that directory's are + * sent. A config selected with `--config` stands in for it, and one the upload + * leaves out through `deploy.include`/`exclude` (e.g. `"include": ["dist/**"]`) + * still applies, so it is read from disk. + */ +export async function collectDenoJsonFiles( + { rootPath, buildDirectory, uploaded, configPath }: { + rootPath: string; + /** The app's stored build directory, as stored. */ + buildDirectory: string; + /** The uploaded deno.json/deno.jsonc files, by `/`-separated path. */ + uploaded: Record; + configPath?: string; + }, +): Promise> { + const appDir = normalizeBuildDirectory(buildDirectory); + const prefix = appDir === "" ? "" : `${appDir}/`; + const files: Record = {}; + for (const name of DENO_JSON_NAMES) { + if (`${prefix}${name}` in uploaded) { + files[`${prefix}${name}`] = uploaded[`${prefix}${name}`]; + } + } + if (configPath) { + delete files[`${prefix}deno.jsonc`]; + files[`${prefix}deno.json`] = await Deno.readTextFile(configPath); + return files; + } + // Symlinks are resolved first, so that neither the app directory nor the + // config can point outside the deploy root. + const root = await Deno.realPath(rootPath); + for (const name of DENO_JSON_NAMES) { + if (`${prefix}${name}` in files) continue; + let path; + try { + path = await Deno.realPath(join(rootPath, appDir, name)); + } catch (err) { + if (err instanceof Deno.errors.NotFound) continue; + throw err; + } + const fromRoot = relative(root, path); + if ( + fromRoot === ".." || fromRoot.startsWith(`..${SEPARATOR}`) || + isAbsolute(fromRoot) + ) { + continue; + } + files[`${prefix}${name}`] = await Deno.readTextFile(path); + } + return files; +} + +/** + * The app's stored build directory, as stored (see `normalizeBuildDirectory`). + * A failure here (e.g. a missing app) is reported and exits just as + * `apps.initiateCliRevision` would have. + */ +async function appBuildDirectory( + context: GlobalContext, + org: string, + app: string, +): Promise { + const fullApp = await createTrpcClient(context).query("apps.get", { + org, + app, + }) as AppDetail; + return fullApp.build_config?.buildDirectory ?? ""; +} + export async function publish( context: GlobalContext, configContext: ConfigContext, @@ -83,13 +171,25 @@ export async function publish( const [counter, body] = stream.tee(); const manifest: Record = {}; + const uploadedDenoJsonFiles: Record = {}; spinner.message = "Generating hashes..."; - for await (const { hash, relativePath } of counter) { - manifest[relativePath.replaceAll(SEPARATOR, "/")] = hash; + for await (const { hash, relativePath, data } of counter) { + const path = relativePath.replaceAll(SEPARATOR, "/"); + manifest[path] = hash; + if (DENO_JSON_NAMES.has(path.slice(path.lastIndexOf("/") + 1))) { + uploadedDenoJsonFiles[path] = new TextDecoder().decode(data); + } } + const denoJsonFiles = await collectDenoJsonFiles({ + rootPath, + buildDirectory: await appBuildDirectory(context, org, app), + uploaded: uploadedDenoJsonFiles, + configPath: context.config, + }); + if (context.debug) { console.error("Manifest", manifest); } @@ -103,6 +203,7 @@ export async function publish( app, production: prod, manifest, + denoJsonFiles, }, ) as string; diff --git a/tests/deno_json_files.test.ts b/tests/deno_json_files.test.ts new file mode 100644 index 0000000..5f31955 --- /dev/null +++ b/tests/deno_json_files.test.ts @@ -0,0 +1,145 @@ +import { assertEquals } from "@std/assert"; +import { dirname, join } from "@std/path"; +import { + collectDenoJsonFiles, + normalizeBuildDirectory, +} from "../deploy/publish.ts"; + +Deno.test("normalizeBuildDirectory: matches the console's normalization", () => { + assertEquals(normalizeBuildDirectory(""), ""); + assertEquals(normalizeBuildDirectory("."), ""); + assertEquals(normalizeBuildDirectory("apps/web"), "apps/web"); + assertEquals(normalizeBuildDirectory("./apps/web/"), "apps/web"); + assertEquals(normalizeBuildDirectory("apps\\web"), "apps/web"); + assertEquals(normalizeBuildDirectory("..app"), "..app"); + assertEquals(normalizeBuildDirectory("../private"), ""); + assertEquals(normalizeBuildDirectory("apps/../.."), ""); + assertEquals(normalizeBuildDirectory("..\\private"), ""); +}); + +async function withProject( + files: Record, + fn: (root: string) => Promise, +) { + const root = await Deno.makeTempDir(); + try { + for (const [path, content] of Object.entries(files)) { + await Deno.mkdir(join(root, dirname(path)), { recursive: true }); + await Deno.writeTextFile(join(root, path), content); + } + await fn(root); + } finally { + await Deno.remove(root, { recursive: true }); + } +} + +Deno.test("collectDenoJsonFiles: sends only the app directory's config", async () => { + await withProject({ + "deno.json": "root", + "apps/web/deno.json": "web", + "apps/api/deno.json": "api", + }, async (rootPath) => { + const uploaded = { + "deno.json": "root", + "apps/web/deno.json": "web", + "apps/api/deno.json": "api", + }; + assertEquals( + await collectDenoJsonFiles({ + rootPath, + buildDirectory: "apps/web", + uploaded, + }), + { "apps/web/deno.json": "web" }, + ); + assertEquals( + await collectDenoJsonFiles({ + rootPath, + buildDirectory: "./apps/web/", + uploaded, + }), + { "apps/web/deno.json": "web" }, + ); + assertEquals( + await collectDenoJsonFiles({ rootPath, buildDirectory: "", uploaded }), + { "deno.json": "root" }, + ); + }); +}); + +Deno.test("collectDenoJsonFiles: reads a config the upload excluded", async () => { + await withProject({ + "apps/web/deno.jsonc": "web", + }, async (rootPath) => { + assertEquals( + await collectDenoJsonFiles({ + rootPath, + buildDirectory: "apps/web", + uploaded: {}, + }), + { "apps/web/deno.jsonc": "web" }, + ); + }); +}); + +Deno.test("collectDenoJsonFiles: --config stands in for the app directory's", async () => { + await withProject({ + "staging.json": "staging", + }, async (rootPath) => { + assertEquals( + await collectDenoJsonFiles({ + rootPath, + buildDirectory: "apps/web", + uploaded: { "apps/web/deno.jsonc": "web" }, + configPath: join(rootPath, "staging.json"), + }), + { "apps/web/deno.json": "staging" }, + ); + }); +}); + +Deno.test("collectDenoJsonFiles: never reads outside the deploy root", async () => { + await withProject({ + "project/main.ts": "", + "private/deno.json": "secret", + }, async (dir) => { + const rootPath = join(dir, "project"); + for (const buildDirectory of ["../private", "..\\private"]) { + assertEquals( + await collectDenoJsonFiles({ rootPath, buildDirectory, uploaded: {} }), + {}, + buildDirectory, + ); + } + }); +}); + +Deno.test("collectDenoJsonFiles: never follows a symlink out of the deploy root", async () => { + await withProject({ + "project/main.ts": "", + "private/deno.json": "secret", + }, async (dir) => { + const rootPath = join(dir, "project"); + await Deno.symlink(join(dir, "private"), join(rootPath, "linked")); + await Deno.symlink( + join(dir, "private", "deno.json"), + join(rootPath, "deno.json"), + ); + assertEquals( + await collectDenoJsonFiles({ + rootPath, + buildDirectory: "linked", + uploaded: {}, + }), + {}, + ); + assertEquals( + await collectDenoJsonFiles({ + rootPath, + buildDirectory: "", + uploaded: {}, + }), + {}, + ); + }); +});