Skip to content

Commit

Permalink
Add ext option for avoid the same name of key
Browse files Browse the repository at this point in the history
  • Loading branch information
yss committed Jun 5, 2013
1 parent b4017a7 commit b86cea0
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ module.exports = function(grunt) {
dest: 'tmp/type.json'
}
]
},
ext: {
options: {
format: true,
ext: {
level: 1
}
},
files: [
{
src: [SRC_PATH],
dest: 'tmp/ext.json'
}
]
}
},

Expand Down
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# grunt-tree
Parse a directory to a tree with json format.

## Why I writen this plugin?
I need build my static files with md5 version. I want to get this:
``` js
// js.json
{
"base": "js/base.xxx.js",
"jquery": "js/jquery.xxx.js"
}
// or
{
"www-base": "www/js/base.xxx.js",
"mobile-base": "mobile/js/base.xxx.js"
}
// css.json
{
"base": "css/base.xxx.css"
}
```
Then, I can get the real path via a function, no matter what the environment is development or production.

> The best Grunt plugin ever.
## Getting Started
Expand Down Expand Up @@ -76,6 +96,17 @@ Default value: `true`

Whether recurse in your given directory.

#### options.ext
Type: `Object`
Default value: `{ level:0, hyphen: "-" }`

There is a new option for resolve the problem of the same name in file.
And the form option must be set to true.

The level option in options.ext means the subdirectory level.
If file relative path is 'www/css/base.css' and level set to 1, then the result will be: `"www-base": "www/css/base.css"`.
And if level set to 2, then the result will be: `"css-base": "www/css/base.css"`. But you set level to 3 in this condition, the result also is : `"base": "www/css/base.css"`.

Anyway, see the examples.

### Usage Examples
Expand Down Expand Up @@ -138,10 +169,18 @@ grunt.initConfig({
"c": "c"
}

// 6. try to mix the options, and have a look.
// 6. change the options to : { format: true, ext: { level: 1, hyphen: "-" } }, and result will be like:
// Attention: if we use ext option, and the format must be set to true.
{
"a": "a.css",
"c":"c",
"js-b":"js/b.js"
}
// 7. try to mix the options, and have a look.
...

```

### Test
```shell
# Once you run the follow command in console, you should run `npm install` before.
Expand All @@ -152,3 +191,4 @@ grunt test

1. Compatibility fix for node 0.10.x [2013/04/12]
2. Add nodeunit test case [2013/04/12]
3. Add ext option for avoid the same name of key [2013/06/03]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-tree",
"description": "Parse a directory to a tree with json format.",
"version": "0.4.2",
"version": "0.4.3",
"homepage": "https://github.com/yss/grunt-tree",
"author": {
"name": "yansong",
Expand Down
34 changes: 33 additions & 1 deletion tasks/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ function getFileName(filename, version) {
}
}

// get the prefix with subdir
function getExtFileName(subdir, ext, filename) {
var prefix;
filename = getFileName(filename);
if (ext.level > 0 && subdir) {
prefix = subdir.split('/')[ext.level - 1];
if (prefix) {
filename = prefix + ext.hyphen + filename;
}
}
return filename;
}

// get md5 version of file
function getMd5Version(str, encoding, len) {
str = str || Math.random().toString();
Expand All @@ -80,10 +93,23 @@ module.exports = function(grunt) {
var options = this.options({
recurse: true,
// type: [],
ext: { // can be covered
// level: 0,
// hyphen: '-'
},
md5: false,
format: false
}), typeReg;

if (!options.ext.hyphen) {
options.ext.hyphen = '-';
}

if (options.ext.level > 0 && !options.format) {
grunt.fail.fatal('For use the ext option, you must set "format: true" in your options.');
return;
}

if (grunt.util.kindOf(options.type) === 'array') {
typeReg = new RegExp('\\.(?:' + options.type.join('|') + ')$');
} else {
Expand Down Expand Up @@ -113,6 +139,7 @@ module.exports = function(grunt) {
});

function toTree(abspath, subdir, filename) {
var extFileName;
// ensure subdir is not undefined
subdir = subdir || "";
// ignore hidden file
Expand All @@ -125,7 +152,12 @@ module.exports = function(grunt) {
if (typeReg && !typeReg.test(filename)) {
return;
}
tree[getFileName(filename)] = path.join(subdir, getMd5Name(abspath, filename, options.md5));
if (options.format) {
extFileName = getExtFileName(subdir, options.ext, filename);
} else {
extFileName = getFileName(filename);
}
tree[extFileName] = path.join(subdir, getMd5Name(abspath, filename, options.md5));
}
}

Expand Down
1 change: 1 addition & 0 deletions test/expected/ext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":"a.css","c":"c","js-b":"js/b.js"}
9 changes: 9 additions & 0 deletions test/tree_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ exports.tree = {
var actual = grunt.file.readJSON('tmp/type.json'),
expected = grunt.file.readJSON('test/expected/type.json');

test.deepEqual(actual, expected, 'Not equal at type method.');
test.done();
},
ext: function(test) {
test.expect(1);

var actual = grunt.file.readJSON('tmp/ext.json'),
expected = grunt.file.readJSON('test/expected/ext.json');

test.deepEqual(actual, expected, 'Not equal at type method.');
test.done();
}
Expand Down

0 comments on commit b86cea0

Please sign in to comment.