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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 1x 1x 1x 1x 1x 4x 4x 4x 4x 1x 1x 4x 4x 4x 2x 14x 14x 4x 60x 60x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 4x 8x 8x 8x 8x 8x 8x 4x 4x 1x 1x | import path from 'path'; import fs from 'fs-extra'; import { constants, loadFileAndReplaceKeywords } from '../../../tools'; import log from '../../../logger'; import { isDirectory, existsMustBeDir, dumpJSON, loadJSON, sanitize, mapClientID2NameSorted, } from '../../../utils'; import { DirectoryHandler } from '.'; import DirectoryContext from '..'; import { Asset, KeywordMappings, ParsedAsset } from '../../../types'; type ParsedDatabases = ParsedAsset<'databases', Asset[]>; type DatabaseMetadata = { options?: { customScripts?: { change_password: string; create: string; delete: string; get_user: string; login: string; verify: string; }; }; }; function getDatabase( folder: string, mappingOpts: { mappings: KeywordMappings; disableKeywordReplacement: boolean } ): {} { const metaFile = path.join(folder, 'database.json'); const metaData: DatabaseMetadata | {} = (() => { try { return loadJSON(metaFile, mappingOpts); } catch (err) { log.warn(`Skipping database folder ${folder} as cannot find or read ${metaFile}`); return {}; } })(); Iif (!metaData) { log.warn(`Skipping database folder ${folder} as ${metaFile} is empty`); return {}; } const database = { ...metaData, options: { //@ts-ignore because this code exists currently, but still needs to be understood if it is correct or not ...metaData.options, //@ts-ignore because this code exists currently, but still needs to be understood if it is correct or not ...(metaData.customScripts && { customScripts: metaData.customScripts }), }, }; // If any customScripts configured then load content of files if (database.options.customScripts) { Object.entries(database.options.customScripts).forEach(([name, script]: [string, string]) => { Iif (!constants.DATABASE_SCRIPTS.includes(name)) { // skip invalid keys in customScripts object log.warn('Skipping invalid database configuration: ' + name); } else { database.options.customScripts[name] = loadFileAndReplaceKeywords( path.join(folder, script), mappingOpts ); } }); } return database; } function parse(context: DirectoryContext): ParsedDatabases { const databaseFolder = path.join(context.filePath, constants.DATABASE_CONNECTIONS_DIRECTORY); if (!existsMustBeDir(databaseFolder)) return { databases: null }; // Skip const folders = fs .readdirSync(databaseFolder) .map((f) => path.join(databaseFolder, f)) .filter((f) => isDirectory(f)); const databases = folders .map((f) => getDatabase(f, { mappings: context.mappings, disableKeywordReplacement: context.disableKeywordReplacement, }) ) .filter((p) => Object.keys(p).length > 1); return { databases, }; } async function dump(context: DirectoryContext): Promise<void> { const { databases } = context.assets; Iif (!databases) return; // Skip, nothing to dump const databasesFolder = path.join(context.filePath, constants.DATABASE_CONNECTIONS_DIRECTORY); fs.ensureDirSync(databasesFolder); databases.forEach((database) => { const dbFolder = path.join(databasesFolder, sanitize(database.name)); fs.ensureDirSync(dbFolder); const sortCustomScripts = (name1: string, name2: string): 1 | 0 | -1 => { Iif (name1 === name2) return 0; return name1 > name2 ? 1 : -1; }; const formatted = { ...database, ...(database.enabled_clients && { enabled_clients: mapClientID2NameSorted( database.enabled_clients, context.assets.clientsOrig || [] ), }), 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]) => { // Dump custom script to file const scriptName = sanitize(`${name}.js`); const scriptFile = path.join(dbFolder, scriptName); log.info(`Writing ${scriptFile}`); //@ts-ignore because we'll fix this in subsequent PR fs.writeFileSync(scriptFile, script); scripts[name] = `./${scriptName}`; return scripts; }, {}), }), }, }; const databaseFile = path.join(dbFolder, 'database.json'); dumpJSON(databaseFile, formatted); }); } const databasesHandler: DirectoryHandler<ParsedDatabases> = { parse, dump, }; export default databasesHandler; |