forked from benrowe/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
333 lines (299 loc) · 6.17 KB
/
util.go
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/kjk/u"
)
func must(err error) {
u.Must(err)
}
func fmtSmart(format string, args ...interface{}) string {
if len(args) == 0 {
return format
}
return fmt.Sprintf(format, args...)
}
func fmtArgs(args ...interface{}) string {
if len(args) == 0 {
return ""
}
format := args[0].(string)
if len(args) == 1 {
return format
}
return fmt.Sprintf(format, args[1:]...)
}
func panicIf(cond bool, args ...interface{}) {
if !cond {
return
}
if len(args) == 0 {
panic("condition failed")
}
panic(fmtArgs(args...))
}
func logIfError(err error) {
if err != nil {
fmt.Printf("Error: %s\n", err)
}
}
// whitelisted characters valid in url
func validateRune(c rune) byte {
if c >= 'a' && c <= 'z' {
return byte(c)
}
if c >= '0' && c <= '9' {
return byte(c)
}
if c == '-' || c == '_' || c == '.' || c == ' ' {
return '-'
}
return 0
}
func charCanRepeat(c byte) bool {
if c >= 'a' && c <= 'z' {
return true
}
if c >= '0' && c <= '9' {
return true
}
return false
}
// urlify generates safe url from tile by removing hazardous characters
func urlify(title string) string {
s := strings.TrimSpace(title)
s = strings.ToLower(s)
var res []byte
for _, r := range s {
c := validateRune(r)
if c == 0 {
continue
}
// eliminute duplicate consequitive characters
var prev byte
if len(res) > 0 {
prev = res[len(res)-1]
}
if c == prev && !charCanRepeat(c) {
continue
}
res = append(res, c)
}
s = string(res)
if len(s) > 128 {
s = s[:128]
}
s = strings.TrimLeft(s, "-")
s = strings.TrimRight(s, "-")
return s
}
var (
softErrorMode bool
delayedErrors []string
totalHTMLBytes int
totalHTMLBytesMinified int
)
func maybePanicIfErr(err error) {
if err == nil {
return
}
if !softErrorMode {
u.Must(err)
}
delayedErrors = append(delayedErrors, err.Error())
}
func clearErrors() {
delayedErrors = nil
totalHTMLBytes = 0
totalHTMLBytesMinified = 0
}
func printAndClearErrors() {
fmt.Printf("HTML: optimized %d => %d (saved %d bytes)\n", totalHTMLBytes, totalHTMLBytesMinified, totalHTMLBytes-totalHTMLBytesMinified)
if len(delayedErrors) == 0 {
return
}
errStr := strings.Join(delayedErrors, "\n")
fmt.Printf("\n%d errors:\n%s\n\n", len(delayedErrors), errStr)
clearErrors()
}
func createDirForFileMaybeMust(path string) {
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0755)
maybePanicIfErr(err)
}
func copyFileMaybeMust(dst, src string) error {
createDirForFileMaybeMust(dst)
err := u.CopyFile(dst, src)
maybePanicIfErr(err)
return err
}
// "foo.js" => "foo-${sha1}.js"
func nameToSha1Name(name, sha1Hex string) string {
ext := filepath.Ext(name)
n := len(name)
s := name[:n-len(ext)]
return s + "-" + sha1Hex[:8] + ext
}
func urlJoin(s1, s2 string) string {
if strings.HasSuffix(s1, "/") {
if strings.HasPrefix(s2, "/") {
return s1 + s2[1:]
}
return s1 + s2
}
if strings.HasPrefix(s2, "/") {
return s1 + s2
}
return s1 + "/" + s2
}
// removes empty lines from the beginning and end of the array
func trimEmptyLines(lines []string) []string {
for len(lines) > 0 && len(lines[0]) == 0 {
lines = lines[1:]
}
for len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
lines = lines[:len(lines)-1]
}
n := len(lines)
res := make([]string, 0, n)
prevWasEmpty := false
for i := 0; i < n; i++ {
l := lines[i]
shouldAppend := l != "" || !prevWasEmpty
prevWasEmpty = l == ""
if shouldAppend {
res = append(res, l)
}
}
return res
}
func countStartChars(s string, c byte) int {
for i := range s {
if s[i] != c {
return i
}
}
return len(s)
}
// remove longest common space/tab prefix on non-empty lines
func shiftLines(lines []string) {
maxTabPrefix := 1024
maxSpacePrefix := 1024
// first determine how much we can remove
for _, line := range lines {
if len(line) == 0 {
continue
}
n := countStartChars(line, ' ')
if n > 0 {
if n < maxSpacePrefix {
maxSpacePrefix = n
}
continue
}
n = countStartChars(line, '\t')
if n > 0 {
if n < maxTabPrefix {
maxTabPrefix = n
}
continue
}
// if doesn't start with space or tab, early abort
return
}
if maxSpacePrefix == 1024 && maxTabPrefix == 1024 {
return
}
toRemove := maxSpacePrefix
if maxTabPrefix != 1024 {
toRemove = maxTabPrefix
}
if toRemove == 0 {
return
}
for i, line := range lines {
if len(line) == 0 {
continue
}
lines[i] = line[toRemove:]
}
}
// replace potentially windows paths \foo\bar into unix paths /foo/bar
func toUnixPath(s string) string {
return strings.Replace(s, `\`, "/", -1)
}
func dataToLines(d []byte) []string {
s := string(d)
return strings.Split(s, "\n")
}
func reverseStringSlice(a []string) {
n := len(a) / 2
for i := 0; i < n; i++ {
a[i], a[n-i] = a[n-i], a[i]
}
}
// turn "010 Defining a SetterGetter" to "Defining a SetterGetter"
func cleanTitle(s string) string {
idx := strings.Index(s, " ")
if idx == -1 {
return s
}
if idx > 6 {
return s
}
maybeNum := s[:idx]
rest := s[idx+1:]
_, err := strconv.Atoi(maybeNum)
if err != nil {
return s
}
return strings.TrimSpace(rest)
}
func openForAppend(path string) *os.File {
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
must(err)
return f
}
// for dev, rebuilds in background
func launchRollup(ctx context.Context) func() {
path := filepath.Join("node_modules", ".bin", "rollup")
if !u.FileExists(path) {
// assume it's because node_modules doesn't exist, so install deps
//cmd := exec.Command("npm", "install")
cmd := exec.Command("yarn", "install")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
logf("Running: '%s'\n", cmd.String())
err := cmd.Run()
must(err)
}
cmd := exec.Command(path, "-c", "-w")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
logf("Running: '%s'\n", cmd.String())
err := cmd.Start()
must(err)
return func() {
_ = cmd.Process.Kill()
logf("Killed rollup\n")
}
}
func buildFrontend() {
{
os.Remove("package-lock.json")
os.RemoveAll("node_modules")
cmd := exec.Command("yarn", "install")
u.RunCmdMust(cmd)
}
// could also be
// .\node_modules\.bin\rollup -c
{
cmd := exec.Command("yarn", "build-dev")
u.RunCmdMust(cmd)
}
}