FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql-action/build.mjs at default-setup-testing · github/codeql-action · GitHub
github
codeql-action
Repository navigation
Code
Issues
165
(165)
Pull requests
44
(44)
Actions
Security and quality
2
(2)
Insights
Expand file tree
Breadcrumbs
codeql-action
/
build.mjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
223 lines (195 loc) · 7.12 KB
Breadcrumbs
codeql-action
/
build.mjs
Copy path
File metadata and controls
223 lines (195 loc) · 7.12 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
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
import
{
copyFile
,
readFile
,
rm
,
writeFile
}
from
"node:fs/promises"
;
import
{
basename
,
dirname
,
join
}
from
"node:path"
;
import
{
fileURLToPath
}
from
"node:url"
;
import
*
as
esbuild
from
"esbuild"
;
import
{
globSync
}
from
"glob"
;
import
pkg
from
"./package.json"
with
{
type
:
"json"
}
;
const
__filename
=
fileURLToPath
(
import
.
meta
.
url
)
;
const
__dirname
=
dirname
(
__filename
)
;
const
SRC_DIR
=
join
(
__dirname
,
"src"
)
;
const
OUT_DIR
=
join
(
__dirname
,
"lib"
)
;
/**
* Clean the output directory before building.
*
*
@type
{
esbuild.Plugin
}
*/
const
cleanPlugin
=
{
name
:
"clean"
,
setup
(
build
)
{
build
.
onStart
(
async
(
)
=>
{
await
rm
(
OUT_DIR
,
{
recursive
:
true
,
force
:
true
}
)
;
}
)
;
}
,
}
;
/**
* Copy defaults.json to the output directory since other projects depend on it.
*
*
@type
{
esbuild.Plugin
}
*/
const
copyDefaultsPlugin
=
{
name
:
"copy-defaults"
,
setup
(
build
)
{
build
.
onEnd
(
async
(
)
=>
{
await
rm
(
join
(
OUT_DIR
,
"defaults.json"
)
,
{
force
:
true
,
}
)
;
await
copyFile
(
join
(
SRC_DIR
,
"defaults.json"
)
,
join
(
OUT_DIR
,
"defaults.json"
)
,
)
;
}
)
;
}
,
}
;
/**
* Log when the build ends.
*
*
@type
{
esbuild.Plugin
}
*/
const
onEndPlugin
=
{
name
:
"on-end"
,
setup
(
build
)
{
build
.
onEnd
(
(
result
)
=>
{
// eslint-disable-next-line no-console
console
.
log
(
`Build ended with
${
result
.
errors
.
length
}
errors`
)
;
}
)
;
}
,
}
;
/** The name of the virtual `entry-points` module. */
const
SHARED_ENTRYPOINT
=
"entry-points"
;
/** The property name under which `upload-lib`'s namespace is exposed in `entry-points`. */
const
UPLOAD_LIB_EXPORT
=
"uploadLib"
;
/** The relative source path of the `upload-lib` module that we re-export from `entry-points`. */
const
UPLOAD_LIB_SRC
=
"./src/upload-lib"
;
/**
* This plugin finds all source files that contain Action entry points. It then generates the
* virtual `entry-points` module which imports all identified files, and re-exports their
* `runWrapper` functions with suitable aliases.
*
* The virtual module additionally re-exports `upload-lib` under the `uploadLib` namespace so that
* external consumers can access it via the small `lib/upload-lib.js` stub emitted below.
*
* A tiny stub file is emitted for each Action entrypoint, and one for `upload-lib`. Each stub
* imports the shared bundle and calls/re-exports from the respective entry point.
*
*
@type
{
esbuild.Plugin
}
*/
const
entryPointsPlugin
=
{
name
:
"entry-points"
,
setup
(
build
)
{
const
namespace
=
"actions"
;
const
actions
=
[
]
;
const
toPascal
=
(
s
)
=>
s
.
replace
(
/
(
^
|
-
)
(
[
a
-
z
0
-
9
]
)
/
gi
,
(
_
,
__
,
c
)
=>
c
.
toUpperCase
(
)
)
;
// Find the source files containing Action entry points.
build
.
onStart
(
(
)
=>
{
const
actionFiles
=
globSync
(
"src/*-action{,-post}.ts"
)
;
for
(
const
actionFile
of
actionFiles
)
{
const
match
=
basename
(
actionFile
)
.
match
(
/
(
.
*
)
-
a
c
t
i
o
n
(
-
p
o
s
t
)
?
/
)
;
if
(
match
.
length
<
2
)
{
throw
new
Error
(
`'
${
actionFile
}
' didn't match expected pattern.`
)
;
}
const
actionName
=
match
[
1
]
;
const
isPost
=
match
[
2
]
!==
undefined
;
actions
.
push
(
{
path
:
actionFile
,
name
:
actionName
,
isPost
,
pascalCaseName
:
`
${
toPascal
(
actionName
)
}
${
isPost
?
"Post"
:
""
}
Action`
,
}
)
;
}
}
)
;
// Resolve the virtual `entry-points` file and set the corresponding namespace.
// Ideally, we'd `RegExp.escape` the entrypoint here, but that API isn't supported in Node 20.
// Since we're dealing with a hardcoded string, this isn't too much of a problem.
build
.
onResolve
(
{
filter
:
new
RegExp
(
`^
${
SHARED_ENTRYPOINT
}
$`
)
}
,
(
)
=>
{
return
{
path
:
SHARED_ENTRYPOINT
,
namespace
}
;
}
)
;
// Generate the virtual `entry-points` file based on the Actions we discovered.
// Restrict using the namespace. The path filter does not need to discriminate any further.
build
.
onLoad
(
{
filter
:
/
.
*
/
,
namespace
}
,
async
(
)
=>
{
const
wrapperTemplatePath
=
"entry-wrapper.js.tpl"
;
const
wrapperTemplate
=
await
readFile
(
join
(
SRC_DIR
,
wrapperTemplatePath
)
,
"utf-8"
,
)
;
const
actionsSorted
=
actions
.
sort
(
(
a
,
b
)
=>
a
.
name
.
localeCompare
(
b
.
name
)
,
)
;
const
imports
=
actionsSorted
.
map
(
(
action
)
=>
`import * as
${
action
.
pascalCaseName
}
from "./src/
${
basename
(
action
.
path
)
}
";`
,
)
.
join
(
"\n"
)
;
const
wrappers
=
actionsSorted
.
map
(
(
action
)
=>
wrapperTemplate
.
replaceAll
(
"__ACTION__"
,
action
.
pascalCaseName
)
,
)
.
join
(
"\n\n"
)
;
// Also re-export the `upload-lib` namespace so that external consumers can reach it
// via the `lib/upload-lib.js` stub without us having to bundle a second copy.
const
uploadLibReExport
=
`export * as
${
UPLOAD_LIB_EXPORT
}
from "
${
UPLOAD_LIB_SRC
}
";`
;
return
{
contents
:
`"use strict";\n
${
imports
}
\n\n
${
uploadLibReExport
}
\n\n
${
wrappers
}
\n`
,
resolveDir
:
"."
,
loader
:
"ts"
,
}
;
}
)
;
// Emit entry point stubs for each Action using the entry template.
build
.
onEnd
(
async
(
)
=>
{
const
makeHeader
=
(
templatePath
,
sourceFile
)
=>
`// Automatically generated from '
${
templatePath
}
' for 'src/
${
basename
(
sourceFile
)
}
'.\n\n`
;
// Read the entry point template.
const
actionTemplatePath
=
"action-entry.js.tpl"
;
const
actionTemplate
=
await
readFile
(
join
(
SRC_DIR
,
actionTemplatePath
)
,
"utf-8"
,
)
;
// Write entry point stubs for each Action.
for
(
const
action
of
actions
)
{
await
writeFile
(
join
(
OUT_DIR
,
`
${
action
.
name
}
${
action
.
isPost
?
"-post"
:
""
}
-entry.js`
,
)
,
makeHeader
(
actionTemplatePath
,
action
.
path
)
+
actionTemplate
.
replaceAll
(
"__ACTION__"
,
action
.
pascalCaseName
)
,
)
;
}
// Write a small stub for `upload-lib` that re-exports it from the shared bundle.
// External callers (e.g. internal testing environments) `require("./lib/upload-lib")`
// and expect the same shape as before, so we expose the namespace as `module.exports`.
const
uploadLibStubTemplatePath
=
"upload-lib-stub.js.tpl"
;
const
uploadLibStubTemplate
=
await
readFile
(
join
(
SRC_DIR
,
uploadLibStubTemplatePath
)
,
"utf-8"
,
)
;
await
writeFile
(
join
(
OUT_DIR
,
"upload-lib.js"
)
,
makeHeader
(
uploadLibStubTemplatePath
,
`
${
UPLOAD_LIB_SRC
}
.ts`
)
+
uploadLibStubTemplate
.
replaceAll
(
"__UPLOAD_LIB_EXPORT__"
,
UPLOAD_LIB_EXPORT
,
)
,
)
;
}
)
;
}
,
}
;
const
context
=
await
esbuild
.
context
(
{
entryPoints
:
[
{
in
:
SHARED_ENTRYPOINT
,
out
:
SHARED_ENTRYPOINT
}
]
,
bundle
:
true
,
format
:
"cjs"
,
outdir
:
OUT_DIR
,
platform
:
"node"
,
external
:
[
"./entry-points"
]
,
plugins
:
[
cleanPlugin
,
copyDefaultsPlugin
,
entryPointsPlugin
,
onEndPlugin
]
,
target
:
[
"node20"
]
,
define
:
{
__CODEQL_ACTION_VERSION__
:
JSON
.
stringify
(
pkg
.
version
)
,
__CODEQL_ACTION_TEST_ENV__
:
JSON
.
stringify
(
""
)
,
}
,
metafile
:
true
,
}
)
;
const
result
=
await
context
.
rebuild
(
)
;
await
writeFile
(
join
(
__dirname
,
"meta.json"
)
,
JSON
.
stringify
(
result
.
metafile
)
)
;
await
context
.
dispose
(
)
;
Back
|
FazBrowse Home
|
New Git URL