0

I have the following directory structure:

lib- 
    |-filled
            |-A4MpFILLED.svelte
            |-A5KFILLED.svelte
            |- // more files
    |-outlined
            |-A4MpOUTLINED.svelte
            |-A5KOUTLINED.svelte
            |- // more files
    |-round
            |-A4MpROUND.svelte
            |-A5KROUND.svelte
            |- // more files
    |-sharp 
            |-Add_cardSHARP.svelte
            |-Add_homeSHARP.svelte
            |- // more files

From this structure, I'd like to create a index.js file with the following content.

export { default as A4MpFILLED } from './filled/A4MpFILLED.svelte';
export { default as A5KFILLED } from './filled/A5KFILLED.svelte';
// more lines
export { default as A4MpOUTLINED } from './outlined/A4MpOUTLINED.svelte';
export { default as A5KOUTLINED } from './outlined/A5KOUTLINED.svelte';
// more lines
export { default as A4MpROUND } from './round/A4MpROUND.svelte';
export { default as A5KROUND } from './round/A5KROUND.svelte';
// more lines
export { default as Add_cardSHARP } from './sharp/Add_cardSHARP.svelte';
export { default as Add_homeSHARP } from './sharp/Add_homeSHARP.svelte';
// more lines

All file names are unique since added the directory name at the end of file names, like A4MpFILLED, A4MpOUTLINED, etc.

How can I do this using bash?

I started with the following but I'm not sure how to proceed after this.

# list file names
find . -type f '(' -name '*.svelte' ')' > index1
# remove ./ from each line
sed 's|.*/||' index1 > index2
# create a names.txt
sed 's|.svelte||' index2 > names.txt

Please explain what your code does. I'd like to learn as well.

2
  • Is it possible to have, for instance A5KOUTLINED.svelte in multiple directories?
    – doneal24
    Commented May 27, 2022 at 20:46
  • I just updated the question. All the file names are unique. I updated the dir structure.
    – shin
    Commented May 27, 2022 at 20:51

1 Answer 1

2

Assuming your filenames have not newlines or more dots before .svelte:

cd lib && find . -type f -name '*.svelte' | sort | awk -F'[/.]' '{
    print "export { default as " $(NF-1) " } from \047" $0 "\047;"
}' > my/path/to/index.js

or

cd lib && printf '%s\n' ./*/*.svelte | awk -F'[/.]' '{
    print "export { default as " $(NF-1) " } from \047" $0 "\047;"
}' > my/path/to/index.js

(filenames are already sorted)

1
  • I have no idea where you use these files and what their syntax is. For example I wonder how is the syntax for a filename with spaces? export { default as 'some file' } maybe? Obviously we don't cover cases like this or more specific to your usage. You have to ensure on your own it's ok to do this with simple text processing.
    – thanasisp
    Commented May 27, 2022 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .