|
| 1 | +/* |
| 2 | + * IMPORTANT: |
| 3 | + * |
| 4 | + * This file is suffixed with `.isomorphic` because the code in this file is |
| 5 | + * meant to run not just in a nodejs environment but also in a browser. For this |
| 6 | + * reason there are some restrictions for which nodejs imports are allowed in |
| 7 | + * this module. See `.eslintrc.json` for more details. |
| 8 | + */ |
| 9 | +import { cloneDeep, get, has, set, unset } from "lodash"; |
| 10 | + |
| 11 | +import { AnyObj } from "@/lib/helpers/object.isomorphic"; |
| 12 | +import { ObjKeyOrArrayIdx, ObjPath } from "@/lib/helpers/object.isomorphic"; |
| 13 | +import { FILEPATH_MARKER } from "@/lib/marshal/shared/const.isomorphic"; |
| 14 | +import { ExtractionSettings, WithAnnotation } from "@/lib/marshal/shared/types"; |
| 15 | + |
| 16 | +import { prepareResourceJson } from "../shared/helpers.isomorphic"; |
| 17 | +import { ReusableStepData } from "./types"; |
| 18 | + |
| 19 | +export const REUSABLE_STEP_JSON = "reusable-step.json"; |
| 20 | + |
| 21 | +export type ReusableStepDirBundle = { |
| 22 | + [relpath: string]: string; |
| 23 | +}; |
| 24 | + |
| 25 | +/* |
| 26 | + * Traverse a given reusable step data and compile extraction settings of every |
| 27 | + * extractable field into a sorted map. |
| 28 | + * |
| 29 | + * NOTE: Currently we do NOT support content extraction at nested levels for |
| 30 | + * reusable steps. |
| 31 | + */ |
| 32 | +type CompiledExtractionSettings = Map<ObjKeyOrArrayIdx[], ExtractionSettings>; |
| 33 | + |
| 34 | +const compileExtractionSettings = ( |
| 35 | + reusableStep: ReusableStepData<WithAnnotation>, |
| 36 | +): CompiledExtractionSettings => { |
| 37 | + const extractableFields = get( |
| 38 | + reusableStep, |
| 39 | + ["__annotation", "extractable_fields"], |
| 40 | + {}, |
| 41 | + ); |
| 42 | + const map: CompiledExtractionSettings = new Map(); |
| 43 | + |
| 44 | + for (const key of Object.keys(reusableStep)) { |
| 45 | + // If the field we are on is extractable, then add its extraction |
| 46 | + // settings to the map with the current object path. |
| 47 | + if (key in extractableFields) { |
| 48 | + map.set([key], extractableFields[key]); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return map; |
| 53 | +}; |
| 54 | + |
| 55 | +/* |
| 56 | + * For a given reusable step payload, this function builds a "reusable step |
| 57 | + * directory bundle". This is an object which contains all the relative paths and |
| 58 | + * its file content. It includes the extractable fields, which are extracted out |
| 59 | + * and added to the bundle as separate files. |
| 60 | + */ |
| 61 | +export const buildReusableStepDirBundle = ( |
| 62 | + remoteReusableStep: ReusableStepData<WithAnnotation>, |
| 63 | + localReusableStep: AnyObj = {}, |
| 64 | +): ReusableStepDirBundle => { |
| 65 | + const bundle: ReusableStepDirBundle = {}; |
| 66 | + const mutRemoteReusableStep = cloneDeep(remoteReusableStep); |
| 67 | + // A map of extraction settings of every field in the reusable step |
| 68 | + const compiledExtractionSettings = compileExtractionSettings( |
| 69 | + mutRemoteReusableStep, |
| 70 | + ); |
| 71 | + |
| 72 | + // Iterate through each extractable field, determine whether we need to |
| 73 | + // extract the field content, and if so, perform the extraction. |
| 74 | + for (const [objPathParts, extractionSettings] of compiledExtractionSettings) { |
| 75 | + // If this reusable step doesn't have this field path, then we don't extract. |
| 76 | + if (!has(mutRemoteReusableStep, objPathParts)) continue; |
| 77 | + |
| 78 | + // If the field at this path is extracted in the local reusable step, then |
| 79 | + // always extract; otherwise extract based on the field settings default. |
| 80 | + const objPathStr = ObjPath.stringify(objPathParts); |
| 81 | + |
| 82 | + const extractedFilePath = get( |
| 83 | + localReusableStep, |
| 84 | + `${objPathStr}${FILEPATH_MARKER}`, |
| 85 | + ); |
| 86 | + |
| 87 | + const { default: extractByDefault, file_ext: fileExt } = extractionSettings; |
| 88 | + |
| 89 | + if (!extractedFilePath && !extractByDefault) continue; |
| 90 | + |
| 91 | + // By this point, we have a field where we need to extract its content. |
| 92 | + const data = get(mutRemoteReusableStep, objPathParts); |
| 93 | + const fileName = objPathParts.pop(); |
| 94 | + |
| 95 | + // If we have an extracted file path from the local reusable step, we use that. |
| 96 | + // In the other case we use the default path. |
| 97 | + const relpath = |
| 98 | + typeof extractedFilePath === "string" |
| 99 | + ? extractedFilePath |
| 100 | + : `${fileName}.${fileExt}`; |
| 101 | + |
| 102 | + // Perform the extraction by adding the content and its file path to the |
| 103 | + // bundle for writing to the file system later. Then replace the field |
| 104 | + // content with the extracted file path and mark the field as extracted |
| 105 | + // with @ suffix. |
| 106 | + const content = |
| 107 | + typeof data === "string" ? data : JSON.stringify(data, null, 2); |
| 108 | + |
| 109 | + set(bundle, [relpath], content); |
| 110 | + set(mutRemoteReusableStep, `${objPathStr}${FILEPATH_MARKER}`, relpath); |
| 111 | + unset(mutRemoteReusableStep, objPathStr); |
| 112 | + } |
| 113 | + |
| 114 | + // At this point the bundle contains all extractable files, so we finally add |
| 115 | + // the reusable step JSON relative path + the file content. |
| 116 | + |
| 117 | + return set( |
| 118 | + bundle, |
| 119 | + [REUSABLE_STEP_JSON], |
| 120 | + prepareResourceJson(mutRemoteReusableStep), |
| 121 | + ); |
| 122 | +}; |
0 commit comments