|
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs-extra'; |
| 3 | +import { constants } from '../../../tools'; |
| 4 | +import log from '../../../logger'; |
| 5 | +import { getFiles, existsMustBeDir, dumpJSON, loadJSON, sanitize } from '../../../utils'; |
| 6 | +import { DirectoryHandler } from '.'; |
| 7 | +import DirectoryContext from '..'; |
| 8 | +import { Asset, ParsedAsset } from '../../../types'; |
| 9 | + |
| 10 | +type ParsedTokenExchangeProfiles = ParsedAsset<'tokenExchangeProfiles', Asset[]>; |
| 11 | + |
| 12 | +function parse(context: DirectoryContext): ParsedTokenExchangeProfiles { |
| 13 | + const folder = path.join(context.filePath, constants.TOKEN_EXCHANGE_PROFILES_DIRECTORY); |
| 14 | + if (!existsMustBeDir(folder)) return { tokenExchangeProfiles: null }; // Skip |
| 15 | + |
| 16 | + const files = getFiles(folder, ['.json']); |
| 17 | + |
| 18 | + const profiles = files.map((f) => |
| 19 | + loadJSON(f, { |
| 20 | + mappings: context.mappings, |
| 21 | + disableKeywordReplacement: context.disableKeywordReplacement, |
| 22 | + }) |
| 23 | + ); |
| 24 | + |
| 25 | + return { |
| 26 | + tokenExchangeProfiles: profiles, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +async function dump(context: DirectoryContext) { |
| 31 | + const { tokenExchangeProfiles } = context.assets; |
| 32 | + |
| 33 | + if (!tokenExchangeProfiles || !Array.isArray(tokenExchangeProfiles)) return; // Skip |
| 34 | + |
| 35 | + const folder = path.join(context.filePath, constants.TOKEN_EXCHANGE_PROFILES_DIRECTORY); |
| 36 | + fs.ensureDirSync(folder); |
| 37 | + |
| 38 | + tokenExchangeProfiles.forEach((profile) => { |
| 39 | + const { id, created_at, updated_at, ...profileWithoutMetadata } = profile; |
| 40 | + const fileName = path.join(folder, sanitize(`${profile.name}.json`)); |
| 41 | + log.info(`Writing ${fileName}`); |
| 42 | + dumpJSON(fileName, profileWithoutMetadata); |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +const handler: DirectoryHandler<ParsedTokenExchangeProfiles> = { |
| 47 | + parse, |
| 48 | + dump, |
| 49 | +}; |
| 50 | + |
| 51 | +export default handler; |
0 commit comments