From a1debba2e178f390caad6a5bd325be065ea18824 Mon Sep 17 00:00:00 2001 From: Joakim Carlstein Date: Thu, 16 Nov 2023 10:51:43 +0100 Subject: [PATCH] fix(cli): allow creating new migration files with only the "extension" option --- .changeset/unlucky-files-swim.md | 5 +++++ packages/cli/src/new-command.ts | 22 ++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 .changeset/unlucky-files-swim.md diff --git a/.changeset/unlucky-files-swim.md b/.changeset/unlucky-files-swim.md new file mode 100644 index 0000000..80917b1 --- /dev/null +++ b/.changeset/unlucky-files-swim.md @@ -0,0 +1,5 @@ +--- +'@emigrate/cli': patch +--- + +Fix a logical error that didn't allow creating new migration files with only the "extension" option diff --git a/packages/cli/src/new-command.ts b/packages/cli/src/new-command.ts index 6da7355..7b22e45 100644 --- a/packages/cli/src/new-command.ts +++ b/packages/cli/src/new-command.ts @@ -37,24 +37,30 @@ export default async function newCommand({ directory, template, plugins = [], ex filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod( extension ?? fileExtension, )}`; - } else if (plugins.length > 0) { + } + + let hasGeneratedFile = Boolean(filename && content !== undefined); + + if (plugins.length > 0 && !hasGeneratedFile) { const generatorPlugin = await getOrLoadPlugin('generator', plugins); - if (!generatorPlugin) { - throw new Error('No generator plugin found, please specify a generator plugin using the plugin option'); + if (generatorPlugin) { + const generated = await generatorPlugin.generateMigration(name); + + filename = generated.filename; + content = generated.content; } + } - const generated = await generatorPlugin.generateMigration(name); + hasGeneratedFile = Boolean(filename && content !== undefined); - filename = generated.filename; - content = generated.content; - } else if (extension) { + if (extension && !hasGeneratedFile) { content = ''; filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod(extension)}`; } if (!filename || content === undefined) { - throw new Error('Unexpected error, missing filename or content for migration file'); + throw new ShowUsageError('No generator plugin found, please specify a generator plugin using the plugin option'); } const directoryPath = path.resolve(process.cwd(), directory);