【问题标题】:grunt task copy failing to write the file in the destination咕噜任务副本未能将文件写入目标
【发布时间】:2020-04-17 23:48:10
【问题描述】:

我是 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"]);
};

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

正在加载“Gruntfile.js”任务...确定
+ 复制CSS
运行任务:复制
运行“复制”任务
运行“copy:dist”(复制)任务
验证属性 copy.dist 是否存在于配置中...确定
文件:./wwwroot/lib/html5-reset/assets/css/reset.css -> src
文件:./wwwroot/css/ -> dest
选项:encoding="utf8"、processContent=false、processContentExclude=[]、timestamp=false、mode=false
复制 ./wwwroot/lib/html5-reset/assets/css/reset.css -> src
正在阅读 ./wwwroot/lib/html5-reset/assets/css/reset.css...OK
写入 src...错误
警告:无法写入“src”文件(错误代码:EISDIR)。使用 --force,继续。
完成,但有警告。
进程以代码 0 终止。

感谢您帮助查明问题。

【问题讨论】:

  • 你想复制什么?是否要将名为reset.css 的文件 wwwroot/lib/html5-reset/assets/css/ 目录复制到 ./wwwroot/css/ 目录?还是别的什么?
  • 是的,我正在尝试将 html5-reset css 文件从 lib 文件夹复制到 css 文件夹。

标签: gruntjs grunt-contrib-copy asp.net-core-3.1


【解决方案1】:

nodejs错误EISDIR描述为:

EISDIR(是目录​​):操作需要文件,但给定的路径名​​是目录。

如下所示配置您的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 文件是否存在,然后再继续复制它。

【讨论】:

  • 感谢您的详细解释@RobC。感谢您帮助了解问题的根本原因以及自定义任务 sn-p。正如你所解释的,我尝试了这两种方法,两者都工作正常。
猜你喜欢
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 2015-06-23
  • 2014-06-25
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多