diff --git a/src/commands/local.mjs b/src/commands/local.mjs index 526aa50b..c78d0555 100644 --- a/src/commands/local.mjs +++ b/src/commands/local.mjs @@ -2,10 +2,10 @@ import chalk from "chalk"; import { AbortError } from "fauna"; import { container } from "../cli.mjs"; +import { pushSchema } from "../commands/schema/push.mjs"; import { ensureContainerRunning } from "../lib/docker-containers.mjs"; import { CommandError, ValidationError } from "../lib/errors.mjs"; import { colorize, Format } from "../lib/formatting/colorize.mjs"; -import { pushSchema } from "../lib/schema.mjs"; /** * Starts the local Fauna container diff --git a/src/commands/schema/push.mjs b/src/commands/schema/push.mjs index 802f590f..e87ec3bf 100644 --- a/src/commands/schema/push.mjs +++ b/src/commands/schema/push.mjs @@ -1,9 +1,102 @@ //@ts-check +import path from "path"; + +import { container } from "../../cli.mjs"; import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs"; -import { pushSchema } from "../../lib/schema.mjs"; +import { ValidationError } from "../../lib/errors.mjs"; +import { getSecret } from "../../lib/fauna-client.mjs"; +import { reformatFSL } from "../../lib/schema.mjs"; import { localSchemaOptions } from "./schema.mjs"; +/** + * Pushes a schema (FSL) based on argv. + * @param {import("yargs").Argv & {dir: string, active: boolean, input: boolean}} argv + */ +export async function pushSchema(argv) { + const logger = container.resolve("logger"); + const makeFaunaRequest = container.resolve("makeFaunaRequest"); + const gatherFSL = container.resolve("gatherFSL"); + + const isStagedPush = !argv.active; + const secret = await getSecret(); + const fslFiles = await gatherFSL(argv.dir); + const hasLocalSchema = fslFiles.length > 0; + const absoluteDirPath = path.resolve(argv.dir); + const fsl = reformatFSL(fslFiles); + + if (!hasLocalSchema) { + throw new ValidationError( + `No schema files (*.fsl) found in '${absoluteDirPath}'. Use '--dir' to specify a different directory, or create new .fsl files in this location.`, + ); + } else if (!argv.input) { + const params = new URLSearchParams({ + force: "true", + staged: argv.active ? "false" : "true", + }); + + await makeFaunaRequest({ + argv, + path: "/schema/1/update", + params, + body: fsl, + method: "POST", + secret, + }); + } else { + // Confirm diff, then push it. + const params = new URLSearchParams({ + staged: argv.active ? "false" : "true", + }); + + const response = await makeFaunaRequest({ + argv, + path: "/schema/1/diff", + params, + body: fsl, + method: "POST", + secret, + }); + + let message = isStagedPush + ? "Stage the above changes?" + : "Push the above changes?"; + if (response.diff) { + logger.stdout(`Proposed diff:\n`); + logger.stdout(response.diff); + } else { + logger.stdout("No logical changes."); + message = isStagedPush + ? "Stage the file contents anyway?" + : "Push the file contents anyway?"; + } + + const confirm = container.resolve("confirm"); + const confirmed = await confirm({ + message, + default: false, + }); + + if (confirmed) { + const params = new URLSearchParams({ + version: response.version, + staged: argv.active ? "false" : "true", + }); + + await makeFaunaRequest({ + argv, + path: "/schema/1/update", + params, + body: fsl, + method: "POST", + secret, + }); + } else { + logger.stdout("Push cancelled."); + } + } +} + function buildPushCommand(yargs) { return yargsWithCommonQueryOptions(yargs) .options({ diff --git a/src/lib/schema.mjs b/src/lib/schema.mjs index 407f3d24..ab02ea03 100644 --- a/src/lib/schema.mjs +++ b/src/lib/schema.mjs @@ -4,98 +4,9 @@ import * as path from "path"; import { container } from "../cli.mjs"; import { makeFaunaRequest } from "../lib/db.mjs"; -import { ValidationError } from "./errors.mjs"; import { getSecret } from "./fauna-client.mjs"; import { dirExists, dirIsWriteable } from "./file-util.mjs"; -/** - * Pushes a schema (FSL) based on argv. - * @param {import("yargs").Argv & {dir: string, active: boolean, input: boolean}} argv - */ -export async function pushSchema(argv) { - const logger = container.resolve("logger"); - const makeFaunaRequest = container.resolve("makeFaunaRequest"); - const gatherFSL = container.resolve("gatherFSL"); - - const isStagedPush = !argv.active; - const secret = await getSecret(); - const fslFiles = await gatherFSL(argv.dir); - const hasLocalSchema = fslFiles.length > 0; - const absoluteDirPath = path.resolve(argv.dir); - const fsl = reformatFSL(fslFiles); - - if (!hasLocalSchema) { - throw new ValidationError( - `No schema files (*.fsl) found in '${absoluteDirPath}'. Use '--dir' to specify a different directory, or create new .fsl files in this location.`, - ); - } else if (!argv.input) { - const params = new URLSearchParams({ - force: "true", - staged: argv.active ? "false" : "true", - }); - - await makeFaunaRequest({ - argv, - path: "/schema/1/update", - params, - body: fsl, - method: "POST", - secret, - }); - } else { - // Confirm diff, then push it. - const params = new URLSearchParams({ - staged: argv.active ? "false" : "true", - }); - - const response = await makeFaunaRequest({ - argv, - path: "/schema/1/diff", - params, - body: fsl, - method: "POST", - secret, - }); - - let message = isStagedPush - ? "Stage the above changes?" - : "Push the above changes?"; - if (response.diff) { - logger.stdout(`Proposed diff:\n`); - logger.stdout(response.diff); - } else { - logger.stdout("No logical changes."); - message = isStagedPush - ? "Stage the file contents anyway?" - : "Push the file contents anyway?"; - } - - const confirm = container.resolve("confirm"); - const confirmed = await confirm({ - message, - default: false, - }); - - if (confirmed) { - const params = new URLSearchParams({ - version: response.version, - staged: argv.active ? "false" : "true", - }); - - await makeFaunaRequest({ - argv, - path: "/schema/1/update", - params, - body: fsl, - method: "POST", - secret, - }); - } else { - logger.stdout("Push cancelled."); - } - } -} - /** * @param {string} dir - The directory path to check for existence and write access */