【问题标题】:Problems copying many folders复制多个文件夹时出现问题
【发布时间】:2015-08-30 05:31:46
【问题描述】:

我对 grunt 很陌生,所以我需要一些帮助。

我在官方gruntjs/grunt-contrib-copy上问了这个问题,但没有得到回应。

我想将使用 grunt-contrib-copy 的文件从某个目录复制到另一个目录,它们看起来像这样:

parentpath/ (source file)
    /assets/sass/bootstrap/
    /assets/sass/fontawesome/
    /assets/sass/storms/
    /assets/sass/wordpress/
    /assets/sass/_layout.scss
    /assets/sass/_woocommerce.scss

childpath/ (destiny file)
    /assets/sass/bootstrap/
    /assets/sass/fontawesome/
    /assets/sass/storms/
    /assets/sass/wordpress/
    /assets/sass/_layout.scss
    /assets/sass/_woocommerce.scss

我的 gruntfile 是这样配置的:

copy: {
    parenttheme_bootstrap: {
        src: '<%= dirs.parentpath %>/assets/sass/bootstrap',
        dest: '<%= dirs.sass %>'
    },
    parenttheme_fontawesome: {
        src: '<%= dirs.parentpath %>/assets/sass/fontawesome',
        dest: '<%= dirs.sass %>'
    },
    parenttheme_storms: {
        src: '<%= dirs.parentpath %>/assets/sass/storms',
        dest: '<%= dirs.sass %>'
    },
    parenttheme_wordpress: {
        src: '<%= dirs.parentpath %>/assets/sass/wordpress',
        dest: '<%= dirs.sass %>'
    },
    parenttheme_partials: {
        files: [{
                expand: true,
                cwd: '<%= dirs.parentpath %>/assets/sass/',
                src: '_layout.scss',
                dest: '<%= dirs.sass %>'
            },
            {
                expand: true,
                cwd: '<%= dirs.parentpath %>/assets/sass/',
                src: '_woocommerce.scss',
                dest: '<%= dirs.sass %>'
        }]
    }
}

// parent Task
grunt.registerTask( 'parent', [
    'copy:parenttheme_bootstrap',
    'copy:parenttheme_fontawesome',
    'copy:parenttheme_storms',
    'copy:parenttheme_wordpress',
    'copy:parenttheme_partials',
    'sass',
    'postcss'
] );

当我运行父任务时,/assets/sass/bootstrap 文件夹被正确复制到他的命运中。部分文件也是如此。

但由于某种原因,所有其他文件夹都没有被复制,我没有收到任何错误!

运行 grunt parent --verbose 我明白了:

Running tasks: parent

Running "parent" task

Running "copy:parenttheme_bootstrap" (copy) task
Verifying property copy.parenttheme_bootstrap exists in config...OK
Files: ../../childpath/assets/sass/bootstrap -> ../assets/sass
Options: encoding="utf8", processContent=false, processContentExclude=[], timestamp=false, mode=false
Creating ../assets/sass
Created 1 directory

Running "copy:parenttheme_fontawesome" (copy) task
Verifying property copy.parenttheme_fontawesome exists in config...OK
Files: ../../childpath/assets/sass/fontawesome -> ../assets/sass
Options: encoding="utf8", processContent=false, processContentExclude=[], timestamp=false, mode=false
Creating ../assets/sass
Created 1 directory

Running "copy:parenttheme_storms" (copy) task
Verifying property copy.parenttheme_storms exists in config...OK
Files: ../../childpath/assets/sass/storms -> ../assets/sass
Options: encoding="utf8", processContent=false, processContentExclude=[], timestamp=false, mode=false
Creating ../assets/sass
Created 1 directory

Running "copy:parenttheme_wordpress" (copy) task
Verifying property copy.parenttheme_wordpress exists in config...OK
Files: ../../childpath/assets/sass/wordpress -> ../assets/sass
Options: encoding="utf8", processContent=false, processContentExclude=[], timestamp=false, mode=false
Creating ../assets/sass
Created 1 directory

Running "copy:parenttheme_partials" (copy) task
Verifying property copy.parenttheme_partials exists in config...OK
Files: ../../childpath/assets/sass/_layout.scss -> ../assets/sass/_layout.scss
Files: ../../childpath/assets/sass/_woocommerce.scss -> ../assets/sass/_woocommerce.scss
Options: encoding="utf8", processContent=false, processContentExclude=[], timestamp=false, mode=false
Copying ../../childpath/assets/sass/_layout.scss -> ../assets/sass/_layout.scss
Reading ../../childpath/assets/sass/_layout.scss...OK
Writing ../assets/sass/_layout.scss...OK
Copying ../../childpath/assets/sass/_woocommerce.scss -> ../assets/sass/_woocommerce.scss
Reading ../../childpath/assets/sass/_woocommerce.scss...OK
Writing ../assets/sass/_woocommerce.scss...OK
Copied 2 files

有人知道我在这里做错了什么吗?

谢谢!

【问题讨论】:

    标签: gruntjs grunt-contrib-copy


    【解决方案1】:

    我认为您只需要稍微试验一下您的目录和文件规范。有几件事可以尝试:

    1. 向源路径添加通配模式,而不仅仅是目录名称,例如 .../bootstrap/**/*.scss。
    2. 在目标路径中添加一个尾部斜杠,使其清楚地是一个目录,例如dest: '&lt;%= dirs.sass %&gt;/'。 grunt 可能正在复制到单个文件输出,而不是目录树。

    一个小例子:

    copy: {
        parenttheme_bootstrap: {
            src: '<%= dirs.parentpath %>/assets/sass/bootstrap/**/*.scss',
            dest: '<%= dirs.sass %>/'
        },
        parenttheme_fontawesome: {
            src: '<%= dirs.parentpath %>/assets/sass/fontawesome/**/*.scss',
            dest: '<%= dirs.sass %>/'
        },
    

    【讨论】:

    • 嗨,@詹姆斯! Sry 这么久才回答。我试试你的解决方案,它有效!但是文件被复制到childpath/parentpath/assets/... 而不是childpath/assets/...。我改变了你的建议,我定义的目录是sass: '../assets/sass' 和parentpath: '../../parentpath'。有什么想法吗?
    • 有道理,输出路径将相对于 cwd 参数。
    【解决方案2】:

    感谢@James 的建议,我通过改成这个来完成这项工作:

            sf2_bootstrap: {
                files: [{
                    expand: true,
                    cwd: '<%= dirs.sfpath %>/assets/sass/bootstrap/',
                    src: '**/*.scss',
                    dest: '<%= dirs.sass %>/bootstrap/'
                }]
            },
    

    现在我的文件已正确复制。

    James的解决办法,把我所有的文件都复制了,但是到了错误的目录,不知道为什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2021-01-20
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多