0

I'm watching this series of tutorials here and I can see that the grunt ( not the grunt-cli ) files are stored in the project root.

I see that this is normally the case but wanted to put my files elsewhere.

Is there a way to do this. Obviously I could run the init to create the package.json file in say /grunt and do the install there, but would it be relatively easy to tell it how to interface with my project /root/project.

Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52
cade galt
  • 3,843
  • 8
  • 32
  • 48
  • Simple question to understand your question. Why do you want to put the grunt task outside a project? When you means outside, you mean for example outside of the git repository? Could you add to your question the structure that you would like to have, please? – Louis Barranqueiro Nov 14 '15 at 14:43
  • I think must do a grunt install for each project. I just want one installation in my root directory. – cade galt Nov 23 '15 at 17:47
  • How your team mates will work with you if grunt tasks are not on the git repository? How do you write efficient and clean task if you are writing them for 3 different projects?That is clearly the wrong way man. I recommend you to write grunt tasks for each project. Read this post to know how to organize your tasks : [Organize your grunt tasks](http://stackoverflow.com/questions/33804659/how-to-create-and-organise-config-and-register-grunt-tasks) :) – Louis Barranqueiro Nov 23 '15 at 18:55
  • I only have one project. Plus the two config files that grunt uses I do push to my repo. They are linked in symbolically. – cade galt Nov 23 '15 at 22:05
  • ok, I understand. then what is your problem? – Louis Barranqueiro Nov 23 '15 at 22:08
  • What is the process for installing grunt in my root directory and having it available globally ? – cade galt Nov 23 '15 at 22:10
  • what do you means by globally? I will give you an example as an answer. – Louis Barranqueiro Nov 23 '15 at 22:11
  • I mean I want to be able to run `grunt watch` and have it watch my files no matter where I am ... – cade galt Nov 23 '15 at 22:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95969/discussion-between-louis-barranqueiro-and-cade-galt). – Louis Barranqueiro Nov 23 '15 at 22:28

1 Answers1

2

Here is an potential example of your structure :

main-folder
├── package.json
├── Gruntfile.js
├── grunt
│   ├── config.js
│   ├── config
│   │   │   ├── copy.js
│   │   │   ├── sass.js
│   │   │   ├── sync.js
│   │   │   ├── linkAsset.js
│   │   │   └── uglify.js
│   ├── register
│   │   │   ├── compileAssets.js
│   │   │   ├── linkAssets.js
│   │   │   ├── build.js
│   │   │   └── buildProd.js
├── project-1
│   └── folders and files ...
├── project-2
│   └── folders and files ...
  • package.json : contains all your grunt-plugins dependencies and other useful npm modules
  • Gruntfile.js : load and configure all tasks in grunt/config and grunt/register folder

Read this if you want to have more information to setup this configuration of grunt : How to create and organise config and register grunt tasks

Configuration file :

I recommend you also to use a configuration file (E.g : main-folder/grunt/config.js) file to register some shortcut, variables to make your grunt tasks more dynamic.

Example :

var version = '0.1.0';
var project1Dir = 'project-1';
var project2Dir = 'project-2';

module.exports.version = version;
module.exports.project1Dir = project1Dir;
module.exports.project2Dir = project2Dir;

And import this config in each task with : var config = require('../config');. It will be easy to refactor the code if you rename for example the folder project1.

Run tasks :

Now when you are working in your directory (main-folder/project1or main-folder/project2) and entering your grunt command, use b flag to tell to grunt where is your Gruntfile.js file.

Example :

grunt -b ../ build

You can also configure in the code this behavior. Read Grunt documentation for more information : Grunt CLI - b flag

Community
  • 1
  • 1
Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52