Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DojoCLI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dojo Project (HES-SO)
Projects
UI
DojoCLI
Commits
042edc8d
Verified
Commit
042edc8d
authored
1 year ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
convert to string instead of console
parent
4a879744
No related branches found
No related tags found
1 merge request
!8
Draft: Resolve "Add bash completion helper function"
Pipeline
#29348
passed
1 year ago
Stage: code_quality
Stage: test
Changes
2
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
NodeApp/src/commander/CommanderApp.ts
+1
-1
1 addition, 1 deletion
NodeApp/src/commander/CommanderApp.ts
NodeApp/src/helpers/AutoCompletionHelper.ts
+49
-49
49 additions, 49 deletions
NodeApp/src/helpers/AutoCompletionHelper.ts
with
50 additions
and
50 deletions
NodeApp/src/commander/CommanderApp.ts
+
1
−
1
View file @
042edc8d
...
...
@@ -44,7 +44,7 @@ class CommanderApp {
this
.
registerCommands
();
getBashCompletion
(
this
.
program
)
getBashCompletion
(
this
.
program
,
"
bash_completion.sh
"
)
this
.
program
.
parse
();
}
...
...
This diff is collapsed.
Click to expand it.
NodeApp/src/helpers/AutoCompletionHelper.ts
+
49
−
49
View file @
042edc8d
import
{
Command
}
from
'
commander
'
;
import
{
writeFileSync
}
from
'
fs
'
;
interface
Node
{
readonly
name
:
string
;
...
...
@@ -19,20 +19,6 @@ function isLeaf(tree: Node): boolean {
return
tree
.
children
===
undefined
||
tree
.
children
.
length
==
0
}
function
getOptions
(
cmd
:
Command
):
Array
<
Node
>
{
return
cmd
.
options
.
filter
(
o
=>
!
o
.
hidden
).
map
(
o
=>
createNode
(
o
.
flags
.
replace
(
/<.*
?
>/
,
''
).
replace
(
/
\[
.*
?\]
/
,
''
).
replace
(
'
,
'
,
''
).
trimEnd
(),
[],
cmd
.
name
())
)
}
function
getSubNode
(
cmd
:
Command
,
parent
:
Command
):
Node
{
if
(
cmd
.
commands
.
length
>
0
)
{
return
createNode
(
cmd
.
name
(),
cmd
.
commands
.
map
((
sc
)
=>
getSubNode
(
sc
,
cmd
)).
concat
(
getOptions
(
cmd
)),
parent
.
name
())
}
else
{
return
createNode
(
cmd
.
name
(),
getOptions
(
cmd
),
parent
.
name
())
}
}
function
search
(
tree
:
Node
,
nodes
:
Array
<
Node
>
,
cmdName
:
string
):
Array
<
Node
>
{
if
(
isLeaf
(
tree
))
{
if
(
tree
.
name
!=
cmdName
)
{
...
...
@@ -59,25 +45,39 @@ function flatten(tree: Node): Node[] {
}
}
function
addWords
(
cmd
:
Node
,
prefix
:
string
):
string
{
return
prefix
+
"
words=
\"
"
+
cmd
.
children
.
map
(
c
=>
c
.
name
).
join
(
"
"
)
+
"
--help -h
"
+
"
\"
"
return
prefix
+
"
words=
\"
"
+
cmd
.
children
.
map
(
c
=>
c
.
name
).
join
(
"
"
)
+
"
--help -h
"
+
"
\"
\n
"
}
function
printWordsGrandParents
(
cmd
:
Node
)
{
function
printWordsGrandParents
(
node
:
Node
):
string
{
let
parentWords
=
""
let
prefix
=
""
if
(
cmd
.
parent
!==
undefined
)
{
console
.
log
(
"
case
\"
${grand_parent}
\"
in
"
)
console
.
log
(
"
"
+
cmd
.
parent
+
"
)
"
)
if
(
node
.
parent
!==
undefined
)
{
parentWords
+=
"
case
\"
${grand_parent}
\"
in
\n
"
parentWords
+=
"
"
+
node
.
parent
+
"
)
\n
"
prefix
=
"
"
}
const
words
=
addWords
(
cmd
,
prefix
)
console
.
log
(
words
)
if
(
cmd
.
parent
!==
undefined
)
{
console
.
log
(
"
;;
"
)
console
.
log
(
"
*)
"
)
console
.
log
(
"
;;
"
)
console
.
log
(
"
esac
"
)
parentWords
+=
addWords
(
node
,
prefix
)
if
(
node
.
parent
!==
undefined
)
{
parentWords
+=
"
;;
\n
"
parentWords
+=
"
*)
\n
"
parentWords
+=
"
;;
\n
"
parentWords
+=
"
esac
\n
"
}
return
parentWords
}
function
getOptions
(
cmd
:
Command
):
Array
<
Node
>
{
return
cmd
.
options
.
filter
(
o
=>
!
o
.
hidden
).
map
(
o
=>
createNode
(
o
.
flags
.
replace
(
/<.*
?
>/
,
''
).
replace
(
/
\[
.*
?\]
/
,
''
).
replace
(
'
,
'
,
''
).
trimEnd
(),
[],
cmd
.
name
())
)
}
function
getSubNode
(
cmd
:
Command
,
parent
:
Command
):
Node
{
if
(
cmd
.
commands
.
length
>
0
)
{
return
createNode
(
cmd
.
name
(),
cmd
.
commands
.
map
((
sc
)
=>
getSubNode
(
sc
,
cmd
)).
concat
(
getOptions
(
cmd
)),
parent
.
name
())
}
else
{
return
createNode
(
cmd
.
name
(),
getOptions
(
cmd
),
parent
.
name
())
}
}
...
...
@@ -85,34 +85,34 @@ function buildCommandNode(root: Command): Node {
return
createNode
(
root
.
name
(),
root
.
commands
.
map
((
sc
)
=>
getSubNode
(
sc
,
root
)).
concat
(
getOptions
(
root
)))
}
export
function
getBashCompletion
(
root
:
Command
)
{
export
function
getBashCompletion
(
root
:
Command
,
filename
:
string
)
{
const
tree
=
buildCommandNode
(
root
)
console
.
log
(
"
#/usr/bin/env bash
"
)
console
.
log
(
"
function _dojo_completions()
"
)
console
.
log
(
"
{
"
)
console
.
log
(
"
latest=
\"
${COMP_WORDS[$COMP_CWORD]}
\"
"
)
console
.
log
(
"
parent=
\"
${COMP_WORDS[$COMP_CWORD - 1]}
\"
"
)
console
.
log
(
"
grand_parent=
\"
${COMP_WORDS[$COMP_CWORD - 2]}
\"
"
)
console
.
log
(
"
words=
\"\"
"
)
console
.
log
(
"
case
\"
${parent}
\"
in
"
)
let
data
=
"
#/usr/bin/env bash
\n
function _dojo_completions()
\n
"
;
data
+=
"
{
\n
"
data
+=
"
latest=
\"
${COMP_WORDS[$COMP_CWORD]}
\"\n
"
data
+=
"
parent=
\"
${COMP_WORDS[$COMP_CWORD - 1]}
\"\n
"
data
+=
"
grand_parent=
\"
${COMP_WORDS[$COMP_CWORD - 2]}
\"\n
"
data
+=
"
words=
\"\"\n
"
data
+=
"
case
\"
${parent}
\"
in
\n
"
const
commands
=
Array
.
from
(
new
Set
(
flatten
(
tree
).
filter
(
t
=>
!
isLeaf
(
t
)).
map
(
n
=>
n
.
name
))).
map
(
s
=>
search
(
tree
,
[],
s
))
for
(
const
node
of
commands
)
{
const
cmd
=
node
[
0
]
console
.
log
(
"
"
+
cmd
.
name
+
"
)
"
)
data
+=
"
"
+
cmd
.
name
+
"
)
\n
"
if
(
cmd
!==
undefined
&&
node
.
length
==
1
)
{
const
words
=
addWords
(
cmd
,
""
)
console
.
log
(
words
)
data
+=
addWords
(
cmd
,
""
)
}
else
if
(
cmd
!==
undefined
&&
node
.
length
>
1
)
{
node
.
forEach
(
n
=>
printWordsGrandParents
(
n
))
node
.
forEach
(
n
=>
data
+=
printWordsGrandParents
(
n
))
}
console
.
log
(
"
;;
"
)
data
+=
"
;;
\n
"
}
console
.
log
(
"
*)
"
)
console
.
log
(
"
;;
"
)
console
.
log
(
"
esac
"
)
console
.
log
(
"
COMPREPLY=($(compgen -W
\"
$words
\"
-- $latest))
"
)
console
.
log
(
"
return 0
"
)
console
.
log
(
"
}
"
)
console
.
log
(
"
complete -F _dojo_completions dojo
"
)
data
+=
"
*)
\n
"
data
+=
"
;;
\n
"
data
+=
"
esac
\n
"
data
+=
"
COMPREPLY=($(compgen -W
\"
$words
\"
-- $latest))
\n
"
data
+=
"
return 0
\n
"
data
+=
"
}
\n
"
data
+=
"
complete -F _dojo_completions dojo
\n
"
writeFileSync
(
filename
,
data
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment