From a058ebf88861caf06fc04693d15d050ddc7d2a1b Mon Sep 17 00:00:00 2001 From: Joakim Carlstein Date: Wed, 15 Nov 2023 16:11:28 +0100 Subject: [PATCH] feat(cli): handle file extensions with or without a leading period --- .changeset/fuzzy-panthers-bake.md | 5 +++++ packages/cli/src/new-command.ts | 25 +++++++----------------- packages/cli/src/strip-leading-period.ts | 1 + 3 files changed, 13 insertions(+), 18 deletions(-) create mode 100644 .changeset/fuzzy-panthers-bake.md create mode 100644 packages/cli/src/strip-leading-period.ts diff --git a/.changeset/fuzzy-panthers-bake.md b/.changeset/fuzzy-panthers-bake.md new file mode 100644 index 0000000..052c0c1 --- /dev/null +++ b/.changeset/fuzzy-panthers-bake.md @@ -0,0 +1,5 @@ +--- +'@emigrate/cli': minor +--- + +Handle file extensions with or without a leading period, i.e. `emigrate new -e .js ...` is now the same as `emigrate new -e js ...` diff --git a/packages/cli/src/new-command.ts b/packages/cli/src/new-command.ts index 3577ca3..6da7355 100644 --- a/packages/cli/src/new-command.ts +++ b/packages/cli/src/new-command.ts @@ -1,10 +1,10 @@ import process from 'node:process'; import fs from 'node:fs/promises'; import path from 'node:path'; -import { getTimestampPrefix, sanitizeMigrationName, loadPlugin, isGeneratorPlugin } from '@emigrate/plugin-tools'; -import { type GeneratorPlugin } from '@emigrate/plugin-tools/types'; +import { getTimestampPrefix, sanitizeMigrationName, getOrLoadPlugin } from '@emigrate/plugin-tools'; import { ShowUsageError } from './show-usage-error.js'; import { type Config } from './types.js'; +import { stripLeadingPeriod } from './strip-leading-period.js'; export default async function newCommand({ directory, template, plugins = [], extension }: Config, name: string) { if (!directory) { @@ -34,22 +34,11 @@ export default async function newCommand({ directory, template, plugins = [], ex throw new Error(`Failed to read template file: ${templatePath}`, { cause: error }); } - filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}${extension ?? fileExtension}`; + filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod( + extension ?? fileExtension, + )}`; } else if (plugins.length > 0) { - let generatorPlugin: GeneratorPlugin | undefined; - - for await (const plugin of plugins) { - if (isGeneratorPlugin(plugin)) { - generatorPlugin = plugin; - break; - } - - generatorPlugin = typeof plugin === 'string' ? await loadPlugin('generator', plugin) : undefined; - - if (generatorPlugin) { - break; - } - } + const generatorPlugin = await getOrLoadPlugin('generator', plugins); if (!generatorPlugin) { throw new Error('No generator plugin found, please specify a generator plugin using the plugin option'); @@ -61,7 +50,7 @@ export default async function newCommand({ directory, template, plugins = [], ex content = generated.content; } else if (extension) { content = ''; - filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}${extension}`; + filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod(extension)}`; } if (!filename || content === undefined) { diff --git a/packages/cli/src/strip-leading-period.ts b/packages/cli/src/strip-leading-period.ts new file mode 100644 index 0000000..33073a0 --- /dev/null +++ b/packages/cli/src/strip-leading-period.ts @@ -0,0 +1 @@ +export const stripLeadingPeriod = (string: string) => string.replace(/^\./, '');