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
ce88a755
Verified
Commit
ce88a755
authored
1 year ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
removed cmdNodes which were useless
parent
304e0953
No related branches found
No related tags found
1 merge request
!8
Draft: Resolve "Add bash completion helper function"
Pipeline
#29373
passed
1 year ago
Stage: code_quality
Stage: test
Changes
1
Pipelines
2
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
NodeApp/src/helpers/AutoCompletionHelper.ts
+29
-52
29 additions, 52 deletions
NodeApp/src/helpers/AutoCompletionHelper.ts
with
29 additions
and
52 deletions
NodeApp/src/helpers/AutoCompletionHelper.ts
+
29
−
52
View file @
ce88a755
...
@@ -79,84 +79,62 @@ import { writeFileSync } from 'fs';
...
@@ -79,84 +79,62 @@ import { writeFileSync } from 'fs';
// return 0
// return 0
// }
// }
interface
CmdNode
{
function
isLeaf
(
cmd
:
Command
):
boolean
{
readonly
name
:
string
;
return
cmd
.
commands
.
length
==
0
readonly
children
:
Array
<
CmdNode
>
;
readonly
parent
?:
string
;
}
}
function
createCmdNode
(
name
:
string
,
children
:
Array
<
CmdNode
>
,
parent
?:
string
):
CmdNode
{
function
search
(
cmd
:
Command
,
cmdName
:
string
):
Array
<
Command
>
{
return
{
if
(
isLeaf
(
cmd
))
{
name
,
if
(
cmd
.
name
()
!=
cmdName
)
{
children
,
parent
};
}
function
isLeaf
(
tree
:
CmdNode
):
boolean
{
return
tree
.
children
===
undefined
||
tree
.
children
.
length
==
0
}
function
search
(
tree
:
CmdNode
,
cmdName
:
string
):
Array
<
CmdNode
>
{
if
(
isLeaf
(
tree
))
{
if
(
tree
.
name
!=
cmdName
)
{
return
[]
return
[]
}
else
{
}
else
{
return
[
tree
]
return
[
cmd
]
}
}
}
else
if
(
tree
.
name
==
cmdName
)
{
}
else
if
(
cmd
.
name
()
==
cmdName
)
{
return
tree
.
children
.
flatMap
(
st
=>
search
(
st
,
cmdName
)).
concat
(
tree
)
return
cmd
.
commands
.
flatMap
(
st
=>
search
(
st
,
cmdName
)).
concat
(
cmd
)
}
else
{
}
else
{
return
tree
.
children
.
flatMap
(
st
=>
search
(
st
,
cmdName
))
return
cmd
.
commands
.
flatMap
(
st
=>
search
(
st
,
cmdName
))
}
}
}
}
function
flatten
(
tree
:
CmdNode
):
Array
<
C
mdNode
>
{
function
flatten
(
cmd
:
Command
):
Array
<
C
ommand
>
{
if
(
isLeaf
(
tree
))
{
if
(
isLeaf
(
cmd
))
{
return
[
tree
]
return
[
cmd
]
}
else
{
}
else
{
return
tree
.
children
return
cmd
.
commands
.
map
(
child
=>
flatten
(
child
))
.
map
(
child
=>
flatten
(
child
))
.
reduce
((
acc
,
cmd
)
=>
acc
.
concat
(
cmd
),
[
tree
])
.
reduce
((
acc
,
cmd
)
=>
acc
.
concat
(
cmd
),
[
cmd
])
}
}
}
}
function
addWordsGrandParents
(
cmd
:
C
mdNode
):
string
{
function
addWordsGrandParents
(
cmd
:
C
ommand
):
string
{
let
parentWords
=
""
let
parentWords
=
""
let
ident
=
3
let
ident
=
3
if
(
cmd
.
parent
!==
undefined
)
{
if
(
cmd
.
parent
!==
null
)
{
parentWords
+=
openCase
(
ident
,
'
grand_parent
'
)
parentWords
+=
openCase
(
ident
,
'
grand_parent
'
)
ident
+=
1
ident
+=
1
parentWords
+=
addLine
(
ident
,
`
${
cmd
.
parent
}
)`
)
parentWords
+=
addLine
(
ident
,
`
${
cmd
.
parent
.
name
()
}
)`
)
ident
+=
1
ident
+=
1
}
}
parentWords
+=
addLine
(
ident
,
`words="
${
c
md
.
children
.
map
(
c
=>
c
.
name
).
join
(
"
"
)}
--help -h
"`
)
parentWords
+=
addLine
(
ident
,
`words="
${
c
ommandsAndOptionsToString
(
cmd
)}
"`
)
ident
-=
1
ident
-=
1
parentWords
+=
addLine
(
ident
,
'
;;
'
)
parentWords
+=
addLine
(
ident
,
'
;;
'
)
if
(
cmd
.
parent
!==
undefined
)
{
if
(
cmd
.
parent
!==
null
)
{
ident
-=
1
ident
-=
1
parentWords
+=
`
${
closeCase
(
ident
)}
`
parentWords
+=
`
${
closeCase
(
ident
)}
`
}
}
return
parentWords
return
parentWords
}
}
function
getOptions
(
cmd
:
Command
):
Array
<
CmdNode
>
{
function
getOptions
(
cmd
:
Command
):
string
{
// we remove <args>, [command], and , from option lines
// we remove <args>, [command], and , from option lines
return
cmd
.
options
.
filter
(
opt
=>
!
opt
.
hidden
).
map
(
opt
=>
return
cmd
.
options
.
filter
(
opt
=>
!
opt
.
hidden
).
map
(
opt
=>
createCmdNode
(
opt
.
flags
.
replace
(
/<.*
?
>/
,
''
).
replace
(
/
\[
.*
?\]
/
,
''
).
replace
(
'
,
'
,
''
).
trimEnd
()
,
[],
cmd
.
name
())
opt
.
flags
.
replace
(
/<.*
?
>/
,
''
).
replace
(
/
\[
.*
?\]
/
,
''
).
replace
(
'
,
'
,
''
).
trimEnd
()
)
)
.
join
(
"
"
)
}
}
function
getSubCmdNode
(
cmd
:
Command
,
parent
:
Command
):
CmdNode
{
function
commandsAndOptionsToString
(
cmd
:
Command
):
string
{
if
(
cmd
.
commands
.
length
>
0
)
{
return
cmd
.
commands
.
map
(
c
=>
c
.
name
()).
join
(
"
"
).
concat
(
'
'
+
getOptions
(
cmd
)).
trim
().
concat
(
'
--help -h
'
).
trim
()
return
createCmdNode
(
cmd
.
name
(),
cmd
.
commands
.
map
(
subCmd
=>
getSubCmdNode
(
subCmd
,
cmd
)).
concat
(
getOptions
(
cmd
)),
parent
.
name
())
}
else
{
return
createCmdNode
(
cmd
.
name
(),
getOptions
(
cmd
),
parent
.
name
())
}
}
function
buildCmdNode
(
root
:
Command
):
CmdNode
{
return
createCmdNode
(
root
.
name
(),
root
.
commands
.
map
(
subCmd
=>
getSubCmdNode
(
subCmd
,
root
)).
concat
(
getOptions
(
root
)))
}
}
function
openCase
(
identLevel
:
number
,
pattern
:
string
):
string
{
function
openCase
(
identLevel
:
number
,
pattern
:
string
):
string
{
...
@@ -174,20 +152,19 @@ function addLine(identLevel: number, pattern: string): string {
...
@@ -174,20 +152,19 @@ function addLine(identLevel: number, pattern: string): string {
}
}
export
function
getBashCompletion
(
root
:
Command
,
filename
:
string
)
{
export
function
getBashCompletion
(
root
:
Command
,
filename
:
string
)
{
const
tree
=
buildCmdNode
(
root
)
let
data
=
let
data
=
`
${
addLine
(
0
,
'
#/usr/bin/env bash
\n
function _dojo_completions()
'
)}
`
`
${
addLine
(
0
,
'
#/usr/bin/env bash
\n
function _dojo_completions()
'
)}
`
+
`
${
addLine
(
0
,
'
{
'
)}${
addLine
(
1
,
'
latest="${COMP_WORDS[$COMP_CWORD]}"
'
)}
`
+
`
${
addLine
(
0
,
'
{
'
)}${
addLine
(
1
,
'
latest="${COMP_WORDS[$COMP_CWORD]}"
'
)}
`
+
`
${
addLine
(
1
,
'
parent="${COMP_WORDS[$COMP_CWORD - 1]}"
'
)}
`
+
`
${
addLine
(
1
,
'
parent="${COMP_WORDS[$COMP_CWORD - 1]}"
'
)}
`
+
`
${
addLine
(
1
,
'
grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"
'
)}
`
+
`
${
addLine
(
1
,
'
grand_parent="${COMP_WORDS[$COMP_CWORD - 2]}"
'
)}
`
+
`
${
addLine
(
1
,
'
words=""
'
)}${
openCase
(
1
,
"
parent
"
)}
`
+
`
${
addLine
(
1
,
'
words=""
'
)}
`
const
commands
=
Array
.
from
(
new
Set
(
flatten
(
tree
).
filter
(
cmd
=>
!
isLeaf
(
cmd
)).
map
(
cmd
=>
cmd
.
name
))).
map
(
name
=>
search
(
tree
,
name
))
+
`
${
openCase
(
1
,
"
parent
"
)}
`
const
commands
=
Array
.
from
(
new
Set
(
flatten
(
root
).
map
(
cmd
=>
cmd
.
name
()))).
map
(
name
=>
search
(
root
,
name
))
for
(
const
cmdNode
of
commands
)
{
for
(
const
cmdNode
of
commands
)
{
const
cmd
=
cmdNode
[
0
]
const
cmd
=
cmdNode
[
0
]
data
+=
addLine
(
2
,
`
${
cmd
.
name
}
)`
)
data
+=
addLine
(
2
,
`
${
cmd
.
name
()
}
)`
)
if
(
cmd
!==
undefined
&&
cmdNode
.
length
==
1
)
{
if
(
cmd
!==
undefined
&&
cmdNode
.
length
==
1
)
{
data
+=
addLine
(
3
,
`words="
${
c
md
.
children
.
map
(
c
=>
c
.
name
).
join
(
"
"
)}
--help -h
"`
)
data
+=
addLine
(
3
,
`words="
${
c
ommandsAndOptionsToString
(
cmd
)}
"`
)
}
else
if
(
cmd
!==
undefined
&&
cmdNode
.
length
>
1
)
{
}
else
if
(
cmd
!==
undefined
&&
cmdNode
.
length
>
1
)
{
cmdNode
.
forEach
(
n
=>
data
+=
addWordsGrandParents
(
n
))
cmdNode
.
forEach
(
n
=>
data
+=
addWordsGrandParents
(
n
))
}
}
...
...
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