Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F969575
index.ts
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
index.ts
View Options
#!/usr/bin/env node
import
yargs
from
"yargs/yargs"
;
import
{
ChildProcess
,
spawn
}
from
"child_process"
;
import
_locreq
from
"locreq"
;
import
{
hasShape
,
is
}
from
"@sealcode/ts-predicates"
;
import
{
cwd
}
from
"node:process"
;
const
locreq
=
_locreq
(
cwd
());
import
{
PackageJsonShape
,
TmuxScriptsPredicate
}
from
"./types/PackageJson"
;
const
defaultCommandLineDesignOptions
=
"tmux select-layout even-horizontal && tmux set -g pane-border-status top && tmux set -g mouse on && tmux attach"
;
const
parser
=
yargs
(
process
.
argv
.
slice
(
2
)).
options
({
p
:
{
alias
:
"scriptKey"
,
describe
:
"Name of the script key"
,
type
:
"string"
,
},
});
function
consoleChildProcessError
(
child
:
ChildProcess
)
{
child
.
on
(
"error"
,
(
err
)
=>
{
console
.
error
(
`There is an error
${
err
.
message
}
`
);
});
}
function
getFinalCommand
(
commands
:
string
[])
{
const
tmuxCommand
=
commands
.
map
((
script
,
index
)
=>
{
const
command
=
index
===
0
?
"new-session"
:
"split-window"
;
const
windowTitle
=
script
.
split
(
" "
)[
2
];
const
setTitle
=
`printf '\\033]2;%s\\033\\' '
${
windowTitle
}
'`
;
return
`tmux
${
command
}
-d "
${
setTitle
}
&&
${
script
}
"`
;
})
.
join
(
" && "
);
const
finalCommand
=
`
${
tmuxCommand
}
&&
${
defaultCommandLineDesignOptions
}
`
;
return
finalCommand
;
}
function
getChild
(
finalCommand
:
string
)
{
return
spawn
(
finalCommand
,
{
shell
:
true
,
stdio
:
"inherit"
,
});
}
async
function
main
()
{
const
argv
=
await
parser
.
argv
;
if
(
Object
.
keys
(
argv
).
length
===
2
&&
!
argv
.
_
.
length
)
{
console
.
error
(
"No script key or custom scripts provided"
);
process
.
exit
(
0
);
}
if
(
argv
.
scriptKey
)
{
if
(
argv
.
_
.
length
>
0
)
{
console
.
error
(
"No additional arguments are allowed when using -p."
);
process
.
exit
(
1
);
}
const
packageJsonData
:
unknown
=
locreq
(
"package.json"
)
as
unknown
;
const
scriptKey
=
argv
.
scriptKey
as
string
;
if
(
!
hasShape
(
PackageJsonShape
,
packageJsonData
))
{
throw
new
Error
(
"The package.json does not have the expected tmux-scripts."
);
}
const
tmuxScripts
:
Record
<
string
,
string
[]
>
=
packageJsonData
[
"tmux-scripts"
];
if
(
!
is
(
tmuxScripts
,
TmuxScriptsPredicate
))
{
throw
new
Error
(
"The object tmux-scripts does not have the expected shape."
);
}
if
(
!
tmuxScripts
[
scriptKey
])
{
console
.
error
(
`Script with key '
${
scriptKey
}
' not found in tmux-scripts`
);
process
.
exit
(
0
);
}
console
.
log
(
`Running script with key:
${
scriptKey
}
`
);
const
commands
=
tmuxScripts
[
scriptKey
];
const
finalCommand
=
getFinalCommand
(
commands
);
const
child
=
getChild
(
finalCommand
);
consoleChildProcessError
(
child
);
}
else
if
(
argv
.
_
.
length
>
0
)
{
const
commands
=
argv
.
_
as
string
[];
console
.
log
(
`Running custom scripts:
${
commands
.
join
(
", "
)
}
`
);
const
finalCommand
=
getFinalCommand
(
commands
);
const
child
=
getChild
(
finalCommand
);
consoleChildProcessError
(
child
);
}
}
void
main
().
catch
((
error
)
=>
{
console
.
error
(
`An error occurred:
${
error
}
`
);
process
.
exit
(
1
);
});
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 23, 06:15 (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
547553
Default Alt Text
index.ts (2 KB)
Attached To
Mode
rMULT multiple-scripts-tmux
Attached
Detach File
Event Timeline
Log In to Comment