选择带有递归glob for babel命令的文件

问题描述 投票:0回答:1

给定以下文件夹结构:

.
└── dogs
    ├── index.js
    └── cats
        ├── index.js
        └── racoons
            ├── index.js
            └── walrus
                └── index.js

我想运行babel-cli命令babel并生成对应于每个index.js的文件作为index.build.js

babel命令接收文件或文件夹:

babel index.js --out-file index.build.js

我想知道它可以采取递归glob。

node.js unix babel
1个回答
0
投票

您可以使用以下命令处理当前目录(和子目录)中的所有index.js文件:

babel . --only index.js -d /output/directory

这将复制/output/directory中当前目录的子文件夹结构,并将编译后的文件放在那里。

此外,可以将处理委托给bash并在每个文件上独立执行babel:

function processFile(){
    local fPath=$1
    local fDir=$(dirname $fPath)
    local fName=$(basename $fPath)

    pushd "$fDir"
    babel "$fName" -o "${fName%.js}.build.js"
    popd
}
export -f processFile

find . -type f -name "index.js" -exec bash -c 'processFile "$0"' {} \;
© www.soinside.com 2019 - 2024. All rights reserved.