All files / src/context/yaml/handlers databases.ts

93.33% Statements 28/30
81.25% Branches 13/16
100% Functions 7/7
100% Lines 27/27

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 901x 1x 1x 1x                 41x   41x   2x   2x             7x                           7x   7x   7x 6x 6x     7x   3x                           8x 8x 8x     8x 8x 8x   8x 8x 8x               1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { mapClientID2NameSorted, sanitize } from '../../../utils';
import log from '../../../logger';
import { YAMLHandler } from '.';
import YAMLContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type ParsedDatabases = ParsedAsset<'databases', Asset[]>;
 
async function parse(context: YAMLContext): Promise<ParsedDatabases> {
  // Load the script file for custom db
  const { databases } = context.assets;
 
  if (!databases) return { databases: null };
 
  return {
    databases: [
      ...databases.map((database) => ({
        ...database,
        options: {
          ...database.options,
          // customScripts option only written if there are scripts
          ...(database.options.customScripts && {
            customScripts: Object.entries(database.options.customScripts).reduce(
              (scripts, [name, script]) => ({
                ...scripts,
                [name]: context.loadFile(script),
              }),
              {}
            ),
          }),
        },
      })),
    ],
  };
}
 
async function dump(context: YAMLContext): Promise<ParsedDatabases> {
  const { databases, clients } = context.assets;
 
  Iif (!databases) return { databases: null };
 
  const sortCustomScripts = ([name1]: [string, Function], [name2]: [string, Function]): number => {
    Iif (name1 === name2) return 0;
    return name1 > name2 ? 1 : -1;
  };
 
  return {
    databases: [
      ...databases.map((database) => ({
        ...database,
        ...(database.enabled_clients && {
          enabled_clients: mapClientID2NameSorted(database.enabled_clients, clients || []),
        }),
        options: {
          ...database.options,
          // customScripts option only written if there are scripts
          ...(database.options.customScripts && {
            customScripts: Object.entries(database.options.customScripts)
              //@ts-ignore because we'll fix this in subsequent PR
              .sort(sortCustomScripts)
              .reduce((scripts, [name, script]) => {
                // Create Database folder
                const dbName = sanitize(database.name);
                const dbFolder = path.join(context.basePath, 'databases', sanitize(dbName));
                fs.ensureDirSync(dbFolder);
 
                // Dump custom script to file
                const scriptName = sanitize(name);
                const scriptFile = path.join(dbFolder, `${scriptName}.js`);
                log.info(`Writing ${scriptFile}`);
                //@ts-ignore because we'll fix this in subsequent PR
                fs.writeFileSync(scriptFile, script);
                scripts[name] = `./databases/${dbName}/${scriptName}.js`;
                return scripts;
              }, {}),
          }),
        },
      })),
    ],
  };
}
const databasesHandler: YAMLHandler<ParsedDatabases> = {
  parse,
  dump,
};
 
export default databasesHandler;