public ModuleScript loadAsDirectory()

in src/main/java/org/apache/sling/scripting/esx/Module.java [421:484]


    public ModuleScript loadAsDirectory(String module, String path, Resource currentResource, String loader) throws ScriptException {

        ResourceResolver resolver = currentResource.getResourceResolver();
        Resource packageJson = resolver.getResource(path + "/package.json");

        if (packageJson != null) {
            Node jsonFile = packageJson.adaptTo(Node.class);

            try {
                boolean isFile = (jsonFile.isNodeType(NodeType.NT_FILE) || jsonFile.isNodeType(NodeType.NT_RESOURCE));

                if (isFile) {

                    InputStream is = packageJson.getChild("jcr:content").adaptTo(InputStream.class);
                    try {
                        String jsonData = IOUtils.toString(is);

                        Invocable invocable = (Invocable) factory.getNashornEngine();
                        JSObject jsonprop = null;
                        try {
                            jsonprop = (JSObject) invocable.invokeMethod(factory.getNashornEngine().eval("JSON"), "parse", jsonData);
                        } catch (NoSuchMethodException ex) {
                            throw new ScriptException(ex);
                        }

                        Object main = jsonprop.getMember("main");
                        if (main != null) {
                            String packageModule = (String) main;

                            String mainpath = normalizePath(packageModule,
                                    path);
                            return loadAsFile(packageModule, mainpath, currentResource, loader);
                        }

                    } catch (IOException ex) {
                        throw new ScriptException(ex);
                    }
                }

            } catch (RepositoryException ex) {
                throw new ScriptException(ex);
            }

        }

        Resource indexjs = resolver.getResource(path + "/index.js");

        if (indexjs != null) {
            return createModuleScript(indexjs, ModuleScript.JS_FILE);
        }

        Resource indexjson = resolver.getResource(path + "/index.json");

        if (indexjson != null) {
            return createModuleScript(indexjson, ModuleScript.JSON_FILE);
        }

        Resource indexnode = resolver.getResource(path + "/index.node");
        if (indexnode != null) {
            throw new ScriptException("Node module .node (binary) loading is currently not supported");
        }

        return null;
    }