如何运行多个grunt脚本.postinstall?

23

我试图从grunt的scripts.postinstall运行多个CLI命令。我无法弄清如何使两个命令都运行。如果我添加第二个命令,两个命令都不会运行。单独地,在postinstall和控制台中,它们都能正常工作。

我尝试将它们包装在一个数组中:

"scripts": {
    "postinstall": ["node_modules/.bin/bower install", "grunt setup"]
},

我试着用分号将它们分开:

  "scripts": {
    "postinstall": "node_modules/.bin/bower install; grunt setup"
  },

我似乎在NPM Scripts找不到解决方法。

这些部分的gruntfile.js看起来像这样:

mkdir: {
    setup: {
        options: {
            create: [
                'app/main/source/www', 'app/main/build', 'app/main/docs', 'app/main/tests',
                'app/development',
                'app/releases'
            ]
        }
    }
}

grunt.registerTask('setup', [
    'mkdir:setup',
    'bowercopy:wordpress'
]);

如果有帮助的话,这是我裁剪过的 package.json 的简化版本,其中我删除了上面的代码示例,主要是为了提供背景信息。

{
  "name": "webapp",
  "version": "0.1.0",
  "description": "A web app using bower and grunt",
  "main": "gruntfile.js",
  "scripts": {
    "postinstall": "node_modules/.bin/bower install"
  },
  "repository": {
    "type": "git",
    "url": "someurl.com"
  },
  "keywords": [
    "web", "app"
  ],
  "author": {
    "company": "somecompany",
    "name": "somename",
    "email": "email@me.com"
  },
  "license": "MIT",
  "homepage": "https://someurl.com",
  "bugs": {
    "url": "someurl.com"
  },
  "devDependencies": {
    "grunt": "^0.4.5",
    "bower" : "~1.3.5",
    etc
  }
}
2个回答

78
您可以在npm脚本部分使用“&&”来运行多个命令。

您可以在npm脚本部分使用“&&”来运行多个命令。

"scripts": {
    "postinstall": "bower install && grunt setup"
},

你不能在Windows上做这件事。 - eugenekgn
1
我主要使用Windows并且以前使用过这个技巧。它可以运行。 - kenwarner
@kobe 是的,应该没问题。&&是标准的Shell脚本运算符,不是特定于Node的。 - kenwarner

11

你可以尝试编写一个Bash脚本,执行这两个命令,并运行该脚本。

post_install.sh:

#!/bin/bash
node_modules/.bin/bower install
grunt setup

package.json:

"scripts": {
    "postinstall": "./post_install.sh"
  },

嗨,Chris,起初它似乎可以工作,但是似乎在某个地方失败了,因为bower install没有拉取依赖项,而grunt setup也没有创建项目文件层次结构。不过它似乎确实运行了postinstall???你会如何调试这个问题?似乎没有任何错误出现。 - mtpultz
1
抱歉,Chris,非常好用。由于某种原因,我默认使用我的文本编辑器打开.sh文件。因此,它会“运行”post_install.sh,但实际上它只是尝试重新打开我已经在sublime中打开的内容。 - mtpultz
我遇到了权限错误,顺便说一下,我不能使用sudo,因为我将在Docker中工作。 - Rafee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接