如何在Windows 10 Powershell上使用npm脚本递归地复制整个目录?
现在我有一棵树:
test
├───1
│ package.json
│
└───2
└───src
│ asd.txt
│
└───asd
asd - Copy (2).txt
asd - Copy.txt
asd.txt
我想要的是一个脚本,当在dir 1
中运行时,它会转到dir 2
并递归地将整个dir src
复制到dir 1
。因此,最终,我将在1
中有一个类似的1
,就像在2
中一样。
当我cd
到目录1
并运行npm run build:ui
(在package.json
中定义为
"scripts": {
"build:ui": "cd ..\\2 && copy src ..\\1"
}
它开始做我想做的事情,但不完全是这样;它将内容从目录2
复制到1
。问题是它没有复制整个目录及其所有子目录和所有可能的内容,而是直接从2/src/
内部复制文件。换句话说,下面是操作后树的样子:
test
├───1
│ asd.txt
│ package.json
│
└───2
└───src
│ asd.txt
│
└───asd
asd - Copy (2).txt
asd - Copy.txt
asd.txt
所以只有asd.txt
文件被复制了。
我尝试过的其他配置没有成功,包括:
"scripts": {
"build:ui": "cd ..\\2 && copy -r src ..\\1"
}
"scripts": {
"build:ui": "cd ..\\2 && Copy-Item -Recursive src ..\\1"
}
"scripts": {
"build:ui": "cd ..\\2 && cp -r src ..\\1"
}
其...none甚至是有效的。
发布于 2020-05-01 04:01:06
对于类似的,我可能使用类似于下面的.的方法。
修改NPM脚本(build:ui
)以调用位于与package.json文件相同的dir中的Powershell脚本(build.ui.ps1
)。
"scripts": {
"build:ui": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./build.ui.ps1"
},
使用以下内容创建上述Powershell脚本。
param(
$srcParentDir = '2',
$srcDir = 'src',
$srcDestDir = '1'
)
Set-Location (get-item $PSScriptRoot).parent.FullName
Copy-Item -Path "$srcParentDir\$srcDir" -Destination $srcDestDir -Recurse
运行npm脚本
npm run build:ui
https://stackoverflow.com/questions/61539374
复制