Skip to content

Commit

Permalink
Merge pull request #233 from NativeScript/master
Browse files Browse the repository at this point in the history
merge master into release
  • Loading branch information
blagoev committed Sep 30, 2015
2 parents f2ed9e9 + 0a77c8a commit 6033ffc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/src/com/tns/FileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

public class FileSystem
{
private static final String appRootPrefix = "~/";

public static String readAll(InputStream inputStream) throws IOException
{
StringBuilder text;
Expand Down Expand Up @@ -96,7 +98,17 @@ public static JSONObject readJSONFile(File file) throws IOException, JSONExcepti

public static String resolveRelativePath(String path, String currentDirectory)
{
File temp = new File(currentDirectory, path);
String baseDir;
if (path.startsWith(appRootPrefix))
{
baseDir = new File(NativeScriptApplication.getInstance().getApplicationContext().getFilesDir(), "app").getAbsolutePath();
path = path.substring(appRootPrefix.length());
}
else
{
baseDir = currentDirectory;
}
File temp = new File(baseDir, path);
try
{
return temp.getCanonicalPath();
Expand All @@ -105,7 +117,7 @@ public static String resolveRelativePath(String path, String currentDirectory)
{
try
{
URI uri = new URI(currentDirectory);
URI uri = new URI(baseDir);
return uri.resolve(path).getPath();
}
catch(URISyntaxException e1){
Expand Down
2 changes: 1 addition & 1 deletion src/src/com/tns/Require.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static File findModuleFile(String moduleName, String currentDirectory)
directory = new File(moduleName);
jsFile = isJSFile ? new File(moduleName) : new File(moduleName + ".js");
}
else if (moduleName.startsWith("./") || moduleName.startsWith("../"))
else if (moduleName.startsWith("./") || moduleName.startsWith("../") || moduleName.startsWith("~/"))
{
// same or up directory
String resolvedPath = FileSystem.resolveRelativePath(moduleName, currentDirectory);
Expand Down
3 changes: 3 additions & 0 deletions test-app/assets/app/modules/mymodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.echo = function(value) {
return value;
}
11 changes: 11 additions & 0 deletions test-app/assets/app/tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,17 @@ describe("Tests ", function () {
expect(moduleName).toEqual("testModule");
});

it("When_require_a_module_via_app_root_syntax_it_should_be_loaded", function () {

__log("TEST: When_require_a_module_via_app_root_syntax_it_should_be_loaded");

var mymodule = require("~/modules/mymodule");
var value = mymodule.echo(12345)

expect(value).toBe(12345);
});


it("When_require_a_bcl_module_in_a_dir_it_should_be_loaded", function () {

__log("TEST: When_require_a_bcl_module_in_a_dir_it_should_be_loaded");
Expand Down

0 comments on commit 6033ffc

Please sign in to comment.