咕噜任务副本无法将文件写入目标

grunt task copy failing to write the file in the destination

我是 gruntjs 的新手,很难理解为什么简单的复制文件任务不起作用。下面是我在 Gruntfile.js.

中的代码
module.exports = function (grunt) {
    "use strict";

    // Project configuration
    grunt.initConfig({
        pkg: grunt.file.readJSON("package.json"),

        // copy other css files
        copy: {
            dist: {
                files: {
                    //expand: true, // required when using cwd
                    //cwd: ['.'], // set working folder / root to copy
                    src: './wwwroot/lib/html5-reset/assets/css/reset.css', // copy all files and subfolders
                    dest: './wwwroot/css/' // destination folder
                }
            }
        }
    });

    grunt.loadNpmTasks("grunt-contrib-copy");

    grunt.registerTask("copyCss", ["copy"]);
};

当我执行任务时出现以下错误

Loading "Gruntfile.js" tasks...OK
+ copyCss
Running tasks: copy
Running "copy" task
Running "copy:dist" (copy) task
Verifying property copy.dist exists in config...OK
Files: ./wwwroot/lib/html5-reset/assets/css/reset.css -> src
Files: ./wwwroot/css/ -> dest
Options: encoding="utf8", processContent=false, processContentExclude=[], timestamp=false, mode=false
Copying ./wwwroot/lib/html5-reset/assets/css/reset.css -> src
Reading ./wwwroot/lib/html5-reset/assets/css/reset.css...OK
Writing src...ERROR
Warning: Unable to write "src" file (Error code: EISDIR). Used --force, continuing.
Done, but with warnings.
Process terminated with code 0.

感谢您帮助查明问题。

nodejs错误EISDIR描述为:

EISDIR (Is a directory): An operation expected a file, but the given pathname was a directory.

改为如下所示配置您的 Gruntfile.js

Gruntfile.js

module.exports = function (grunt) {
  "use strict";

  // Project configuration
  grunt.initConfig({
    pkg: grunt.file.readJSON("package.json"),

    copy: {
      dist: {
        expand: true,
        cwd: 'wwwroot/lib/html5-reset/assets/css',
        src: 'reset.css',
        dest: 'wwwroot/css'
      }
    }
  });

  grunt.loadNpmTasks("grunt-contrib-copy");
  grunt.registerTask("copyCss", ["copy"]);
};

运行 以下命令:

grunt copyCss

会将名为 reset.css 的文件从 wwwroot/lib/html5-reset/assets/css/ 目录复制到 wwwroot/css/ 目录。

之前的例子:

.
├── Gruntfile.js
├── node_modules
│   └── ...
├── package.json
└── wwwroot
    └── lib
        └── html5-reset
            └── assets
                └── css
                    └── reset.css   <--------------

之后的示例:

.
├── Gruntfile.js
├── node_modules
│   └── ...
├── package.json
└── wwwroot
    ├── css
    │   └── reset.css               <--------------
    └── lib
        └── html5-reset
            └── assets
                └── css
                    └── reset.css   <--------------

编辑:创建自定义任务而不是使用 grunt-contrib-copy

如果您只想复制一个文件,那么使用 grunt-contrib-copy 似乎有点不必要。考虑改为创建 custom Task。例如:

Gruntfile.js

module.exports = function (grunt) {
  "use strict";

  grunt.initConfig({
    pkg: grunt.file.readJSON("package.json")
  });

  grunt.registerTask('copyCss', 'Copies a single file', function() {
    grunt.file.copy(
      'wwwroot/lib/html5-reset/assets/css/reset.css',
      'wwwroot/css/reset.css'
    )
  });

};
  • 上述自定义任务利用了 grunt 的内置 grunt.file.copy 方法。

  • 您可能还想考虑向该自定义任务添加一些错误处理。例如,您可能想利用 grunt 的内置 grunt.file.exists 方法在继续复制之前检查 reset.css 文件是否存在。