diff --git a/fixtures/dup-ok1.js b/fixtures/duplicate/ok1.js similarity index 83% rename from fixtures/dup-ok1.js rename to fixtures/duplicate/ok1.js index c10c5a4..8c99113 100644 --- a/fixtures/dup-ok1.js +++ b/fixtures/duplicate/ok1.js @@ -1,6 +1,5 @@ module.exports = { - title: 'OK1', up: function(client) { }, diff --git a/fixtures/ok1.js b/fixtures/ok1.js index c10c5a4..8c99113 100644 --- a/fixtures/ok1.js +++ b/fixtures/ok1.js @@ -1,6 +1,5 @@ module.exports = { - title: 'OK1', up: function(client) { }, diff --git a/fixtures/ok2.js b/fixtures/ok2.js index 40ac936..8c99113 100644 --- a/fixtures/ok2.js +++ b/fixtures/ok2.js @@ -1,6 +1,5 @@ module.exports = { - title: 'OK2', up: function(client) { }, diff --git a/src/loader.test.ts b/src/loader.test.ts index a1f5ac1..f5d8cd8 100644 --- a/src/loader.test.ts +++ b/src/loader.test.ts @@ -15,18 +15,18 @@ describe('Loader', function() { it('Should resolve an OK migration', async function() { const loader = new Loader(fixtures, 'ok1.js'); await expect(loader.load()).resolves.toEqual([ - expect.objectContaining({title: 'OK1'}) + expect.objectContaining({title: 'ok1'}) ]); }); it('Should sort migrations by title', async function() { const loader = new Loader(fixtures, ['ok2.js', 'ok1.js']); await expect(loader.load()).resolves.toEqual([ - expect.objectContaining({title: 'OK1'}), - expect.objectContaining({title: 'OK2'}) + expect.objectContaining({title: 'ok1'}), + expect.objectContaining({title: 'ok2'}) ]); }); it('Should require a unique name for each migration', async function() { - const loader = new Loader(fixtures, ['ok1.js', 'dup-ok1.js']); - await expect(loader.load()).rejects.toThrowError('Duplicate migration detected: OK1'); - }) + const loader = new Loader(fixtures, ['ok1.js', 'duplicate/ok1.js']); + await expect(loader.load()).rejects.toThrowError('Duplicate migration detected: ok1'); + }); }) diff --git a/src/loader.ts b/src/loader.ts index db28151..5df1336 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -1,7 +1,7 @@ import {Migration, MigrationSet} from './migration' import glob from 'glob'; import {promisify} from "util"; -import {join} from 'path' +import {join, basename} from 'path' import Maybe = jest.Maybe; const globp = promisify(glob); @@ -31,29 +31,15 @@ export default class Loader { throw new Error(`Unable to load migration from ${filename} - this does not look like a JS file.`); } const prospect = require(join(this.cwd, filename)) - if(!this.isMigration(prospect)) { + + if(!isMigrationStub(prospect)) { throw new Error(`Unable to load migration from ${filename} - this file needs to export an object with a title, up, and down.`); } - return prospect + return Object.assign({}, prospect, {title: basename(filename, '.js')}) } private sort(a: Migration, b: Migration): number { return a.title > b.title ? 1 : -1 } - private isMigration(prospect: Maybe): prospect is Migration { - if(typeof prospect !== 'object' || prospect === null) { - return false - } - if(!('title' in prospect) || !prospect.title.length) { - return false - } - if(!('up' in prospect) || typeof prospect.up !== 'function') { - return false; - } - if(!('down' in prospect) || typeof prospect.down !== 'function') { - return false; - } - return true - } private assertUnique(migrations: MigrationSet) { const seen: {[k: string]: boolean} = {} migrations.forEach(function(migration) { @@ -66,3 +52,18 @@ export default class Loader { }) } } + +type MigrationStub = Pick + +function isMigrationStub(prospect: Maybe): prospect is MigrationStub { + if(typeof prospect !== 'object' || prospect === null) { + return false + } + if(!('up' in prospect) || typeof prospect.up !== 'function') { + return false; + } + if(!('down' in prospect) || typeof prospect.down !== 'function') { + return false; + } + return true +} diff --git a/src/migration.ts b/src/migration.ts index 6b21647..8d2ebe8 100644 --- a/src/migration.ts +++ b/src/migration.ts @@ -1,6 +1,6 @@ import {Client as ElasticsearchClient} from "@elastic/elasticsearch"; -export interface Migration { +export type Migration = { title: string up: (client: ElasticsearchClient) => Promise down: (client: ElasticsearchClient) => Promise