Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7317236
generate-routes.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
generate-routes.ts
View Options
import
{
promises
as
fs
}
from
"fs"
;
import
{
relative
}
from
"path"
;
import
_locreq
from
"locreq"
;
import
{
walk
}
from
"./utils/walk"
;
import
{
importPath
}
from
"./utils/import-path"
;
import
{
assertType
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
unescape_url_params
}
from
"./utils/escape-url-params"
;
const
target_locreq
=
_locreq
(
process
.
cwd
());
async
function
extractActionName
(
full_file_path
:
string
)
:
Promise
<
string
>
{
const
file_content
=
await
fs
.
readFile
(
full_file_path
,
"utf-8"
);
const
result
=
file_content
.
match
(
/export const actionName = "(\w+)"/
);
if
(
result
===
null
)
{
throw
new
Error
(
"Missing 'export const actionName' statement?"
);
}
return
assertType
(
result
[
1
],
predicates
.
string
,
"Missing export default class?"
);
}
export
async
function
generateRoutes
()
:
Promise
<
void
>
{
const
files
=
await
Promise
.
all
(
(
await
walk
(
target_locreq
.
resolve
(
"src/back/routes"
))
)
.
filter
((
f
)
=>
f
.
endsWith
(
".page.ts"
)
||
f
.
endsWith
(
".form.ts"
))
.
map
(
async
(
fullpath
)
=>
({
fullpath
,
actionName
:
await
extractActionName
(
fullpath
),
url
:
unescape_url_params
(
"/"
+
relative
(
target_locreq
.
resolve
(
"src/back/routes"
),
fullpath
).
replace
(
/\..+/
,
""
)
),
}))
);
await
fs
.
writeFile
(
target_locreq
.
resolve
(
"src/back/routes/routes.ts"
),
`// DO NOT EDIT! This file is generated automaticaly with npm run generate-routes
import Router from "@koa/router";
import { Middlewares } from "sealious";
${
files
.
map
(
({
actionName
,
fullpath
}
) =>
`
import
{
default
as
$
{
actionName
}
}
from
"${importPath(
target_locreq.resolve("
src
/
back
/
routes
/
routes
.
ts
"),
fullpath
)}"
;
`
)
.join("\n")}
${
files
.
map
(({
actionName
,
url
}
) => `
export
const
$
{
actionName
}
URL
=
"${url}"
;
`)
.join("\n")}
export default function mountAutoRoutes(router: Router) {
${
files
.
map
(
({
actionName
,
}
) => `
router
.
use
(
$
{
actionName
}
URL
,
Middlewares
.
extractContext
(),
Middlewares
.
parseBody
());
$
{
actionName
}.
mount
(
router
,
$
{
actionName
}
URL
);
`
)
.join("\n\n")}
}
`
);
// eslint-disable-next-line no-console
console
.
log
(
"Successfuly generated new src/back/routes/routes.ts file"
);
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sun, Jul 13, 04:29 (1 d, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
815049
Default Alt Text
generate-routes.ts (2 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment