From a8e3ecbbeb35ce9d3cc29f5cafccb00ae2d8d07f Mon Sep 17 00:00:00 2001 From: Cleve Stuart Date: Mon, 16 Dec 2024 16:27:48 -0500 Subject: [PATCH] Refactor location of push logic to enable code sharing --- src/commands/schema/push.mjs | 92 +----------------------------------- src/lib/schema.mjs | 88 ++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 90 deletions(-) diff --git a/src/commands/schema/push.mjs b/src/commands/schema/push.mjs index 72d6d95d..802f590f 100644 --- a/src/commands/schema/push.mjs +++ b/src/commands/schema/push.mjs @@ -1,97 +1,9 @@ //@ts-check -import path from "path"; - -import { container } from "../../cli.mjs"; import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs"; -import { ValidationError } from "../../lib/errors.mjs"; -import { getSecret } from "../../lib/fauna-client.mjs"; -import { reformatFSL } from "../../lib/schema.mjs"; +import { pushSchema } from "../../lib/schema.mjs"; import { localSchemaOptions } from "./schema.mjs"; -async function doPush(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({ @@ -133,5 +45,5 @@ export default { command: "push", description: "Push local .fsl schema files to Fauna.", builder: buildPushCommand, - handler: doPush, + handler: pushSchema, }; diff --git a/src/lib/schema.mjs b/src/lib/schema.mjs index ab02ea03..8d190588 100644 --- a/src/lib/schema.mjs +++ b/src/lib/schema.mjs @@ -4,9 +4,97 @@ 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} yargs + */ +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 */