【问题标题】:Node module to execute tasks grunt : "Gruntfile.js" not find执行任务 grunt 的节点模块:找不到“Gruntfile.js”
【发布时间】:2015-07-25 03:31:13
【问题描述】:

我目前正在尝试开发一个允许节点从命令行运行 Grunt 任务的模块。这个 Node 模块是全局安装的:

C:\Users\pcharpin\AppData\Roaming\npm\node_modules\task-app

目标是“Grunt”命令的使用对用户是透明的。为了更好地解释我的方法,一个使用我的节点模块的简单示例:

C:\Users\pcharpin\Desktop\demo-app> task-app copy

在本例中,我的模块会将源目录复制到目标目录。

不幸的是,当我运行任务 Grunt 时,我的节点模块向我显示目录“demo-app”中没有文件“Gruntfile.js”。但是,我的 Node 模块应该可以在其自己的目录中找到此文件。

我的树节点模块:

  • 任务应用
  • node_modules
    • 咕噜声
    • task-app.js
  • Gruntfile.js
  • package.json
  • README.md
  • ...

我的 task-app.js 文件,代码如下:

#!/Usr/bin/env node

var grunt = require('grunt');

var args = process.argv.splice(2)

checkArguments(args);

checkArguments function(args) {
    [...]
    runCopy();
}

runCopy = function() {
   var spawn = require('child_process') spawn.
   var exec = spawn('cmd', ['/ c', 'grunt copy']);
   exec.stdout.on("data", function (data) {
     console.log('' + data);
   });
   exec.stderr.on('data', function (data) {
     console.log('' + data);
   });
}

然后在我的“Gruntfile.js”文件中,我有将源代码复制到目标目录的代码:

module.exports = function(grunt) {

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

   grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    copy {
     srcWeb {
      files: [{
        cwd: '<%= pkg.srcWeb%>', //root to copy
        src: '**/*', // copy all files and subfolders
        dest: '<%= pkg.name%>/www/', // destination folder
        expand: true // When required using cwd
     }]
   }
 }
});

我不明白为什么在“Gruntfile.js”文件中找不到 Node 模块。

这个项目目录中是否需要有一个文件“Gruntfile.js”?还有其他解决方案吗?

我也想知道,文件“Gruntfile.js”是否可以读取项目目录中的文件“package.json”?这是为了允许用户配置此文件以更改源路径。

编辑:

经过一些研究,我越来越接近我的问题的解决方案。要更改默认为当前目录的“Gruntfile.js”的执行,我们可以在命令行中使用 --gruntfile 选项。

如源代码 grunt (grunt/lib/grunt/tasks.js) 所示:

//Get any local Gruntfile or tasks that might exist. Use --gruntfile   override
//if specified, otherwise search the current directory or any parent.

如何为任何 Windows 用户指定“Gruntfile.js”(在我全局安装的节点模块中找到)的路径?

这是一个例子:

C:\Users\username\projects\demo-app> 任务应用复制

在我的源代码中,我使用 --gruntfile 选项执行这样的 grunt 任务:

runCopy = function() {
  var spawn = require('child_process').spawn;
  var pathGruntfile = 'C:\Users\username\AppData\Roaming\npm\node_module\task-app';
  var exec = spawn('cmd', ['/ c', 'grunt --gruntfile' + pathGruntfile + ' Gruntfile.js']);
  exec.stdout.on("data", function (data) {
    console.log('' + data);
  });
  exec.stderr.on('data', function (data) {
    console.log('' + data);
 });
}

编辑 2:

等待回复,我找到了一个临时解决方案,可以在我的 Node 模块中获取“Gruntfiles.js”。但缺点是在当前目录中本地安装所有Node模块依赖项和我的Node Module。

var pathDirCurrent = process.cwd();
var pathGruntfile = pathDirCurrent.concat('\\node_modules\\task-app\\Gruntfile.js');    
var spawn = require('child_process').spawn;
var exec = spawn('cmd', ['/c', 'grunt --gruntfile ' + pathGruntfile + ' create']);
exec.stdout.on("data", function(data) {
    console.log('' + data);
});

【问题讨论】:

    标签: javascript node.js command-line gruntjs


    【解决方案1】:

    最后,我了解了使用 Grunt 任务的逻辑。当你有一个需要使用任务 grunt 的 web 项目应用程序时,它必须在当前目录中有 Gruntfile.js。

    但是,就像我在“编辑 2”中所说的那样,我们可以使用 ---gruntfile 选项指定 Gruntfile.js。因此,这不是一个缺点,因为 Grunt 工具可以与您的 Web 项目应用程序所在的当前目录中的 Gruntfile.js 一起使用。

    对于每个任务 grunt,我使用 --gruntfile 选项指定 Gruntfile.js 的路径,作为“编辑 2”中的示例。

    【讨论】:

      【解决方案2】:

      你从哪里发出咕噜声?

      您的项目结构和 grunt 文件看起来不错。也许尝试包含一个基本任务:

       grunt.registerTask('default', ['copy']);
      

      【讨论】:

      • 我正在从全局安装的节点模块运行 grunt。我忘了解释“grunt 模块”包含在我的节点模块“task-app”的“node_modules”中。
      • 当您从正确的文件夹(与 GruntFile.js 相同的级别)运行 grunt 时,这是否有效?我的猜测是,即使您的 grunt 是全球性的,它也不知道要运行哪个“grunt”。 c:\myapp\ --> \grunt
      • 是的,当我从 Gruntfile.js 所在的文件夹(节点模块任务应用程序目录)运行 grunt 时,它可以工作。但是,我试图从我的项目目录中运行 grunt,它不起作用。但是,我从我的项目目录安装了本地 grunt 并且我有相同的消息:致命错误:无法找到 Gruntfile。唯一的解决方案是把这个 Gruntfile.js 放在项目目录中?
      猜你喜欢
      • 1970-01-01
      • 2016-03-02
      • 2015-07-28
      • 2014-01-30
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多