Skip to content

Commit

Permalink
Move schema back into commands dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ecooper committed Dec 16, 2024
1 parent 23a4cb4 commit 18aa4f8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/commands/local.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
95 changes: 94 additions & 1 deletion src/commands/schema/push.mjs
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
89 changes: 0 additions & 89 deletions src/lib/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 18aa4f8

Please sign in to comment.