Skip to content

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed Aug 16, 2013
1 parent ef912a5 commit 3841545
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
v1.0.3 (16 Aug 2013)
The paths from the imported files are relative to the current file.

v1.0.2 (16 Aug 2013)
Minor bugfixes with sections that don't end with "]".

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ _Node.js project_

#### .properties parser/stringifier ####

Version: 1.0.2
Version: 1.0.3

[Specification](http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.Reader%29)

Expand Down Expand Up @@ -261,11 +261,11 @@ Note: The whitespace (`<space>`, `\t`, `\f`) is still considered a separator eve
<a name="include"></a>
__Importing files__

When the `include` option is enabled, the `include` key allows you import files. If the path is a directory it tries to load the file `index.properties`. The paths are relative from the main file, the path that you pass to the [parse()](#parse) function.
When the `include` option is enabled, the `include` key allows you import files. If the path is a directory it tries to load the file `index.properties`. The paths are relative from the __current__ file.

The imported files are merged with the current file, they can replace old data.

The _include_ keywords cannot appear inside a section, they must be global properties.
The _include_ keyword cannot appear inside a section, it must be a global property.

```
include a/file
Expand Down
2 changes: 1 addition & 1 deletion examples/i18n/de
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# German
[de]

text1 Hallo
id0 Hallo
2 changes: 1 addition & 1 deletion examples/i18n/en
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# English
[en]

text1 Hello
id0 Hello
2 changes: 1 addition & 1 deletion examples/i18n/es
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Spanish
[es]

text1 Hola
id0 Hola
6 changes: 3 additions & 3 deletions examples/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ properties.parse (".", options, function (error, p){
/*
{
en: {
text1: "Hello"
id0: "Hello"
},
es: {
text1: "Hola"
id0: "Hola"
},
de: {
text1: "Hallo"
id0: "Hallo"
}
}
*/
Expand Down
22 changes: 11 additions & 11 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var merge = function (o1, o2){
return o1;
};

var build = function (data, options, cb){
var build = function (data, options, dirname, cb){
var o = {};

if (options.namespaces){
Expand All @@ -170,7 +170,7 @@ var build = function (data, options, cb){
"a section: " + currentSection));
}

var p = path.resolve (value);
var p = path.resolve (dirname, value);
if (options._included[p]) return;

options._included[p] = true;
Expand Down Expand Up @@ -373,20 +373,22 @@ var build = function (data, options, cb){
return options.namespaces ? n : o;
};

var read = function (f, options, cb, first){
var read = function (f, options, cb){
fs.stat (f, function (error, stats){
if (error) return cb (error);

var dirname;

if (stats.isDirectory ()){
if (first) options._dirname = f;
dirname = f;
f = path.join (f, INDEX_FILE);
}else if (first){
options._dirname = path.dirname (f);
}else{
dirname = path.dirname (f);
}

fs.readFile (f, { encoding: "utf8" }, function (error, data){
if (error) return cb (error);
build (data, options, cb);
build (data, options, dirname, cb);
});
});
};
Expand All @@ -401,8 +403,6 @@ module.exports = function (data, options, cb){

if (options.include){
options._included = {};
//If there's no path the basedir is the cwd
options._dirname = ".";
}

options = options || {};
Expand Down Expand Up @@ -439,14 +439,14 @@ module.exports = function (data, options, cb){
if (!cb) throw new PropertiesError ("A callback must be passed if the " +
"data is a path");
if (options.include){
read (data, options, cb, true);
read (data, options, cb);
}else{
fs.readFile (data, { encoding: "utf8" }, function (error, data){
if (error) return cb (error);
build (data, options, cb);
});
}
}else{
return build (data, options, cb);
return build (data, options, ".", cb);
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "properties",
"version": "1.0.2",
"version": "1.0.3",
"description": ".properties parser/stringifier",
"keywords": ["properties", "ini", "parser", "stringifier", "config"],
"author": {
Expand Down
1 change: 1 addition & 0 deletions test/include-relative-1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include include-relative/include-relative-2
1 change: 1 addition & 0 deletions test/include-relative-3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a 1
1 change: 1 addition & 0 deletions test/include-relative/include-relative-2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../include-relative-3
19 changes: 18 additions & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ var tests = {
done ();
});
},
"include, empty file": function (done){
"include empty file": function (done){
var options = { include: true };

properties.parse ("include include-empty", options, function (error, p){
Expand Down Expand Up @@ -615,6 +615,23 @@ var tests = {
properties.parse ("include-section-1", options, function (error, p){
assert.ok (error);

done ();
});
},
"include relative from current file": function (done){
var options = {
include: true,
sections: true
};

properties.parse ("include include-relative-1", options,
function (error, p){
assert.ifError (error);

assert.deepEqual (p, {
a: 1
});

done ();
});
}
Expand Down

0 comments on commit 3841545

Please sign in to comment.