Skip to content

Commit

Permalink
fix(cli): allow creating new migration files with only the "extension…
Browse files Browse the repository at this point in the history
…" option
  • Loading branch information
joakimbeng committed Nov 16, 2023
1 parent b56794a commit a1debba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-files-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emigrate/cli': patch
---

Fix a logical error that didn't allow creating new migration files with only the "extension" option
22 changes: 14 additions & 8 deletions packages/cli/src/new-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a1debba

Please sign in to comment.