@@ -273,13 +273,95 @@ const processRelativeComponentRef = function (inMatch, splitSep, compLocMap) {
273273 return retString ;
274274} ;
275275
276+ /** This function finds the number of levels we need to traverse up in the middlePath
277+ to get to the correct relative path to the component
278+ *
279+ * @param {* } inMatch import statement that we're processing
280+ * @param {* } splitSep split separator we're using
281+ * @returns {number } number of levels we need to traverse up in the middlePath
282+ */
283+ const findLevels = function ( inMatch , splitSep ) {
284+ const levels = inMatch . split ( splitSep ) . length - 1 ; // number of occurrences of splitSep in inMatch
285+ return levels ;
286+ } ;
287+
288+ /** This function gets the middle part of the filePath that is between 'lib/' and the last '/'
289+ *
290+ * @param {* } filePath path of the file being processed
291+ * @returns {string } middle part of the filePath
292+ */
293+ const getMiddlePath = function ( filePath ) {
294+ const libIndex = filePath . indexOf ( 'lib' ) ;
295+ const trailingSlashIndex = filePath . lastIndexOf ( '/' ) ;
296+ return filePath . slice ( libIndex + 4 , trailingSlashIndex ) ;
297+ } ;
298+
299+ /** This function traverses the middlePath by the number of levels we need to traverse up
300+ * to get to the correct relative path to the component
301+ * @param {* } middlePath middle part of the filePath that is between 'lib/' and the last '/'
302+ * @param {* } levels number of levels we need to traverse up in the middlePath
303+ * @returns {string } middlePath after traversing up the number of levels
304+ */
305+ const getTraversedMiddlePath = function ( middlePath , levels ) {
306+ while ( levels ) {
307+ const startingIndex = 0 ;
308+ const lastTrailingSlashIndex = middlePath . lastIndexOf ( '/' ) ;
309+ middlePath = middlePath . slice ( startingIndex , lastTrailingSlashIndex ) ;
310+ levels -- ;
311+ }
312+ return middlePath ;
313+ } ;
314+
315+ /** This function is used to process relative paths that are within the same component sub-directory
316+ middlePath is the part of the filePath that is between 'lib/' and the last '/'
317+ ex: if filePath is 'packages/react-sdk-overrides/lib/components/designSystemExtension/FieldValueList.js'
318+ then middlePath will be 'components/designSystemExtension'
319+
320+ @param {* } filePath path of the file being processed
321+ @param {* } inMatch import statement that we're processing
322+ @returns {string } string that should replace inMatch (with relative path replacements)
323+ */
324+
325+ const processRelativePathToSameCompSubDir = ( filePath , inMatch ) => {
326+ let middlePath = getMiddlePath ( filePath ) ;
327+
328+ const splitWith = '../' ;
329+ // levels is the number of '../' in the inMatch string that we need to traverse up
330+ // to get to the correct relative path to the component
331+ // ex: if inMatch is "import FieldValueList from '../../designSystemExtension/FieldValueList';"
332+ // then levels will be 2 because there are 2 occurrences of '../' in the inMatch string
333+ // and we need to traverse up 2 levels in the middlePath
334+ let levels = findLevels ( inMatch , splitWith ) ;
335+ let replacementString ;
336+ // initialPartOfImport is the part of the import statement before the relative path
337+ // ex: if inMatch is "import FieldValueList from '../../designSystemExtension/FieldValueList';"
338+ // then initialPartOfImport will be "import FieldValueList from "
339+ // NOTE: we add 1 to the indexOf to include the single quote at the end of the import statement
340+ const initialPartOfImport = inMatch . slice ( 0 , inMatch . indexOf ( "'" ) + 1 ) ;
341+
342+ // We need to traverse up the middlePath by the number of levels we have in the inMatch string
343+ // ex: if levels is 2, then we need to traverse up 2 levels in the middlePath
344+ middlePath = getTraversedMiddlePath ( middlePath , levels ) ;
345+
346+ // tailPartOfImport is the part of the import statement after the relative path
347+ // ex: if inMatch is "import FieldValueList from '../../designSystemExtension/FieldValueList';"
348+ // then tailPartOfImport will be "/FieldValueList';"
349+ // NOTE: we add 2 to the lastIndexOf to include the single quote at the end of the import statement
350+ // and the semicolon at the end of the import statement
351+ // (the semicolon is not included in the inMatch string, but we need it for the replacement)
352+ const tailPartOfImport = inMatch . slice ( inMatch . lastIndexOf ( '../' ) + 2 ) ;
353+
354+ replacementString = initialPartOfImport + relativePathReplacementComponents + middlePath + tailPartOfImport ;
355+ return replacementString ;
356+ } ;
357+
276358/**
277359 * processImportLine
278360 *
279361 * @param {* } inMatch - should be a line starting with import
280362 * @returns string that inMatch should be replaced with (possible import path updates)
281363 */
282- const processImportLine = function ( inMatch ) {
364+ const processImportLine = function ( inMatch , filePath ) {
283365 // console.log(` in processImportLine: |${inMatch}|`);
284366
285367 let retString = inMatch ; // default to return incoming matched string
@@ -308,7 +390,7 @@ const processImportLine = function (inMatch) {
308390 return replacementString ;
309391 }
310392
311- // 3. If inMatch does contain '../' followed by sdkHooksDir then process as relative bridge reference
393+ // 3. If inMatch does contain '../' followed by sdkHooksDir then process as relative hooks reference
312394 if ( hasRelativeDir ( inMatch , splitWith , overrideConstants . SDK_HOOKS_DIR ) ) {
313395 const replacementString = processRelativeRef ( inMatch , splitWith , overrideConstants . SDK_HOOKS_DIR ) ;
314396 console . log ( ` --> replacing with: ${ replacementString } ` ) ;
@@ -352,6 +434,20 @@ const processImportLine = function (inMatch) {
352434 return replacementString ;
353435 }
354436
437+ // 8. This should handle any relative path within the same components sub-directory
438+ if (
439+ inMatch . includes ( splitWith ) &&
440+ overrideConstants . SDK_COMP_SUBDIRS . some ( dir => {
441+ console . log ( ` checking dir: ${ dir } ` ) ;
442+ return filePath . includes ( dir ) ;
443+ } )
444+ ) {
445+ const replacementString = processRelativePathToSameCompSubDir ( filePath , inMatch ) ;
446+ console . log ( ` --> replacing with: ${ replacementString } ` ) ;
447+ iPathReplacements = iPathReplacements + 1 ;
448+ return replacementString ;
449+ }
450+
355451 // more work to do
356452 console . log ( ` ----> More work to do: ${ inMatch } ` ) ;
357453 iMayNeedPathReplacement = iMayNeedPathReplacement + 1 ;
@@ -379,7 +475,7 @@ const processOverrideFile = function (filePath) {
379475 const options = {
380476 files : filePath ,
381477 from : / ^ i m p o r t .+ / gm,
382- to : match => processImportLine ( match ) ,
478+ to : match => processImportLine ( match , filePath ) ,
383479 countMatches : true
384480 } ;
385481
0 commit comments