Skip to content

Commit

Permalink
Refactor location of push logic to enable code sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
cleve-fauna committed Dec 16, 2024
1 parent 752efe5 commit a8e3ecb
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 90 deletions.
92 changes: 2 additions & 90 deletions src/commands/schema/push.mjs
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -133,5 +45,5 @@ export default {
command: "push",
description: "Push local .fsl schema files to Fauna.",
builder: buildPushCommand,
handler: doPush,
handler: pushSchema,
};
88 changes: 88 additions & 0 deletions src/lib/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit a8e3ecb

Please sign in to comment.