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.
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句