feat(cli): handle file extensions with or without a leading period
This commit is contained in:
parent
23a323c675
commit
a058ebf888
3 changed files with 13 additions and 18 deletions
5
.changeset/fuzzy-panthers-bake.md
Normal file
5
.changeset/fuzzy-panthers-bake.md
Normal file
|
|
@ -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 ...`
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import { getTimestampPrefix, sanitizeMigrationName, loadPlugin, isGeneratorPlugin } from '@emigrate/plugin-tools';
|
import { getTimestampPrefix, sanitizeMigrationName, getOrLoadPlugin } from '@emigrate/plugin-tools';
|
||||||
import { type GeneratorPlugin } from '@emigrate/plugin-tools/types';
|
|
||||||
import { ShowUsageError } from './show-usage-error.js';
|
import { ShowUsageError } from './show-usage-error.js';
|
||||||
import { type Config } from './types.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) {
|
export default async function newCommand({ directory, template, plugins = [], extension }: Config, name: string) {
|
||||||
if (!directory) {
|
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 });
|
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) {
|
} else if (plugins.length > 0) {
|
||||||
let generatorPlugin: GeneratorPlugin | undefined;
|
const generatorPlugin = await getOrLoadPlugin('generator', plugins);
|
||||||
|
|
||||||
for await (const plugin of plugins) {
|
|
||||||
if (isGeneratorPlugin(plugin)) {
|
|
||||||
generatorPlugin = plugin;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
generatorPlugin = typeof plugin === 'string' ? await loadPlugin('generator', plugin) : undefined;
|
|
||||||
|
|
||||||
if (generatorPlugin) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!generatorPlugin) {
|
if (!generatorPlugin) {
|
||||||
throw new Error('No generator plugin found, please specify a generator plugin using the plugin option');
|
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;
|
content = generated.content;
|
||||||
} else if (extension) {
|
} else if (extension) {
|
||||||
content = '';
|
content = '';
|
||||||
filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}${extension}`;
|
filename = `${getTimestampPrefix()}_${sanitizeMigrationName(name)}.${stripLeadingPeriod(extension)}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!filename || content === undefined) {
|
if (!filename || content === undefined) {
|
||||||
|
|
|
||||||
1
packages/cli/src/strip-leading-period.ts
Normal file
1
packages/cli/src/strip-leading-period.ts
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
export const stripLeadingPeriod = (string: string) => string.replace(/^\./, '');
|
||||||
Loading…
Add table
Add a link
Reference in a new issue