feat(cli): implement a default "loader" for JavaScript files
Supports any of the `.js`, `.cjs` and `.mjs` file extensions
This commit is contained in:
parent
62bd5a45e5
commit
3b36b3de52
3 changed files with 55 additions and 3 deletions
44
packages/cli/src/plugin-loader-js.ts
Normal file
44
packages/cli/src/plugin-loader-js.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { type LoaderPlugin } from '@emigrate/plugin-tools/types';
|
||||
|
||||
const loaderJs: LoaderPlugin = {
|
||||
loadableExtensions: ['.js', '.cjs', '.mjs'],
|
||||
async loadMigration(migration) {
|
||||
const migrationModule: unknown = await import(migration.filePath);
|
||||
|
||||
if (typeof migrationModule === 'function') {
|
||||
return async () => {
|
||||
await migrationModule();
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
migrationModule &&
|
||||
typeof migrationModule === 'object' &&
|
||||
'default' in migrationModule &&
|
||||
typeof migrationModule.default === 'function'
|
||||
) {
|
||||
const migrationFunction = migrationModule.default;
|
||||
return async () => {
|
||||
await migrationFunction();
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
migrationModule &&
|
||||
typeof migrationModule === 'object' &&
|
||||
'up' in migrationModule &&
|
||||
typeof migrationModule.up === 'function'
|
||||
) {
|
||||
const migrationFunction = migrationModule.up;
|
||||
return async () => {
|
||||
await migrationFunction();
|
||||
};
|
||||
}
|
||||
|
||||
return async () => {
|
||||
throw new Error(`Migration file does not export a function: ${migration.relativeFilePath}`);
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
export default loaderJs;
|
||||
Loading…
Add table
Add a link
Reference in a new issue