-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.hx
208 lines (124 loc) · 3.57 KB
/
script.hx
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package;
import hxp.*;
import sys.FileSystem;
class Script extends hxp.Script {
private var samples:Array<String>;
public function new () {
super ();
samples = [];
findSamples ("", samples);
samples.sort (function (a, b) {
a = a.toLowerCase ();
b = b.toLowerCase ();
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
switch (command) {
case "list":
listSamples ();
case "clean":
cleanSamples ();
case "npm-check-updates", "check-update", "check-updates":
execSamples ("ncu");
default:
execSamples ("npm");
}
}
private function cleanSamples ():Void {
var paths = [];
if (commandArgs.length > 0) {
var sampleName = commandArgs.shift ();
if (sampleName == "*") sampleName = "all";
for (sample in samples) {
if (sample.split ("/").pop () == sampleName) {
paths.push (sample);
}
}
if (paths.length == 0 && sampleName == "all") {
paths = samples.copy ();
}
if (paths.length == 0) {
for (sample in samples) {
if (StringTools.startsWith (sample, sampleName)) {
paths.push (sample);
}
}
}
if (paths.length == 0) {
Log.error ("Could not find sample name \"" + sampleName + "\"");
}
} else {
paths = paths.concat (samples);
}
for (path in paths) {
var modules = Path.combine (path, "node_modules");
if (FileSystem.exists (modules)) {
Log.info (Log.accentColor + "rm -r " + modules + Log.resetColor);
System.removeDirectory (modules);
}
}
}
private function execSamples (script:String):Void {
var paths = [];
if (commandArgs.length > 0) {
var sampleName = commandArgs.shift ();
for (sample in samples) {
if (sample.split ("/").pop () == sampleName) {
paths.push (sample);
}
}
if (paths.length == 0 && sampleName == "all") {
paths = samples.copy ();
}
if (paths.length == 0) {
for (sample in samples) {
if (StringTools.startsWith (sample, sampleName)) {
paths.push (sample);
}
}
}
if (paths.length == 0) {
Log.error ("Could not find sample name \"" + sampleName + "\"");
}
} else {
paths = paths.concat (samples);
}
for (path in paths) {
var sampleName = path.split ("/").pop ();
args = [ command ].concat (commandArgs);
for (flag in flags.keys ()) {
args.push ("-" + flag);
}
for (option in options.keys ()) {
args.push (option);
}
args.push ("-s");
Log.info (Log.accentColor + "cd " + path + " && " + script + " " + args.join (" ") + Log.resetColor);
System.runCommand (path, script, args);
}
}
private function findSamples (path:String, list:Array<String>):Void {
if (path == "node_modules") return;
for (fileName in FileSystem.readDirectory (path != "" ? path : Sys.getCwd ())) {
var filePath = Path.combine (path, fileName);
if (FileSystem.isDirectory (filePath)) {
var match = false;
for (file in [ "package.json" ]) {
if (FileSystem.exists (Path.combine (filePath, file))) {
list.push (Path.standardize (filePath));
match = true;
break;
}
}
if (!match) findSamples (filePath, list);
}
}
}
private function listSamples ():Void {
for (sample in samples) {
var sampleName = sample.split ("/").pop ();
Log.println (Log.accentColor + sampleName + Log.resetColor + " (" + sample + ")");
}
}
}