From 82fd6d448cee97492a0471412f673003d7728649 Mon Sep 17 00:00:00 2001 From: henryfauna <90654917+henryfauna@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:01:40 -0800 Subject: [PATCH] Notify on `schema push` if no schema files are found (#507) * warn user if no fsl files are found * remove .only * throw a validation error * fix import --------- Co-authored-by: E. Cooper --- src/commands/schema/push.mjs | 15 ++++++++++++--- test/schema/push.mjs | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/commands/schema/push.mjs b/src/commands/schema/push.mjs index def4c50b..a56e5e62 100644 --- a/src/commands/schema/push.mjs +++ b/src/commands/schema/push.mjs @@ -1,6 +1,9 @@ //@ts-check +import path from "path"; + import { container } from "../../cli.mjs"; +import { ValidationError } from "../../lib/errors.mjs"; import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs"; import { getSecret } from "../../lib/fauna-client.mjs"; import { reformatFSL } from "../../lib/schema.mjs"; @@ -13,10 +16,16 @@ async function doPush(argv) { 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); - const fsl = reformatFSL(await gatherFSL(argv.dir)); - - if (!argv.input) { + 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", diff --git a/test/schema/push.mjs b/test/schema/push.mjs index b93a4b22..eee7324b 100644 --- a/test/schema/push.mjs +++ b/test/schema/push.mjs @@ -1,9 +1,11 @@ //@ts-check import { expect } from "chai"; +import chalk from "chalk"; +import path from "path"; import sinon from "sinon"; -import { run } from "../../src/cli.mjs"; +import { builtYargs, run } from "../../src/cli.mjs"; import { setupTestContainer as setupContainer } from "../../src/config/setup-test-container.mjs"; import { reformatFSL } from "../../src/lib/schema.mjs"; import { buildUrl, f } from "../helpers.mjs"; @@ -31,6 +33,21 @@ describe("schema push", function () { gatherFSL.resolves(fsl); }); + it("notifies the user when no schema files are found", async function () { + gatherFSL.resolves([]); + const absoluteDirPath = path.resolve("."); + + try { + await run(`schema push --secret "secret"`, container); + } catch (e) {} + + expect(logger.stderr).to.have.been.calledWith( + `${chalk.reset(await builtYargs.getHelp())}\n\n${chalk.red( + `No schema files (*.fsl) found in '${absoluteDirPath}'. Use '--dir' to specify a different directory, or create new .fsl files in this location.`, + )}`, + ); + }); + it("can push a schema without user input", async function () { await run(`schema push --secret "secret" --no-input`, container);