fix(cli): allow creating new migration files with only the "extension" option

This commit is contained in:
Joakim Carlstein 2023-11-16 10:51:43 +01:00
parent b56794a269
commit a1debba2e1
2 changed files with 19 additions and 8 deletions

View file

@ -0,0 +1,5 @@
---
'@emigrate/cli': patch
---
Fix a logical error that didn't allow creating new migration files with only the "extension" option

View file

@ -37,24 +37,30 @@ export default async function newCommand({ directory, template, plugins = [], ex
filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod( filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod(
extension ?? fileExtension, extension ?? fileExtension,
)}`; )}`;
} else if (plugins.length > 0) {
const generatorPlugin = await getOrLoadPlugin('generator', plugins);
if (!generatorPlugin) {
throw new Error('No generator plugin found, please specify a generator plugin using the plugin option');
} }
let hasGeneratedFile = Boolean(filename && content !== undefined);
if (plugins.length > 0 && !hasGeneratedFile) {
const generatorPlugin = await getOrLoadPlugin('generator', plugins);
if (generatorPlugin) {
const generated = await generatorPlugin.generateMigration(name); const generated = await generatorPlugin.generateMigration(name);
filename = generated.filename; filename = generated.filename;
content = generated.content; content = generated.content;
} else if (extension) { }
}
hasGeneratedFile = Boolean(filename && content !== undefined);
if (extension && !hasGeneratedFile) {
content = ''; content = '';
filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod(extension)}`; filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod(extension)}`;
} }
if (!filename || content === undefined) { 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); const directoryPath = path.resolve(process.cwd(), directory);