fix bug in bash script; read dev env var and generate apt script
MODIFY
packages/@romejs/cli-flags/Parser.ts
packages/@romejs/cli-flags/Parser.ts
@@ -21,7 +21,6 @@ import {
toKebabCase,
} from '@romejs/string-utils';
import {createUnknownFilePath} from '@romejs/path';
-
import {Dict} from '@romejs/typescript-helpers';
import {markup} from '@romejs/string-markup';
import {descriptions} from '@romejs/diagnostics';
@@ -376,6 +375,8 @@ export default class Parser<T> {
}
}
+ // i could add a flag for dev-rome itself
+ // i could take the input command name from the flag
const generateAutocomplete: undefined | SupportedAutocompleteShells = consumer.get(
'generateAutocomplete',
{
@@ -605,6 +606,7 @@ export default class Parser<T> {
// Execute all command defineFlags. Only one is usually ran when the arguments match the command name.
// But to generate autocomplete we want all the flags to be declared for all commands.
+
const flags = this.getFlagsConsumer();
for (const command of this.commands.values()) {
// capture() will cause diagnostics to be suppressed
@@ -612,21 +614,23 @@ export default class Parser<T> {
await this.defineCommandFlags(command, consumer);
}
+ const prg = process.env.ROME_DEV ? 'dev-rome' : 'rome';
+
switch (shell) {
case 'bash': {
- reporter.logAllNoMarkup(this.genBashCompletions());
+ reporter.logAllNoMarkup(this.genBashCompletions(prg));
break;
}
case 'fish': {
- reporter.logAllNoMarkup(this.genFishCompletions());
+ reporter.logAllNoMarkup(this.genFishCompletions(prg));
break;
}
}
}
- genFishCompletions(): string {
+ genFishCompletions(prg: string): string {
let script = '';
- const scriptPre = 'complete -c rome';
+ const scriptPre = `complete -c ${prg}`;
// add rome
script += `${scriptPre} -f\n`;
@@ -648,7 +652,7 @@ export default class Parser<T> {
return script;
}
- genBashCompletions(): string {
+ genBashCompletions(prg: string): string {
let romeCmds = '';
let commandFuncs = '';
let globalFlags = '';
@@ -674,24 +678,25 @@ export default class Parser<T> {
}
for (let [cmd, flags] of cmdFlagMap.entries()) {
- commandFuncs += dedent`__rome_${cmd}()
+ commandFuncs += `
+ __${prg}_${cmd}()
{
cmds="";
local_flags="${flags}"
}
-
`;
}
- let romeFunc = dedent`__rome()
+ let romeFunc = `
+ __${prg}()
{
cmds="${romeCmds}"
local_flags="";
}
-
`;
- return dedent`#!/usr/bin/env bash
+ let mainScript = `
+ #!/usr/bin/env bash
global_flags="${globalFlags}"
# initial state
@@ -705,11 +710,11 @@ export default class Parser<T> {
esac
}
- __rome_gen_completions()
+ __${prg}_gen_completions()
{
local suggestions func flags index
- index="$(($\{#COMP_WORDS[@]} - 1))"
+ index="$((\${#COMP_WORDS[@]} - 1))"
flags="$global_flags $local_flags"
@@ -717,25 +722,29 @@ export default class Parser<T> {
for ((i=0; i < index; i++))
do
- if [[ ! $(__is_flag $\{COMP_WORDS[$i]}) ]]; then
- func="$\{func}_$\{COMP_WORDS[$i]}"
+ leaf=$(echo \${COMP_WORDS[$i]} | grep -o '[^/]*$')
+ if [[ ! $(__is_flag $leaf) ]]; then
+ func="\${func}_\${leaf}"
fi
done
$func 2> /dev/null
- if [[ $(__is_flag $\{COMP_WORDS[$index]}) ]]; then
+ if [[ $(__is_flag \${COMP_WORDS[$index]}) ]]; then
suggestions=$flags
else
suggestions=$cmds
fi
- COMPREPLY=($(compgen -W "$\{suggestions}" -- "$\{COMP_WORDS[$index]}"))
+ COMPREPLY=($(compgen -W "$suggestions" -- "\${COMP_WORDS[$index]}"))
}
+ `;
+ return dedent`
+ ${mainScript}
${commandFuncs}
${romeFunc}
- complete -F __rome_gen_completions rome
+ complete -F __${prg}_gen_completions ${prg}
`;
}

ADD
result
+#!/usr/bin/env bash
+global_flags="--version --generateAutocomplete "
+
+# initial state
+cmds=""
+local_flags=""
+
+__is_flag()
+{
+ case $1 in
+ -*) echo "true"
+ esac
+}
+
+__dev-rome_gen_completions()
+{
+ local suggestions func flags index
+
+ index="$((${#COMP_WORDS[@]} - 1))"
+
+ flags="$global_flags $local_flags"
+
+ func="_"
+
+ for ((i=0; i < index; i++))
+ do
+ leaf=$(echo ${COMP_WORDS[$i]} | grep -o '[^/]*$')
+ if [[ ! $(__is_flag $leaf) ]]; then
+ func="${func}_${leaf}"
+ fi
+ done
+
+ $func 2> /dev/null
+
+ if [[ $(__is_flag ${COMP_WORDS[$index]}) ]]; then
+ suggestions=$flags
+ else
+ suggestions=$cmds
+ fi
+
+ COMPREPLY=($(compgen -W "$suggestions" -- "${COMP_WORDS[$index]}"))
+}
+
+
+__dev-rome_init()
+{
+ cmds="";
+ local_flags="--defaults"
+}
+
+__dev-rome_test()
+{
+ cmds="";
+ local_flags="--coverage --showAllCoverage --updateSnapshots --freezeSnapshots --focusAllowed --syncTests"
+}
+
+__dev-rome_lint()
+{
+ cmds="";
+ local_flags="--save --formatOnly --changed"
+}
+
+__dev-rome_bundle()
+{
+ cmds="";
+ local_flags="--quiet"
+}
+
+__dev-rome_parse()
+{
+ cmds="";
+ local_flags="--allowDiagnostics --compact --sourceType"
+}
+
+__dev-rome_analyzeDependencies()
+{
+ cmds="";
+ local_flags="--compact --focusSource"
+}
+
+__dev-rome_compile()
+{
+ cmds="";
+ local_flags="--bundle"
+}
+
+__dev-rome_ci()
+{
+ cmds="";
+ local_flags="--fix"
+}
+
+__dev-rome_format()
+{
+ cmds="";
+ local_flags="--allowDiagnostics"
+}
+
+
+__dev-rome()
+{
+ cmds="init start develop stop run restart status lsp test lint config bundle parse analyzeDependencies resolve compile publish ci format _evict _moduleSignature _noop rage logs "
+ local_flags="";
+}
+
+complete -F __dev-rome_gen_completions dev-rome

fix bug in bash script; read dev env var and generate apt script