-
Notifications
You must be signed in to change notification settings - Fork 0
/
BowerToNPM.java
161 lines (133 loc) · 6.64 KB
/
BowerToNPM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.StringBuilder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.Scanner;
public class BowerToNPM {
private static final String BLUE = "\u001B[34m";
private static final String BOLD = "\033[0;1m";
private static final String GREEN = "\u001B[32m";
private static final String RED = "\u001B[31m";
private static final String RESET = "\033[0;0m";
private static void convert(Path polymerPath, Path wcPath, Path oldPolymerPath, Path oldWcPath, Path directory) {
class BowerToNPMConverter {
private Path polymerPath;
private Path wcPath;
private Path oldPolymerPath;
private Path oldWcPath;
BowerToNPMConverter(Path polymerPath, Path wcPath, Path oldPolymerPath, Path oldWcPath, Path directory) {
this.polymerPath = polymerPath;
this.wcPath = wcPath;
this.oldPolymerPath = oldPolymerPath;
this.oldWcPath = oldWcPath;
rewriteFiles(directory);
}
private void rewriteFiles(Path directory) {
try {
ArrayDeque<String> files = filesToRewrite(directory.toFile());
BufferedReader reader = null;
BufferedWriter writer = null;
for(String f: files) {
// Note: Must call .getParent() because we want to see the relationship between the
// current file's enclosing folder and the old files
// Relative path from this file to new Polymer file
String relativePolymerPath = Paths.get(f).getParent().relativize(polymerPath).toString();
// Relative path from this file to new webcomponents file
String relativeWcPath = Paths.get(f).getParent().relativize(wcPath).toString();
// Relative path from this file to old Polymer file
String relativeOldPolymerPath = Paths.get(f).getParent().relativize(oldPolymerPath).toString();
// Relative path from this file to old webcomponents file
String relativeOldWcPath = Paths.get(f).getParent().relativize(oldWcPath).toString();
reader = new BufferedReader(new FileReader(f));
StringBuilder sb = new StringBuilder();
String line = "";
while((line = reader.readLine()) != null) {
line = line.replaceAll(relativeOldPolymerPath, relativePolymerPath);
line = line.replaceAll(relativeOldWcPath, relativeWcPath);
sb.append(line + "\n");
}
reader.close();
writer = new BufferedWriter(new FileWriter(f));
writer.write(sb.toString());
writer.close();
}
if(writer != null) writer.close();
if(reader != null) reader.close();
} catch(Exception e) {
System.err.println("Whoopsie. Something went wrong.");
e.printStackTrace();
System.exit(1);
}
}
private ArrayDeque<String> filesToRewrite(File directory) {
ArrayDeque<String> files = new ArrayDeque<String>();
String path = "";
if(!directory.isDirectory() && (path = directory.getAbsolutePath()).endsWith(".html")) {
files.add(path);
} else {
for(File f: directory.listFiles()) {
if(f.isFile() && (path = f.getAbsolutePath()).endsWith(".html")) {
files.add(path);
} else if(f.isDirectory()) {
files.addAll(filesToRewrite(f));
}
}
}
return files;
}
}
BowerToNPMConverter converter = new BowerToNPMConverter(polymerPath, wcPath, oldPolymerPath, oldWcPath, directory);
}
// Note: This must return an absolute path
private static Path checkPath(String path) {
if(!new File(path).exists()) {
// Assumes that even though the path is invalid,
// the user knows which relative directory path gshould be.
System.out.println(RED + "WARNING: " + RESET + "Invalid path: " + path + " , continuing anyway.");
}
return Paths.get(new File(path).getAbsolutePath());
}
public static void main(String[] args) {
// silent
boolean silentMode = false;
if(args.length > 0) {
if(args[0].equals("-s")) {
silentMode = true;
}
}
Scanner in = new Scanner(System.in);
String input = "";
// node_modules/@polymer/polymer/polymer.html
if(!silentMode) System.out.print(RESET + BLUE + "Path to " + RESET + BOLD + "NEW Polymer file: " + RESET + GREEN);
input = in.next();
System.out.print(RESET);
Path polymerPath = checkPath(input);
// node_modules/@polymer/polymer/node_modules/webcomponents.js/webcomponents-lite.js
if(!silentMode) System.out.print(RESET + BLUE + "Path to " + RESET + BOLD + "NEW webcomponents.js file: " + RESET + GREEN);
input = in.next();
System.out.print(RESET);
Path wcPath = checkPath(input);
// bower_components/polymer/polymer.html
if(!silentMode)System.out.print(RESET + BLUE + "Path to " + RESET + BOLD + "OLD Polymer file: " + RESET + GREEN);
input = in.next();
System.out.print(RESET);
Path oldPolymerPath = checkPath(input);
// bower_components/webcomponentsjs/webcomponents-lite.js
if(!silentMode) System.out.print(RESET + BLUE + "Path to " + RESET + BOLD + "OLD webcomponents.js file: " + RESET + GREEN);
input = in.next();
System.out.print(RESET);
Path oldWcPath = checkPath(input);
// bower_components/
if(!silentMode) System.out.print(RESET + BLUE + "Path to " + RESET + BOLD + "directory or file to convert: " + RESET + GREEN);
input = in.next();
System.out.print(RESET);
Path directory = checkPath(input);
convert(polymerPath, wcPath, oldPolymerPath, oldWcPath, directory);
}
}