Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.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
Windows Grunt/Batch:如何在gruntfile.js目录中执行shell命令?_Windows_Gruntjs_Dos_Grunt Shell - Fatal编程技术网

Windows Grunt/Batch:如何在gruntfile.js目录中执行shell命令?

Windows Grunt/Batch:如何在gruntfile.js目录中执行shell命令?,windows,gruntjs,dos,grunt-shell,Windows,Gruntjs,Dos,Grunt Shell,我知道这是一个常见的问题,但我尝试过的所有答案都不起作用。奇怪的是,一个朋友在他的Windows上尝试了这个脚本,实际上得到了当前目录(包含gruntfile.js的目录)。我试着去看差异,但也没有发现任何差异 module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), shell: { test:

我知道这是一个常见的问题,但我尝试过的所有答案都不起作用。奇怪的是,一个朋友在他的Windows上尝试了这个脚本,实际上得到了当前目录(包含gruntfile.js的目录)。我试着去看差异,但也没有发现任何差异

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        shell: {
            test: {
                command: 'dir'
            }
        }
    });

    grunt.loadNpmTasks('grunt-shell');
};
以下是我得到的:

D:\Websites\AUB>grunt shell:test
Running "shell:test" (shell) task
 Volume in drive D is Data
 Volume Serial Number is 6E7B-40AB

 Directory of D:\Websites

04/22/2015  04:53 PM    <DIR>          .
04/22/2015  04:53 PM    <DIR>          ..
04/20/2015  11:04 AM    <DIR>          .sublime
03/30/2015  12:52 PM    <DIR>          .template
04/24/2015  07:55 AM    <DIR>          AUB
我还试图找到一些有用的东西,但我不知道如何使用它,因为我遇到了错误:

command: 'cd %~dp0 && dir'
返回:

D:\Websites\AUB>grunt shell:test
Running "shell:test" (shell) task
The system cannot find the path specified.
Warning: Command failed: C:\WINDOWS\system32\cmd.exe /s /c "cd %~dp0 && dir"
The system cannot find the path specified.
 Use --force to continue.

Aborted due to warnings.

顺便说一句,我直接从Grunt使用的任何其他软件包(clean、copy、rename、uglify等)都工作得很好,Grunt exec也有同样的行为。

这听起来很奇怪,因为通常所有插件都是相对于Gruntfile工作的:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        cwd: process.cwd(),

        shell: {
            test: {
                command: 'cd "<%= cwd %>";dir'
            }
        }
    });

    grunt.loadNpmTasks('grunt-shell');
};

注意:所有文件路径都是相对于GrunFile的,除非当前 使用grunt.file.setBase或--base更改工作目录 命令行选项

您可以手动检索当前工作:

var cwd process.cwd()

// Or to get a file inside the cwd    

var pathOfGruntfile = require('path').resolve('./Gruntfile.js');
并在Grunfile中使用它:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        cwd: process.cwd(),

        shell: {
            test: {
                command: 'cd "<%= cwd %>";dir'
            }
        }
    });

    grunt.loadNpmTasks('grunt-shell');
};
module.exports=函数(grunt){
grunt.initConfig({
pkg:grunt.file.readJSON('package.json'),
cwd:process.cwd(),
外壳:{
测试:{
命令:“cd”“;dir”
}
}
});
grunt.loadNpmTasks(“grunt-shell”);
};

谢谢@jantimon!我猜我的cmd.exe配置有问题。这个技巧非常有效,并且具有保持多平台友好的优势。