Heroku - npm postinstall script to run grunt task depending on enviro

I have two heroku node.js applications, one for prod and one for dev, and I also have a Gruntfile with dev- and prod-specific tasks. I know that you can configure package.json to run grunt as a postinstall hook for npm, but can you specify any different tasks to run depending on which environment you are in?

Here is what the relevant section of my package.json looks like:

"scripts": {
    "postinstall": "./node_modules/grunt/bin/grunt default"
},

Instead of running grunt default each time, I would like to run "grunt production" if NODE_ENV is production, etc.

Is it possible?

+3
source share
2 answers

- dev "development" if. Heroku, : " slug. , , " user-env-compile ", . : http://devcenter.heroku.com/articles/labs-user-env-compile". . , , buildkack heroku-buildpack-nodejs-grunt, : grunt.

+2

, , postInstall postInstallDev. script, . , :

"scripts": { "postinstall": "node postInstall.js" },

script Grunt:

// postInstall.js
var env = process.env.NODE_ENV;

if (env === 'development') {
    // Spawn a process or require the Gruntfile directly for the default task.
    return;
}

if (env === 'production') {
    // Spawn a process or require the Gruntfile directly to the prod task.
    return;
}

console.error('No task for environment:', env);
process.exit(1);

...

  • Grunt co. dependencies. devDependencies, . script Node, , . postInstall script, , git ( ).
  • ./node_modules/grunt/bin/grunt default. grunt-cli dependency devDependency, npm , , grunt default .
+6

All Articles