Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpillora/grunt-source
Reuse a Grunt environment across multiple projects
https://github.com/jpillora/grunt-source
Last synced: 3 months ago
JSON representation
Reuse a Grunt environment across multiple projects
- Host: GitHub
- URL: https://github.com/jpillora/grunt-source
- Owner: jpillora
- Created: 2013-06-10T13:05:14.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-10-03T11:16:29.000Z (over 9 years ago)
- Last Synced: 2024-10-10T10:13:28.531Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 248 KB
- Stars: 18
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Grunt Source
Reuse a Grunt environment across multiple projects
## Usage
* Install Grunt Source
``` sh
npm install -g grunt-source
```* In your project's root, create a `Gruntsource.json` pointing to your Grunt source environment:
``` json
{
"source": "../grunt-source-foo"
}
```
* A Grunt source environment is a directory with a normal `Gruntfile.js` with two slight modifications:* All calls to `grunt.loadNpmTasks()` removed.
* A call to [`grunt.source.loadAllTasks();`](#gruntsourceloadalltasks-function) is added to the top of the `Gruntfile.js` (See the [example](./example)).* Then simply run `grunt-source`
``` sh
grunt-source
```*You can use `grunt-source` just as you would normally use `grunt`. Give `grunt-source --help` a try.*
## Example
For a simple example, please see:
> ### [grunt-source-example](./example)
## Motivation
**The Problem** - Grunt environments can quickly become quite complicated and whenever we need to
reuse a Grunt environment, it requires us to copy the current project and remove the source leaving
just the Grunt related files.**A static solution** - The Grunt team has made [grunt-init](https://github.com/gruntjs/grunt-init)
which gives you a *static* copy of a predefined template. Although `grunt-init` does provide a way to
template in new values for each copy, this is can still be improved.**A dynamic solution** - Enter *Grunt Source*, instead of making numerous **static** copies of a given
Grunt environment, you can actually use one Grunt environment for multiple projects. Also, having a
directory separation between the Grunt environment and the actual source will help to reduce the
complexity of your project. We can still have `grunt-init` like behaviour with `grunt-source`
(e.g. initial placeholder source files) by using the in-built [init task](https://github.com/jpillora/grunt-source#init).When using Grunt Source, projects will now contain only a `Gruntsource.json`, thereby abstracting
the magic of Grunt outside of the project. This will help those who don't need to know the
complexities of the build, yet still want to modify the source.So with Grunt Source, we'll have **one** Grunt Source project which looks like:
```
├── Gruntfile.coffee
├── README.md
├── init
│ └── ...
├── node_modules
│ └── ...
└── package.json
```And then **multiple** projects using this Grunt Source might look like:
```
├── Gruntsource.json
├── css
│ └── app.css
├── index.html
├── js
│ └── app.js
└── src
├── scripts
├── styles
└── views
```This directory structure is for [grunt-source-web](https://github.com/jpillora/grunt-source-web)
to build optimised static websites, ready to be hosted.## API
### Grunt Source Configuration
In your Gruntsource.json file, the following properties are allowed:
* `source` - **required** - the directory where the *source* Grunt environment resides.
* `repo` - the Git repository which will be used to initialise and update the `source`.
* `url[@ref]` where `ref` is a [Git Reference](http://git-scm.com/book/en/Git-Internals-Git-References) (tag or commit hash)
* `config` - an object which will get merged when you call `grunt.initConfig()`, allowing you to override the source Gruntfile configuration.
* `` - any valid JSON value, in your `Gruntfile.js` will be accessible as `grunt.source.`.### Grunt Source Object
In your Gruntfile wrapper function, a `source` object is added to the `grunt` object.
#### `grunt.source.loadAllTasks()` (function)
This function is **very** important, first it loads all of the tasks (npm tasks and
local tasks) in the "source" directory (or the Grunt project directory), then it
changes the working directory *back* to the current directory and loads all local tasks
there. So before the function is called, the current working directory is the `source`
directory. Therefore, in the majority of cases, we'll want to call this function
**at the top** of our Grunt Source `Gruntfile.js`s.Essentially, the above description is the following:
``` js
//cwd is initially set the source directory!//automatically `grunt.loadNpmTasks` all tasks inside the source directory's package.json's 'devDependencies' field
loadGruntTasks(grunt);//load all user defined tasks inside the source directory
grunt.loadTasks("./tasks");//set cwd to project directory (the directory you execute 'grunt-source' from)
process.chdir(PROJECT_DIR);//load all user defined tasks inside the project directory
grunt.loadTasks("./tasks");
````loadGruntTasks` is provided by [load-grunt-tasks](https://github.com/sindresorhus/load-grunt-tasks)
#### `grunt.source.dir` (string)
The absolute path to the source directory
#### `grunt.source.`
All properties defined in your configuration object will also be set on the `grunt.source` object
For example, the `Gruntfile.coffee` in [grunt-source-web](https://github.com/jpillora/grunt-source-web.git), places
the `grunt.source` object in the Jade data option object, so in our `index.jade` file, we can do things like:``` json
{
"source": "~/.grunt-sources/web",
"repo": "https://github.com/jpillora/grunt-source-web.git",
"title": "Hello Grunt Source"
}
`````` jade
!!!5
html
head
title #{source.title}
body
h5 #{source.title}
```## In-built tasks
Before `grunt` is started, the following tasks are registered (`grunt.registerTask`).
Therefore, all Grunt Sources will have the following tasks avaiable (just `init` for now).#### `init`
Although we no longer need to copy and paste our Grunt environments across various projects. It can
still be useful to generate an initial set of source files.`grunt-source init` will copy all files from the source directory's init folder into the current
working directory, however, it will only copy those files that are **missing**.Also, upon clone of the `repo` property, this task will automatically be run.
## Alternative Configuration
Instead of creating a `Gruntsource.json`, you can add a `gruntSource` field to your `package.json` file:
``` json
{
"name": "my-module",
"version": "0.1.3",
"gruntSource": {
"source": "~/.grunt-sources/node",
"repo": "https://github.com/jpillora/grunt-source-node.git"
}
}
```## CLI
The `grunt-source` runs in a similar way to `grunt-cli`, so commands like `grunt-source task2:target4 task3:target1` will work as you expect.
## Examples
See [grunt-source-web](https://github.com/jpillora/grunt-source-web) for
an example Grunt Source, and then see [notifyjs-com](https://github.com/jpillora/notifyjs-com)
for an example project using `grunt-source-web`. Or [grunt-source-node](https://github.com/jpillora/grunt-source-node).#### MIT License
Copyright © 2013 Jaime Pillora <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.