All files / src/context/directory/handlers migrations.ts

88.23% Statements 15/17
75% Branches 6/8
100% Functions 2/2
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x 1x               54x 54x   54x   54x     1x         1x       1x   1x   1x 1x     1x         1x  
import path from 'path';
import { existsMustBeDir, isFile, dumpJSON, loadJSON } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type ParsedMigrations = ParsedAsset<'migrations', Asset[]>;
 
function parse(context: DirectoryContext): ParsedMigrations {
  const baseFolder = path.join(context.filePath);
  Iif (!existsMustBeDir(baseFolder)) return { migrations: null }; // Skip
 
  const migrationsFile = path.join(baseFolder, 'migrations.json');
 
  if (!isFile(migrationsFile)) return { migrations: null };
 
  /* eslint-disable camelcase */
  const migrations = loadJSON(migrationsFile, {
    mappings: context.mappings,
    disableKeywordReplacement: context.disableKeywordReplacement,
  });
 
  return { migrations };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const { migrations } = context.assets;
 
  Iif (!migrations || Object.keys(migrations).length === 0) return; // Skip, nothing to dump
 
  const migrationsFile = path.join(context.filePath, 'migrations.json');
  dumpJSON(migrationsFile, migrations);
}
 
const migrationsHandler: DirectoryHandler<ParsedMigrations> = {
  parse,
  dump,
};
 
export default migrationsHandler;