Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F3893797
add-route.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
add-route.ts
View Options
import
{
resolve
}
from
"path"
;
import
{
promises
as
fs
,
existsSync
}
from
"fs"
;
import
{
question
}
from
"./utils/question"
;
import
{
pageTemplate
}
from
"./templates/page"
;
import
{
formTemplate
}
from
"./templates/form"
;
import
{
testTemplate
}
from
"./templates/route-test"
;
import
{
escape_url_params
}
from
"./utils/escape-url-params"
;
import
_locreq
from
"locreq"
;
import
{
generateRoutes
}
from
"./generate-routes"
;
import
{
listTemplate
}
from
"./templates/list"
;
const
target_locreq
=
_locreq
(
process
.
cwd
());
export
async
function
addRoute
()
:
Promise
<
void
>
{
const
action
=
await
question
(
'What\'s the name of the action (e.g. "AddUser"): '
,
(
s
:
string
)
=>
s
.
length
>
3
);
const
url
=
await
question
(
"Enter a full absolute path for the new route (e.g. /admin/users/add): "
,
(
s
:
string
)
=>
s
.
trim
()[
0
]
==
"/"
);
const
mode_answer
=
(
await
question
(
"Is this a page (p), a form (f), or a list (l)? "
,
(
a
:
string
)
=>
[
"p"
,
"f"
,
"l"
].
includes
(
a
.
toLowerCase
())
))
as
"p"
|
"f"
|
"l"
;
const
mode
=
(
<
const
>
{
p
:
"page"
,
f
:
"form"
,
l
:
"list"
})[
mode_answer
];
const
file_path
=
target_locreq
.
resolve
(
`src/back/routes/
${
escape_url_params
(
url
)
}
.
${
mode
}
.ts`
);
const
test_file_path
=
target_locreq
.
resolve
(
`src/back/routes/
${
escape_url_params
(
url
)
}
.test.ts`
);
const
template
=
{
page
:
pageTemplate
,
form
:
formTemplate
,
list
:
listTemplate
,
}[
mode
];
if
(
existsSync
(
file_path
))
{
// eslint-disable-next-line no-console
console
.
error
(
`ERROR: File
${
file_path
}
already exists.`
);
return
;
}
await
fs
.
mkdir
(
resolve
(
file_path
,
"../"
),
{
recursive
:
true
});
await
fs
.
writeFile
(
file_path
,
await
template
(
action
,
file_path
));
await
fs
.
writeFile
(
test_file_path
,
testTemplate
(
action
,
file_path
));
// eslint-disable-next-line no-console
console
.
log
(
`
${
file_path
}
created`
);
// eslint-disable-next-line no-console
console
.
log
(
`
${
test_file_path
}
created`
);
await
generateRoutes
();
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Mon, May 19, 00:55 (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
635114
Default Alt Text
add-route.ts (1 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment