Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
heldr committed May 22, 2013
0 parents commit ae98f9c
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
node_modules
npm-debug.log
tmp
14 changes: 14 additions & 0 deletions .jshintrc
@@ -0,0 +1,14 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"node": true,
"es5": true
}
2 changes: 2 additions & 0 deletions .npmignore
@@ -0,0 +1,2 @@
tmp
test
70 changes: 70 additions & 0 deletions Gruntfile.js
@@ -0,0 +1,70 @@
/*
* grunt-properties
* https://github.com/helder/grunt-properties
*
* Copyright (c) 2013 Helder Santana
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>',
],
options: {
jshintrc: '.jshintrc',
},
},

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
},

// Configuration to be run (and then tested).
properties: {
page_config: {
files: {
'tmp/page_config.js': ['test/fixtures/page_config.properties'],
},
},
page_color_config: {
options: {
namespace: 'mypage'
},
files: {
'tmp/page_color_config.js': ['test/fixtures/page_config.properties', 'test/fixtures/page_color_config.properties'],
},
},
},

// Unit tests.
nodeunit: {
tests: ['test/*_test.js'],
},

});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'properties', 'nodeunit']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);

};
22 changes: 22 additions & 0 deletions LICENSE-MIT
@@ -0,0 +1,22 @@
Copyright (c) 2013 Helder Santana

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.
91 changes: 91 additions & 0 deletions README.md
@@ -0,0 +1,91 @@
# grunt-properties

> Convert java .properties files to javascript
## Getting Started
This plugin requires Grunt `~0.4.1`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

```shell
npm install grunt-properties --save-dev
```

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
grunt.loadNpmTasks('grunt-properties');
```

## The "properties" task

### Overview
In your project's Gruntfile, add a section named `properties` to the data object passed into `grunt.initConfig()`.

```js
grunt.initConfig({
properties: {
page_config: {
files: {
'tmp/page_config.js': ['test/fixtures/page_config.properties'],
},
},
page_color_config: {
options: {
namespace: 'mypage'
},
files: {
'tmp/page_color_config.js': ['test/fixtures/page_config.properties', 'test/fixtures/page_color_config.properties'],
},
},
},
})
```

### Options

#### options.namespace
Type: `String`
Default value: `'config'`

Use a previously defined namespace.

### Usage Examples

#### Default namespace config
```js
grunt.initConfig({
properties: {
options: {},
files: {
'tmp/page_config.js': ['test/fixtures/page_config.properties'],
},
},
})
```

#### Customized namespace
```js
grunt.initConfig({
properties: {
page_color_config: {
options: {
namespace: 'mypage'
},
files: {
'tmp/page_color_config.js': ['test/fixtures/page_config.properties', 'test/fixtures/page_color_config.properties'],
},
},
},
})
```

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

## Release History
* 2013-05-22   v0.1.0   Initial release.

## Credits
* [node-properties-parser](https://github.com/xavi-/node-properties-parser)
* The Nicholas C. Zakas awesome project [props2js](https://github.com/nzakas/props2js).
46 changes: 46 additions & 0 deletions package.json
@@ -0,0 +1,46 @@
{
"name": "grunt-properties",
"description": "Convert java .properties files to javascript",
"version": "0.1.0",
"homepage": "https://github.com/helder/grunt-properties",
"author": {
"name": "Helder Santana",
"email": "helder.css@gmail.com",
"url": "http://heldr.com"
},
"repository": {
"type": "git",
"url": "git://github.com/helder/grunt-properties.git"
},
"bugs": {
"url": "https://github.com/helder/grunt-properties/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/helder/grunt-properties/blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"test": "grunt test"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt": "~0.4.1"
},
"peerDependencies": {
"grunt": "~0.4.1"
},
"keywords": [
"gruntplugin"
],
"dependencies": {
"properties-parser": "~0.1.1"
}
}
23 changes: 23 additions & 0 deletions tasks/lib/ns.js
@@ -0,0 +1,23 @@
// based on: http://addyosmani.com/blog/essential-js-namespacing/

'use strict';

module.exports = function ( ns, ns_string, lastPropertyValue ) {
var parts = ns_string.split('.'),
parent = ns,
pl, i;
if (parts[0] == "myApp") {
parts = parts.slice(1);
}
pl = parts.length;
for (i = 0; i < pl; i++) {
//create a property if it doesnt exist
if (i !== (parts.length - 1) && typeof parent[parts[i]] == 'undefined') {
parent[parts[i]] = {};
} else if (typeof parent[parts[i]] == 'undefined') {
parent[parts[i]] = lastPropertyValue;
}
parent = parent[parts[i]];
}
return ns;
};
63 changes: 63 additions & 0 deletions tasks/properties.js
@@ -0,0 +1,63 @@
/*
* grunt-properties
* https://github.com/helder/grunt-properties
*
* Copyright (c) 2013 Helder Santana
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

var pParser = require('properties-parser');

// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks

grunt.registerMultiTask('properties', 'Convert java .properties files to javascript', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
namespace: 'config'
}),
src = 'var ' + options.namespace + ' = ' + options.namespace + ' || {};\n';

function convert(filepath) {
var fileContent = grunt.file.read(filepath),
ns = options.namespace,
exp = null,
code = [];

fileContent = pParser.parse(fileContent);

for (var file in fileContent) {
exp = ns + '.' + file + ' = "' + fileContent[file] + '";';
code.push(exp);
}

return code.join('\n');
}

// Iterate over all specified file groups.
this.files.forEach(function(f) {

// Concat specified files.
src += f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('.properties file "' + filepath + '" not found.');
return false;
} else {
return true;
}
}).map(convert).join('\n');

// Write the destination file.
grunt.file.write(f.dest, src);

// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
});
});

};
5 changes: 5 additions & 0 deletions test/expected/page_color_config.js
@@ -0,0 +1,5 @@
var mypage = mypage || {};
mypage.TITLE = "MYPAGE";
mypage.URL = "http://mypage.com";
mypage.SKIN_HEADER = "grey";
mypage.SKIN_FOOTER = "blue";
3 changes: 3 additions & 0 deletions test/expected/page_config.js
@@ -0,0 +1,3 @@
var config = config || {};
config.TITLE = "MYPAGE";
config.URL = "http://mypage.com";
2 changes: 2 additions & 0 deletions test/fixtures/page_color_config.properties
@@ -0,0 +1,2 @@
SKIN_HEADER = grey
SKIN_FOOTER = blue
2 changes: 2 additions & 0 deletions test/fixtures/page_config.properties
@@ -0,0 +1,2 @@
TITLE = MYPAGE
URL = http://mypage.com

0 comments on commit ae98f9c

Please sign in to comment.