Search in sources :

Example 1 with JNPM

use of org.nustaq.kontraktor.webapp.npm.JNPM in project kontraktor by RuedigerMoeller.

the class JSXIntrinsicTranspiler method resolveImportSpec.

private File resolveImportSpec(File requiringFile, ImportSpec importSpec, FileResolver resolver, Map<String, Object> alreadyResolved, Set ignoredRequires) throws IOException {
    String from = importSpec.getFrom();
    File toReadFrom = requiringFile;
    // node package entry processing
    String toReadFromName = null;
    if (importSpec.isRequire()) {
        if (ignoredRequires.contains(importSpec.getFrom()))
            return null;
        if (dev && getConfig().getIgnoredDevRequires().contains(importSpec.getFrom())) {
            Log.Info(this, "omit " + importSpec.getFrom() + " caused by jnpm.kson");
            return null;
        }
        if (!dev && getConfig().getIgnoredProdRequires().contains(importSpec.getFrom())) {
            Log.Info(this, "omit " + importSpec.getFrom() + " caused by jnpm.kson");
            return null;
        }
        String canonicalF = findNodeSubDir(requiringFile);
        if (canonicalF != null) {
            // check for ignored requires in browser entry of package.json
            String key = "browser_" + canonicalF + "_" + from;
            JsonValue o = (JsonValue) alreadyResolved.get(key);
            if (o != null) {
                if (o.isString()) {
                    String oldFrom = from;
                    from = o.asString();
                    Log.Info(this, "mapping package.json/browser:" + oldFrom + " to " + from);
                } else if (o.isBoolean()) {
                    if (!o.asBoolean()) {
                        Log.Info(this, "ignoring because of package.json/browser:" + from);
                        return null;
                    }
                } else
                    Log.Warn(this, "unrecognized browser entry in package.json:" + o + ". file:" + requiringFile.getAbsolutePath());
            } else {
            // System.out.println("key lookup == null for browser setting :"+key);
            }
        } else {
            Log.Warn(this, "node module dir could not be resolved " + requiringFile.getAbsolutePath());
            return null;
        }
    }
    File resolvedFile;
    if (importSpec.isRequire()) {
        resolvedFile = findNodeModulesNearestMatch(requiringFile, from);
        if (resolvedFile != null) {
            toReadFromName = resolvedFile.getName();
            toReadFrom = resolvedFile;
        } else {
            int debug = 1;
        }
    } else {
        resolvedFile = resolver.resolveFile(requiringFile.getParentFile(), from);
    }
    if (resolvedFile != null && resolvedFile.isDirectory()) {
        if (isNotInNodeModules(requiringFile)) {
            String tlFrom = importSpec.getFrom();
            nodeTopLevelImports.put(tlFrom, resolvedFile);
        }
        File indexFile = processNodeDir(resolvedFile, resolver, alreadyResolved);
        if (indexFile == falseFile) {
            return null;
        }
        if (indexFile == null) {
            Log.Warn(this, "node directory could not be resolved to a resource :" + resolvedFile.getCanonicalPath());
            return null;
        } else {
            toReadFrom = indexFile;
            toReadFromName = indexFile.getName();
        }
    } else {
        // fixme: hasExtension
        if (!from.endsWith(".js") && !from.endsWith(".jsx") && !from.endsWith(".json")) {
            from += ".js";
        }
    }
    byte[] resolved = resolver.resolve(toReadFrom.getParentFile(), toReadFromName != null ? toReadFromName : from, alreadyResolved);
    if (resolved == null && from.endsWith(".js")) {
        // try jsx
        from = from.substring(0, from.length() - 3) + ".jsx";
        resolved = resolver.resolve(requiringFile.getParentFile(), from, alreadyResolved);
    }
    if (resolved != null) {
        if (resolved.length > 0) {
            // need re-resolve as extension might have changed
            resolvedFile = resolver.resolveFile(toReadFrom.getParentFile(), toReadFromName != null ? toReadFromName : from);
            String name = null;
            if (resolvedFile.getName().endsWith(".json")) {
                name = constructLibName(resolvedFile, resolver) + ".json";
                ByteArrayOutputStream jsonBao = new ByteArrayOutputStream(resolved.length + 100);
                jsonBao.write("(function(exports, require, module, __filename, __dirname) { module.exports = \n".getBytes("UTF-8"));
                jsonBao.write(resolved);
                String s = constructLibName(requiringFile, resolver);
                jsonBao.write(("})( kgetModule('" + s + "').exports, krequire, kgetModule('" + s + "'), '', '' );").getBytes("UTF-8"));
                resolver.install("/debug/" + name, jsonBao.toByteArray());
            }
        }
    } else {
        if (autoJNPM && jnpmNodeModulesDir != null) {
            String required = importSpec.getFrom();
            int i = required.indexOf("/");
            if (i >= 0) {
                required = required.substring(0, i);
            }
            // single file can't be a node module
            if (required.indexOf(".") < 0) {
                JNPMConfig config = getConfig();
                Log.Info(this, importSpec.getFrom() + " not found. installing .. '" + required + "'");
                try {
                    JNPM.InstallResult await = JNPM.Install(required, null, jnpmNodeModulesDir, config).await(180_000);
                    if (await == JNPM.InstallResult.INSTALLED)
                        return resolveImportSpec(requiringFile, importSpec, resolver, alreadyResolved, ignoredRequires);
                } catch (Throwable kt) {
                    kt.printStackTrace();
                    Log.Error(this, "jnpm install timed out. Check Proxy JVM settings, internet connectivity or just retry");
                }
            }
        }
        Log.Warn(this, importSpec.getFrom() + " not found. requiredBy:" + requiringFile.getCanonicalPath());
    }
    return requiringFile;
}
Also used : JNPM(org.nustaq.kontraktor.webapp.npm.JNPM) JNPMConfig(org.nustaq.kontraktor.webapp.npm.JNPMConfig) JsonValue(com.eclipsesource.json.JsonValue)

Aggregations

JsonValue (com.eclipsesource.json.JsonValue)1 JNPM (org.nustaq.kontraktor.webapp.npm.JNPM)1 JNPMConfig (org.nustaq.kontraktor.webapp.npm.JNPMConfig)1