ディレクトリ構造からファイルを生成する方法

ディレクトリ構造からファイルを生成する方法

次のディレクトリ構造があります。

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

この構造に基づいて、次の内容でindex.jsファイルを生成したいと思います。

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

A4MpFILLED、A4MpOUTLINEDなど、ファイル名の末尾にディレクトリ名が追加されるため、すべてのファイル名は一意です。

Bashを使ってこれを行うにはどうすればよいですか?

次から始めましたが、その後はどのように進むべきかわかりません。

# 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

コードの機能を説明してください。私も学びたい。

ベストアンサー1

ファイル名の前に改行やその他の点がないとします.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

または

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

(ファイル名がソートされました)

おすすめ記事