feat(cli): add the --import option for importing modules/packages before commands are run

Can for instance be used to load environment variables using Dotenv
This commit is contained in:
Joakim Carlstein 2023-12-20 11:01:01 +01:00 committed by Joakim Carlstein
parent e6e4433018
commit 9f91bdcfa0
12 changed files with 131 additions and 40 deletions

View file

@ -1,4 +1,3 @@
import process from 'node:process';
import { getOrLoadReporter, getOrLoadStorage } from '@emigrate/plugin-tools';
import { BadOptionError, MissingOptionError, StorageInitError, toError } from '../errors.js';
import { type Config } from '../types.js';
@ -10,17 +9,21 @@ import { version } from '../get-package-info.js';
const lazyDefaultReporter = async () => import('../reporters/default.js');
type ExtraFlags = {
cwd: string;
};
export default async function listCommand({
directory,
reporter: reporterConfig,
storage: storageConfig,
color,
}: Config) {
cwd,
}: Config & ExtraFlags) {
if (!directory) {
throw MissingOptionError.fromOption('directory');
}
const cwd = process.cwd();
const storagePlugin = await getOrLoadStorage([storageConfig]);
if (!storagePlugin) {

View file

@ -1,4 +1,4 @@
import process from 'node:process';
import { hrtime } from 'node:process';
import fs from 'node:fs/promises';
import path from 'node:path';
import { getTimestampPrefix, sanitizeMigrationName, getOrLoadPlugin, getOrLoadReporter } from '@emigrate/plugin-tools';
@ -18,8 +18,12 @@ import { getDuration } from '../get-duration.js';
const lazyDefaultReporter = async () => import('../reporters/default.js');
type ExtraFlags = {
cwd: string;
};
export default async function newCommand(
{ directory, template, reporter: reporterConfig, plugins = [], extension, color }: Config,
{ directory, template, reporter: reporterConfig, plugins = [], cwd, extension, color }: Config & ExtraFlags,
name: string,
) {
if (!directory) {
@ -34,8 +38,6 @@ export default async function newCommand(
throw MissingOptionError.fromOption(['extension', 'template', 'plugin']);
}
const cwd = process.cwd();
const reporter = await getOrLoadReporter([reporterConfig ?? lazyDefaultReporter]);
if (!reporter) {
@ -47,14 +49,14 @@ export default async function newCommand(
await reporter.onInit?.({ command: 'new', version, cwd, dry: false, directory, color });
const start = process.hrtime();
const start = hrtime();
let filename: string | undefined;
let content: string | undefined;
if (template) {
const fs = await import('node:fs/promises');
const templatePath = path.resolve(process.cwd(), template);
const templatePath = path.resolve(cwd, template);
const fileExtension = path.extname(templatePath);
try {
@ -98,7 +100,7 @@ export default async function newCommand(
);
}
const directoryPath = path.resolve(process.cwd(), directory);
const directoryPath = path.resolve(cwd, directory);
const filePath = path.resolve(directoryPath, filename);
const migration: MigrationMetadata = {

View file

@ -16,13 +16,14 @@ import { exec } from '../exec.js';
import { version } from '../get-package-info.js';
type ExtraFlags = {
cwd: string;
force?: boolean;
};
const lazyDefaultReporter = async () => import('../reporters/default.js');
export default async function removeCommand(
{ directory, reporter: reporterConfig, storage: storageConfig, color, force = false }: Config & ExtraFlags,
{ directory, reporter: reporterConfig, storage: storageConfig, color, cwd, force = false }: Config & ExtraFlags,
name: string,
) {
if (!directory) {
@ -33,7 +34,6 @@ export default async function removeCommand(
throw MissingArgumentsError.fromArgument('name');
}
const cwd = process.cwd();
const storagePlugin = await getOrLoadStorage([storageConfig]);
if (!storagePlugin) {
@ -49,6 +49,8 @@ export default async function removeCommand(
);
}
await reporter.onInit?.({ command: 'remove', version, cwd, dry: false, directory, color });
const [storage, storageError] = await exec(async () => storagePlugin.initializeStorage());
if (storageError) {
@ -57,8 +59,6 @@ export default async function removeCommand(
return 1;
}
await reporter.onInit?.({ command: 'remove', version, cwd, dry: false, directory, color });
const [migrationFile, fileError] = await exec(async () => getMigration(cwd, directory, name, !force));
if (fileError) {

View file

@ -1,4 +1,3 @@
import process from 'node:process';
import { getOrLoadPlugins, getOrLoadReporter, getOrLoadStorage } from '@emigrate/plugin-tools';
import { isFinishedMigration, type LoaderPlugin } from '@emigrate/types';
import { BadOptionError, MigrationLoadError, MissingOptionError, StorageInitError, toError } from '../errors.js';
@ -13,7 +12,7 @@ import { arrayFromAsync } from '../array-from-async.js';
import { version } from '../get-package-info.js';
type ExtraFlags = {
cwd?: string;
cwd: string;
dry?: boolean;
getMigrations?: GetMigrationsFunction;
};
@ -28,7 +27,7 @@ export default async function upCommand({
color,
dry = false,
plugins = [],
cwd = process.cwd(),
cwd,
getMigrations,
}: Config & ExtraFlags): Promise<number> {
if (!directory) {