Skip to content

Commit

Permalink
dr-svg-sprites: nodejs doesn't found dr-svg-sprites #513
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Oct 24, 2016
1 parent 67b86e6 commit 27261fe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
9 changes: 8 additions & 1 deletion jooby-assets-nodejs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>jooby-assets-nodejs</artifactId>

<name>jscs module</name>
<name>nodejs module</name>

<build>
<plugins>
Expand Down Expand Up @@ -78,6 +78,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
24 changes: 21 additions & 3 deletions jooby-assets-nodejs/src/main/java/org/jooby/assets/Nodejs.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.Set;
import java.util.stream.Stream;

import org.jooby.Route;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -203,7 +204,7 @@ public void release() {
node.release();
}

private Path deploy(final String library) throws Exception {
public Path deploy(final String library) throws Exception {
URL url = loader.getResource(library);
if (url == null) {
throw new FileNotFoundException(library);
Expand All @@ -212,12 +213,19 @@ private Path deploy(final String library) throws Exception {
log.debug("{}", uri);
Path outdir = this.basedir.toPath().resolve("node_modules").resolve(library.replace("/", "."));
Optional<Path> basedir = Try.of(() -> Paths.get(uri)).toJavaOptional();
String libroot = Route.normalize("/" + library);
try (Library lib = loadLibrary(uri)) {
try (Stream<Path> stream = lib.stream()) {
stream.filter(it -> !Files.isDirectory(it))
.forEach(it -> {
String relative = basedir.map(d -> d.relativize(it).toString())
.orElse(it.toString().substring(1));
.orElseGet(() -> {
String fname = it.toString();
if (fname.startsWith(libroot)) {
fname = fname.substring(libroot.length());
}
return fname.substring(1);
});
Path output = outdir.resolve(relative);
File fout = output.toFile();
boolean copy = !fout.exists()
Expand Down Expand Up @@ -250,7 +258,17 @@ private Library loadLibrary(final URI lib) {
* @param callback Nodejs callback.
*/
public static void run(final CheckedConsumer<Nodejs> callback) {
Nodejs node = new Nodejs();
run(new File(System.getProperty("java.io.tmpdir")), callback);
}

/**
* Execute the given nodejs callback and automatically release v8 and nodejs resources.
*
* @param basedir Base dir where to deploy a library.
* @param callback Nodejs callback.
*/
public static void run(final File basedir, final CheckedConsumer<Nodejs> callback) {
Nodejs node = new Nodejs(basedir);
try {
callback.accept(node);
} catch (Throwable x) {
Expand Down
22 changes: 22 additions & 0 deletions jooby-assets-nodejs/src/test/java/org/jooby/assets/NodejsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jooby.assets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

public class NodejsTest {

@Test
public void deployjar() throws Exception {
Nodejs node = new Nodejs(new File("target"));
Path dir = node.deploy("META-INF/resources/webjars/jquery/3.1.1");
assertEquals(Paths.get("target", "node_modules", "META-INF.resources.webjars.jquery.3.1.1"),
dir);
assertTrue(dir.resolve("package.json").toFile().exists());
}
}

0 comments on commit 27261fe

Please sign in to comment.