FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tiny-serviceworker/cli.js at master · avoidwork/tiny-serviceworker · GitHub
avoidwork
/
tiny-serviceworker
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
0
Star
7
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tiny-serviceworker
/
cli.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
130 lines (104 loc) · 3.85 KB
Breadcrumbs
tiny-serviceworker
/
cli.js
Copy path
File metadata and controls
130 lines (104 loc) · 3.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env node
const
path
=
require
(
"path"
)
,
fs
=
require
(
"fs"
)
.
promises
,
argv
=
process
.
argv
.
filter
(
i
=>
i
.
charAt
(
0
)
===
"-"
&&
i
.
charAt
(
1
)
===
"-"
)
.
reduce
(
(
a
,
v
)
=>
{
const
x
=
v
.
split
(
"--"
)
[
1
]
.
split
(
"="
)
;
a
[
x
[
0
]
]
=
isNaN
(
x
[
1
]
)
===
false
?
Number
(
x
[
1
]
)
:
x
[
1
]
===
"true"
?
true
:
x
[
1
]
===
"false"
?
false
:
x
[
1
]
;
return
a
;
}
,
{
}
)
,
opts
=
{
cwd
:
process
.
cwd
(
)
,
announce
:
argv
.
announce
===
false
?
false
:
true
,
directories
:
argv
.
directories
||
""
,
files
:
argv
.
files
||
""
,
hosts
:
(
argv
.
hosts
||
""
)
.
split
(
","
)
,
increment
:
argv
.
increment
===
false
?
false
:
true
,
ignore
:
new
RegExp
(
`(
${
Array
.
from
(
new
Set
(
[
"/sw.js"
,
...
(
argv
.
ignore
||
""
)
.
split
(
","
)
.
filter
(
i
=>
i
.
length
>
0
)
]
)
)
.
map
(
i
=>
`/
${
i
.
replace
(
/
^
\/
/
,
""
)
.
replace
(
/
(
\/
|
\.
)
/
g
,
"\\$1"
)
.
replace
(
/
\*
/
,
".*"
)
}
`
)
.
join
(
")|("
)
}
)$`
)
,
loader
:
argv
.
loader
||
false
,
name
:
argv
.
name
||
"my-app"
,
reload
:
argv
.
reload
===
true
,
safari
:
argv
.
safari
!==
"false"
,
src
:
__dirname
,
timeout
:
argv
.
timeout
||
18e2
,
version
:
argv
.
version
||
1
,
walk
:
argv
.
walk
===
false
?
false
:
true
}
;
async
function
walk
(
directory
,
files
,
apath
=
`/
${
directory
}
`
)
{
let
result
;
try
{
const
dir
=
await
fs
.
readdir
(
directory
,
{
encoding
:
"utf8"
,
withFileTypes
:
true
}
)
,
ldirectories
=
dir
.
filter
(
i
=>
i
.
isDirectory
(
)
)
.
map
(
i
=>
i
.
name
)
,
lfiles
=
dir
.
filter
(
i
=>
i
.
isFile
(
)
&&
i
.
name
.
includes
(
"."
)
&&
i
.
name
.
charAt
(
0
)
!==
"."
)
.
map
(
i
=>
`
${
apath
}
/
${
i
.
name
}
`
)
;
result
=
[
...
files
,
...
lfiles
]
;
if
(
opts
.
walk
&&
ldirectories
.
length
>
0
)
{
for
(
const
ldirectory
of
ldirectories
)
{
result
=
await
walk
(
path
.
join
(
directory
,
ldirectory
)
,
result
,
`
${
apath
}
/
${
ldirectory
}
`
)
;
}
}
}
catch
(
err
)
{
console
.
error
(
err
.
stack
)
;
process
.
exit
(
1
)
;
}
return
result
;
}
(
async
function
(
)
{
const
fp
=
path
.
join
(
opts
.
cwd
,
"sw.js"
)
;
let
sw
=
await
fs
.
readFile
(
path
.
join
(
opts
.
src
,
"sw.js"
)
,
"utf8"
)
;
if
(
opts
.
increment
)
{
try
{
const
lsw
=
await
fs
.
readFile
(
fp
,
"utf8"
)
,
lversion
=
lsw
.
match
(
/
v
e
r
s
i
o
n
=
(
\d
+
)
/
)
;
if
(
lversion
!==
null
)
{
opts
.
version
=
Number
(
lversion
[
1
]
)
+
1
;
}
}
catch
(
err
)
{
void
0
;
}
}
if
(
opts
.
name
.
length
>
0
)
{
sw
=
sw
.
replace
(
"name = `my-app-v${version}`"
,
`name = \`
${
opts
.
name
}
-v\$\{version\}\``
)
;
}
sw
=
sw
.
replace
(
/
t
i
m
e
o
u
t
=
(
\d
+
)
/
,
`timeout =
${
opts
.
timeout
}
`
)
;
sw
=
sw
.
replace
(
/
v
e
r
s
i
o
n
=
(
\d
+
)
/
,
`version =
${
opts
.
version
}
`
)
;
if
(
opts
.
hosts
.
length
>
0
)
{
sw
=
sw
.
replace
(
"hosts = []"
,
`hosts =
${
JSON
.
stringify
(
opts
.
hosts
)
}
`
)
;
}
if
(
opts
.
files
.
length
>
0
&&
opts
.
directories
.
length
===
0
)
{
let
files
=
opts
.
files
.
split
(
","
)
;
sw
=
sw
.
replace
(
"urls = []"
,
`urls =
${
JSON
.
stringify
(
files
.
filter
(
i
=>
opts
.
ignore
.
test
(
i
)
===
false
)
)
}
`
)
;
}
if
(
opts
.
directories
.
length
>
0
)
{
const
directories
=
Array
.
from
(
new
Set
(
opts
.
directories
.
split
(
","
)
)
)
;
let
files
=
opts
.
files
.
length
>
0
?
[
"/"
,
"/manifest.json"
,
...
opts
.
files
.
split
(
","
)
]
:
[
"/"
,
"/manifest.json"
]
;
for
(
const
directory
of
directories
)
{
files
=
await
walk
(
directory
,
files
)
;
}
sw
=
sw
.
replace
(
"urls = [\"/\", \"/manifest.json\"]"
,
`urls =
${
JSON
.
stringify
(
files
.
filter
(
i
=>
opts
.
ignore
.
test
(
i
)
===
false
)
)
}
`
)
;
}
if
(
opts
.
reload
)
{
sw
=
sw
.
replace
(
"reload = false"
,
"reload = true"
)
;
}
if
(
opts
.
safari
===
false
)
{
sw
=
sw
.
replace
(
"safari = true"
,
"safari = false"
)
;
}
try
{
await
fs
.
writeFile
(
fp
,
sw
,
"utf8"
)
;
}
catch
(
err
)
{
console
.
error
(
err
.
stack
)
;
process
.
exit
(
1
)
;
}
if
(
opts
.
loader
)
{
let
loader
=
await
fs
.
readFile
(
path
.
join
(
opts
.
src
,
"loader.js"
)
,
"utf8"
)
;
if
(
opts
.
name
.
length
>
0
)
{
loader
=
loader
.
replace
(
"my-app"
,
opts
.
name
)
;
}
try
{
await
fs
.
writeFile
(
path
.
join
(
opts
.
cwd
,
"loader.js"
)
,
loader
,
"utf8"
)
;
}
catch
(
err
)
{
console
.
error
(
err
.
stack
)
;
process
.
exit
(
1
)
;
}
}
console
.
log
(
opts
.
loader
===
false
?
"Generated service worker (sw.js) script"
:
"Generated service worker (sw.js) & loader (loader.js) scripts"
)
;
process
.
exit
(
0
)
;
}
(
)
)
;
Back
|
FazBrowse Home
|
New Git URL