FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
community-themes/env.mjs at trunk · WordPress/community-themes · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WordPress
/
community-themes
Public
Notifications
You must be signed in to change notification settings
Fork
38
Star
96
Code
Issues
56
Pull requests
22
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
community-themes
/
env.mjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
144 lines (123 loc) · 3.98 KB
Breadcrumbs
community-themes
/
env.mjs
Copy path
File metadata and controls
144 lines (123 loc) · 3.98 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
/**
* Wraps wp-env with auto-detected ports and theme activation.
* Saves the active port to .wp-env-port so stop/destroy target the right instance.
*
* Usage:
* node env.mjs start [--theme <theme-slug>] [--config <file>]
* node env.mjs stop
* node env.mjs destroy
*
* npm run env:start -- --theme blue-note
* npm run env:stop
* npm run env:destroy
*/
import
{
createServer
}
from
'net'
;
import
{
spawn
}
from
'child_process'
;
import
{
readFileSync
,
writeFileSync
,
existsSync
,
unlinkSync
}
from
'fs'
;
const
PORT_FILE
=
'.wp-env-port'
;
function
isPortFree
(
port
)
{
return
new
Promise
(
(
resolve
)
=>
{
const
server
=
createServer
(
)
;
server
.
on
(
'error'
,
(
)
=>
resolve
(
false
)
)
;
server
.
listen
(
port
,
(
)
=>
server
.
close
(
(
)
=>
resolve
(
true
)
)
)
;
}
)
;
}
async
function
findFreePort
(
start
=
8888
)
{
return
(
await
isPortFree
(
start
)
)
?
start
:
findFreePort
(
start
+
1
)
;
}
function
readSavedPort
(
)
{
if
(
existsSync
(
PORT_FILE
)
)
{
const
port
=
parseInt
(
readFileSync
(
PORT_FILE
,
'utf8'
)
.
trim
(
)
,
10
)
;
if
(
!
isNaN
(
port
)
)
return
port
;
}
return
null
;
}
function
runWpEnv
(
wpEnvArgs
,
env
)
{
return
new
Promise
(
(
resolve
)
=>
{
const
child
=
spawn
(
'wp-env'
,
wpEnvArgs
,
{
stdio
:
'inherit'
,
env
}
)
;
child
.
on
(
'exit'
,
(
code
)
=>
resolve
(
code
??
0
)
)
;
}
)
;
}
const
args
=
process
.
argv
.
slice
(
2
)
;
const
command
=
args
[
0
]
;
// --- stop / destroy ---
if
(
command
===
'stop'
||
command
===
'destroy'
)
{
const
savedPort
=
readSavedPort
(
)
;
const
wpEnvEnv
=
{
...
process
.
env
}
;
if
(
savedPort
)
{
wpEnvEnv
.
WP_ENV_PORT
=
String
(
savedPort
)
;
wpEnvEnv
.
WP_ENV_TESTS_PORT
=
String
(
savedPort
+
1
)
;
}
const
code
=
await
runWpEnv
(
args
,
wpEnvEnv
)
;
if
(
code
===
0
&&
command
===
'destroy'
)
{
try
{
unlinkSync
(
PORT_FILE
)
;
}
catch
{
}
}
process
.
exit
(
code
)
;
}
// --- start ---
// Extract --theme <slug> from args, pass the rest to wp-env.
const
themeIndex
=
args
.
indexOf
(
'--theme'
)
;
let
themeSlug
=
null
;
if
(
themeIndex
!==
-
1
)
{
themeSlug
=
args
[
themeIndex
+
1
]
;
args
.
splice
(
themeIndex
,
2
)
;
}
const
savedPort
=
readSavedPort
(
)
;
const
alreadyRunning
=
savedPort
&&
!
(
await
isPortFree
(
savedPort
)
)
;
if
(
alreadyRunning
)
{
// Instance is already running — skip wp-env start, just activate the theme.
console
.
log
(
`wp-env is already running on port
${
savedPort
}
.`
)
;
if
(
themeSlug
)
{
activateTheme
(
themeSlug
,
buildEnv
(
savedPort
)
)
;
}
else
{
process
.
exit
(
0
)
;
}
}
else
{
const
port
=
await
findFreePort
(
)
;
const
testsPort
=
await
findFreePort
(
port
+
1
)
;
if
(
port
!==
8888
)
{
console
.
log
(
`Port 8888 is in use, starting on port
${
port
}
instead.`
)
;
}
const
wpEnvEnv
=
buildEnv
(
port
,
testsPort
)
;
const
child
=
spawn
(
'wp-env'
,
args
,
{
stdio
:
'inherit'
,
env
:
wpEnvEnv
}
)
;
child
.
on
(
'exit'
,
(
code
)
=>
{
if
(
code
!==
0
)
{
process
.
exit
(
code
)
;
}
// Persist port so stop/destroy can target this instance.
writeFileSync
(
PORT_FILE
,
String
(
port
)
)
;
if
(
!
themeSlug
)
{
process
.
exit
(
0
)
;
}
activateTheme
(
themeSlug
,
wpEnvEnv
)
;
}
)
;
}
function
buildEnv
(
port
,
testsPort
=
port
+
1
)
{
return
{
...
process
.
env
,
WP_ENV_PORT
:
String
(
port
)
,
WP_ENV_TESTS_PORT
:
String
(
testsPort
)
}
;
}
function
activateTheme
(
slug
,
wpEnvEnv
)
{
console
.
log
(
`Activating theme:
${
slug
}
`
)
;
const
activate
=
spawn
(
'wp-env'
,
[
'run'
,
'cli'
,
'wp'
,
'theme'
,
'activate'
,
slug
]
,
{
stdio
:
[
'inherit'
,
'pipe'
,
'pipe'
]
,
env
:
wpEnvEnv
,
}
)
;
let
activateOutput
=
''
;
activate
.
stdout
.
on
(
'data'
,
(
data
)
=>
{
const
text
=
data
.
toString
(
)
;
activateOutput
+=
text
;
process
.
stdout
.
write
(
text
)
;
}
)
;
activate
.
stderr
.
on
(
'data'
,
(
data
)
=>
{
const
text
=
data
.
toString
(
)
;
activateOutput
+=
text
;
process
.
stderr
.
write
(
text
)
;
}
)
;
activate
.
on
(
'exit'
,
(
activateCode
)
=>
{
if
(
activateCode
!==
0
||
!
activateOutput
.
includes
(
'Success:'
)
)
{
console
.
error
(
`Error: Failed to activate theme '
${
slug
}
'. Make sure the theme slug is correct.`
)
;
process
.
exit
(
1
)
;
}
process
.
exit
(
0
)
;
}
)
;
}
Back
|
FazBrowse Home
|
New Git URL