1
+ package org .jfrog .build .extractor .npm ;
2
+
3
+ import com .fasterxml .jackson .databind .JsonNode ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
5
+ import com .fasterxml .jackson .databind .ObjectReader ;
6
+ import org .apache .commons .lang .StringUtils ;
7
+ import org .jfrog .build .extractor .executor .CommandExecutor ;
8
+ import org .jfrog .build .extractor .executor .CommandResults ;
9
+
10
+ import java .io .File ;
11
+ import java .io .IOException ;
12
+ import java .io .Serializable ;
13
+ import java .util .ArrayList ;
14
+ import java .util .Arrays ;
15
+ import java .util .Collections ;
16
+ import java .util .List ;
17
+ import java .util .stream .Collectors ;
18
+ import java .util .stream .Stream ;
19
+
20
+ /**
21
+ * @author Yahav Itzhak
22
+ */
23
+ public class NpmDriver implements Serializable {
24
+ private static final long serialVersionUID = 1L ;
25
+
26
+ private static ObjectReader jsonReader = new ObjectMapper ().reader ();
27
+ private CommandExecutor commandExecutor ;
28
+
29
+ public NpmDriver (String executablePath ) {
30
+ this .commandExecutor = new CommandExecutor (StringUtils .defaultIfEmpty (executablePath , "npm" ));
31
+ }
32
+
33
+ @ SuppressWarnings ("unused" )
34
+ public boolean isNpmInstalled () {
35
+ try {
36
+ version (new File ("" ));
37
+ return true ;
38
+ } catch (IOException | InterruptedException e ) {
39
+ return false ;
40
+ }
41
+ }
42
+
43
+ public void install (File workingDirectory , List <String > extraArgs ) throws IOException {
44
+ try {
45
+ runCommand (workingDirectory , new String []{"i" }, extraArgs );
46
+ } catch (IOException | InterruptedException e ) {
47
+ throw new IOException ("npm install failed: " + e .getMessage (), e );
48
+ }
49
+ }
50
+
51
+ public void pack (File workingDirectory , List <String > extraArgs ) throws IOException {
52
+ try {
53
+ runCommand (workingDirectory , new String []{"pack" }, extraArgs );
54
+ } catch (IOException | InterruptedException e ) {
55
+ throw new IOException ("npm pack failed: " + e .getMessage (), e );
56
+ }
57
+ }
58
+
59
+ public JsonNode list (File workingDirectory , List <String > extraArgs ) throws IOException {
60
+ List <String > args = new ArrayList <>();
61
+ args .add ("ls" );
62
+ args .add ("--json" );
63
+ args .addAll (extraArgs );
64
+ try {
65
+ CommandResults npmCommandRes = commandExecutor .exeCommand (workingDirectory , args );
66
+ String res = StringUtils .isBlank (npmCommandRes .getRes ()) ? "{}" : npmCommandRes .getRes ();
67
+ return jsonReader .readTree (res );
68
+ } catch (IOException | InterruptedException e ) {
69
+ throw new IOException ("npm ls failed" , e );
70
+ }
71
+ }
72
+
73
+ public String version (File workingDirectory ) throws IOException , InterruptedException {
74
+ return runCommand (workingDirectory , new String []{"--version" }, Collections .emptyList ());
75
+ }
76
+
77
+ public String configList (File workingDirectory , List <String > extraArgs ) throws IOException , InterruptedException {
78
+ return runCommand (workingDirectory , new String []{"c" , "ls" , "--json" }, extraArgs );
79
+ }
80
+
81
+ private String runCommand (File workingDirectory , String [] args , List <String > extraArgs ) throws IOException , InterruptedException {
82
+ List <String > finalArgs = Stream .concat (Arrays .stream (args ), extraArgs .stream ()).collect (Collectors .toList ());
83
+ CommandResults npmCommandRes = commandExecutor .exeCommand (workingDirectory , finalArgs );
84
+ if (!npmCommandRes .isOk ()) {
85
+ throw new IOException (npmCommandRes .getErr ());
86
+ }
87
+ return npmCommandRes .getRes ();
88
+ }
89
+ }
0 commit comments