Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F996313
add-route.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
add-route.ts
View Options
import
{
existsSync
,
promises
as
fs
}
from
"fs"
;
import
_locreq
from
"locreq"
;
import
{
resolve
}
from
"path"
;
import
{
generateRoutes
}
from
"./generate-routes.js"
;
import
{
testTemplate
}
from
"./templates/route-test.js"
;
import
{
escape_url_params
}
from
"./utils/escape-url-params.js"
;
import
prompts
from
"prompts"
;
import
{
Templates
}
from
"./templates/templates.js"
;
import
{
toKebabCase
,
toPascalCase
}
from
"js-convert-case"
;
export
async
function
addRoute
(
params
:
Partial
<
{
[
key
in
"action"
|
"url"
|
"mode"
]
:
string
}
>
,
app_directory
:
string
=
process
.
cwd
()
)
:
Promise
<
void
>
{
const
target_locreq
=
_locreq
(
app_directory
);
prompts
.
override
(
params
);
const
response
=
await
prompts
([
{
type
:
"text"
,
name
:
"action"
,
message
:
'What\'s the name of the action (e.g. "AddUser"): '
,
validate
:
(
s
:
string
)
=>
s
.
length
>
3
?
true
:
"Should be at least 3 characters long"
,
format
:
function
(
action
:
string
)
{
return
toPascalCase
(
action
);
},
},
{
type
:
"text"
,
name
:
"url"
,
message
:
"Enter a full absolute path for the new route (for example: /admin/users/:id/edit): "
,
validate
:
(
s
:
string
)
=>
s
.
trim
()[
0
]
==
"/"
?
true
:
"Should start with a '/'"
,
initial
:
function
(
_
,
{
action
}
:
{
action
:
string
})
{
return
"/"
+
toKebabCase
(
action
);
},
},
{
type
:
"select"
,
name
:
"mode"
,
message
:
"What kind of route is it?"
,
choices
:
[
{
title
:
"page"
,
value
:
"page"
},
{
title
:
"stateful page"
,
value
:
"sreact"
},
{
title
:
"form"
,
value
:
"form"
},
{
title
:
"list"
,
value
:
"list"
},
{
title
:
"multiform"
,
value
:
"multiform"
},
{
title
:
"redirect"
,
value
:
"redirect"
},
{
title
:
"raw POST"
,
value
:
"post"
},
{
title
:
"Long Running Process status page"
,
value
:
"lpr"
},
],
},
]);
const
{
action
,
url
,
mode
}
=
response
as
Record
<
string
,
string
>
;
const
extension
=
[
"list"
,
"lpr"
,
"page"
,
"sreact"
].
includes
(
mode
)
?
"tsx"
:
"ts"
;
const
base_file_name
=
escape_url_params
(
url
)
||
"index"
;
const
file_path
=
target_locreq
.
resolve
(
`src/back/routes/
${
base_file_name
}
.
${
mode
}
.
${
extension
}
`
);
const
test_file_path
=
target_locreq
.
resolve
(
`src/back/routes/
${
base_file_name
}
.test.ts`
);
const
template
=
Templates
[
mode
as
keyof
typeof
Templates
];
if
(
!
template
)
{
throw
new
Error
(
`Could not get template
${
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
));
if
(
mode
!==
"lpr"
)
{
await
fs
.
writeFile
(
test_file_path
,
await
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
Tue, Dec 24, 14:02 (16 h, 32 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557172
Default Alt Text
add-route.ts (2 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment