Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gruntjs 如何使用Grunt将文件复制到每个目录?_Gruntjs - Fatal编程技术网

Gruntjs 如何使用Grunt将文件复制到每个目录?

Gruntjs 如何使用Grunt将文件复制到每个目录?,gruntjs,Gruntjs,因此,我正在构建一个WP插件,通常会将空index.html文件放入每个文件夹中,以防止在主机允许的地方列出目录。我正在用grunt构建部署就绪包,但我唯一缺少的是这些文件。我有很多文件夹,不想手动创建这些文件。我很乐意创建一个,并让Grunt将该文件复制到每个路径。但是怎么做呢?不需要额外的grunt插件。使用Grunt的内置功能可以满足您的需求 考虑根据下面显示的名为createEmptyHtmlFiles的文件向grunfile.js添加一个 grunfile.js module.expo

因此,我正在构建一个WP插件,通常会将空index.html文件放入每个文件夹中,以防止在主机允许的地方列出目录。我正在用grunt构建部署就绪包,但我唯一缺少的是这些文件。我有很多文件夹,不想手动创建这些文件。我很乐意创建一个,并让Grunt将该文件复制到每个路径。但是怎么做呢?

不需要额外的grunt插件。使用Grunt的内置功能可以满足您的需求

考虑根据下面显示的名为
createEmptyHtmlFiles
的文件向
grunfile.js
添加一个

grunfile.js

module.exports=函数(grunt){
grunt.initConfig({
// ...
});
/**
*自定义任务,在所有文件夹中创建空的`index.html`文件。
*/
registerTask('CreateEmptyHTML文件',函数(){
var fileName='index.html',
内容='';
expand({filter:'isDirectory'},'dist/***'))
.forEach(函数(dirPath){
var htmlFilePath=dirPath+'/'+文件名;
write(htmlFilePath,contents,{encoding:'utf8'})
});
});
registerTask('default',['createEmptyHtmlFiles']);
};
说明:

  • 通常,您的
    Gruntfile.js
    将包括
    grunt.initConfig({…})部分。此部件应保持与当前配置一致

  • 已注册名为
    createEmptyHtmlFiles
    的自定义任务,该任务执行以下操作:

    • 将所需的文件名,即
      index.html
      分配给
      filename
      变量,并将空字符串分配给
      contents
      变量

    • 下一步,我们利用我们传递给的。在上面的示例中,提供的glob是
      'dist/***'
      。globbing模式与
      过滤器:'isDirectory'
      选项相结合,基本上可以获得
      dist
      目录中所有文件夹的路径名

      重要提示:您需要根据目录结构更改此全局模式

    • 接下来,我们使用数组的方法迭代每个目录路径名

    • forEach
      循环的每一轮中,我们都为
      htmlFilePath
      变量指定一个新的路径名,用于创建生成的
      index.html
      文件

    • 每个
      index.html
      文件都是使用创建的

  • 演示:

  • 假设项目目录的结构如下所示:

    。
    ├── Grunfile.js
    ├── 距离
    │   ├── A.
    │   │   ├── B
    │   │   │   └── 1.txt
    │   │   └── C
    │   │       └── 2.txt
    │   ├── D
    │   │   ├── 3.txt
    │   │   └── E
    │   │       └── 4.txt
    │   └── F
    │       └── G
    │           └── 5.txt
    ├── 节点单元
    │   └── ...
    └── package.json
    
  • 在运行
    $grunt
    后,给定上面的
    grunfile.js
    ,它将更改为以下内容:

    。
    ├── Grunfile.js
    ├── 距离
    │   ├── A.
    │   │   ├── B
    │   │   │   ├── 1.txt
    │   │   │   └── index.html