如何在 Windows 10 Powershell 上使用 npm 脚本递归复制整个目录?

How to copy a whole directory recursively with an npm script on Windows 10 Powershell?

如何在 Windows 10 Powershell 上使用 npm 脚本递归复制整个目录?

现在我有以下树:

test
├───1
│       package.json
│
└───2
    └───src
        │   asd.txt
        │
        └───asd
                asd - Copy (2).txt
                asd - Copy.txt
                asd.txt

我想要的是一个脚本,当 运行 在目录 1 时,它会转到目录 2 并从那里递归地复制整个目录 src 到目录 1。所以最后我会在 1 中有一个与 2.

中类似的 src

当我cd到目录1和运行时npm run build:uipackage.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 其中甚至有效。

对于这样的事情,我可能会使用类似于下面的方法。

修改您的 NPM 脚本 (build:ui) 以调用与 package.json 文件位于同一目录中的 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

考虑使用 xcopy command instead of copy,因为它更符合您的要求。

package.json 文件的 scripts 部分重新定义 build:ui 脚本,如下所示:

package.json 的脚本部分:

"scripts": {
  "build:ui": "xcopy /e/h/y/q \"../2/src\" \"./src\\" > nul 2>&1"
}

运行:

当你cd到名为1的目录时,(即包含package.json和前面提到的build:ui script defined in it), 然后 运行:

npm run build:ui

它将生成结果目录结构:

test
├── 1
│   ├── package.json
│   └── src
│       ├── asd
│       │   ├── asd - Copy (2).txt
│       │   ├── asd - Copy.txt
│       │   └── asd.txt
│       └── asd.txt
└── 2
    └── src
        ├── asd
        │   ├── asd - Copy (2).txt
        │   ├── asd - Copy.txt
        │   └── asd.txt
        └── asd.txt

如您所见,文件夹 2 内的 src 文件夹及其所有内容已复制到文件夹 1


解释:

以下提供上述xcopy命令的详细分类:

选项:

  • /e - 复制文件夹和子文件夹,包括空文件夹。
  • /h - 复制隐藏和系统文件和文件夹。
  • /y - 取消确认覆盖文件的提示。
  • /q - 复制时不显示文件名。

备注:

  • 每个路径名都包含在 JSON 转义双引号中,即 \"...\"

  • ./src\部分有一个尾部反斜杠(\),已被JSON转义(\),以通知xcopy 目标是一个目录。这也确保创建 src 目录(如果它尚不存在)。

  • > nul 2>&1 部分禁止显示复制了多少文件的确认日志。


相关信息:

值得注意的是,在 Windows 上,npm 使用 cmd.exe 作为 运行ning npm 脚本的默认 shell - 无论您使用的是哪种 CLI 工具,例如PowerShell。您可以使用 npm-config command to check the script-shell 设置来验证这一点。例如 运行 以下命令:

npm config get script-shell

编辑:

如果您希望生成的目录结构如下所示:

test
├── 1
│   ├── asd
│   │   ├── asd - Copy (2).txt
│   │   ├── asd - Copy.txt
│   │   └── asd.txt
│   ├── asd.txt
│   └── package.json
└── 2
    └── src
        ├── asd
        │   ├── asd - Copy (2).txt
        │   ├── asd - Copy.txt
        │   └── asd.txt
        └── asd.txt

这次名为 2 的文件夹中的 src 文件夹的内容已被复制到文件夹 1 - 但不是实际包含 src 文件夹本身。

然后你需要定义你的 npm 脚本如下:

"scripts": {
  "build:ui": "xcopy /e/h/y/q \"../2/src\" \".\" > nul 2>&1"
}

注意:目标路径已从\"./src\\"更改为\".\"