UNPKG

@armanatz/expo-hms-location

Version:

Expo config plugin to configure @hmscore/react-native-hms-location on prebuild

44 lines (43 loc) 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertOrReplaceContentInBlock = insertOrReplaceContentInBlock; const generateCode_1 = require("./generateCode"); /** * Insert or replace content inside of a code block located in a file and add a generated header. * * @param mode Choose between `insert` or `replace` * @param src Contents of the original file * @param newSrc New content to insert or replace * @param anchor If `mode` is `insert`, the line to insert the new content after. * If `mode` is `replace`, the line to replace the content with. * @param tag Used to update and remove merges * @param comment Comment style `//` or `#` */ function insertOrReplaceContentInBlock(params) { const { src, newSrc, anchor, tag, comment, blockName, mode } = params; const generatedComment = (0, generateCode_1.createGeneratedComment)(newSrc, tag, comment); if (!src.includes(generatedComment.start)) { // Ensure the old generated contents are removed. const sanitizedTarget = (0, generateCode_1.removeGeneratedContents)(src, tag); const lines = src.split('\n'); const blockStart = lines.indexOf(`${blockName} {`); const blockEnd = lines.indexOf('}', blockStart) + 1; const linesWithoutBlock = lines.slice(blockEnd); let blockArr = lines.slice(blockStart, blockEnd); const blockTrimmed = lines .slice(blockStart, blockEnd) .map(line => line.trim()); const anchorPos = blockTrimmed.indexOf(anchor); const firstHalfOfBlock = blockArr.slice(0, mode === 'insert' ? anchorPos + 1 : anchorPos); const secondHalfOfBlock = blockArr.slice(anchorPos + 1); firstHalfOfBlock.push(generatedComment.start, newSrc, generatedComment.end); blockArr = firstHalfOfBlock.concat(secondHalfOfBlock); const block = blockArr.concat(linesWithoutBlock).join('\n'); return { contents: sanitizedTarget ?? block, didInsertOrReplace: true, didClear: !!sanitizedTarget, }; } return { contents: src, didClear: false, didInsertOrReplace: false }; }