-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
491 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,136 @@ | ||
import { TConfig, TScript } from "./types"; | ||
import minimist from "minimist"; | ||
|
||
export function printHelp( | ||
config: TConfig, | ||
argv: minimist.ParsedArgs, | ||
args: unknown, | ||
): void { | ||
const helps: string[] = []; | ||
if (typeof args === "boolean") { | ||
if (!args) return; | ||
} else if (typeof args === "string" || typeof args === "number") { | ||
helps.push(`${args}`); | ||
} else if (Array.isArray(args)) { | ||
helps.push(...(args as string[])); | ||
} else { | ||
return; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
class Help { | ||
static printHelp( | ||
config: TConfig, | ||
argv: minimist.ParsedArgs, | ||
args: unknown, | ||
): void { | ||
const helps: string[] = []; | ||
if (typeof args === "boolean") { | ||
if (!args) return; | ||
} else if (typeof args === "string" || typeof args === "number") { | ||
helps.push(`${args}`); | ||
} else if (Array.isArray(args)) { | ||
helps.push(...(args as string[])); | ||
} else { | ||
return; | ||
} | ||
|
||
helps.push(...argv._); | ||
helps.push(...argv._); | ||
|
||
if (helps.length === 0) { | ||
//Printing all commands | ||
console.log(`\n\x1b[1mAvailable commands:\x1b[0m`); | ||
printCommands(config.scripts); | ||
} else { | ||
// TODO clean up this mess | ||
for (const help of helps) { | ||
console.log(`\n\x1b[1mAvailable commands: \x1b[90m${help}\x1b[0m`); | ||
const path = help.split("."); | ||
if (helps.length === 0) { | ||
//Printing all commands | ||
console.log(`\n\x1b[1mAvailable commands:\x1b[0m`); | ||
Help.printCommands(config.scripts); | ||
} else { | ||
// TODO clean up this mess | ||
for (const help of helps) { | ||
console.log(`\n\x1b[1mAvailable commands: \x1b[90m${help}\x1b[0m`); | ||
const path = help.split("."); | ||
|
||
const todo: [TScript, string[], string[]][] = [ | ||
[config.scripts, [], path], | ||
]; | ||
while (todo.length > 0) { | ||
const current = todo.shift(); | ||
if (!current) continue; | ||
const todo: [TScript, string[], string[]][] = [ | ||
[config.scripts, [], path], | ||
]; | ||
while (todo.length > 0) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const current = todo.shift()!; | ||
|
||
if (current[2].length === 0) { | ||
printCommands(current[0], current[1]); | ||
} else if (current[2][0] === "*") { | ||
if (typeof current[0] === "object") { | ||
if (Array.isArray(current[0])) { | ||
for (let i = 0; i < current[0].length; i++) { | ||
todo.push([current[0][i], [...current[1], i.toString()], []]); | ||
} | ||
} else { | ||
for (const key in current[0]) { | ||
if (Object.hasOwn(current[0], key)) { | ||
todo.push([ | ||
current[0][key], | ||
[...current[1], key], | ||
current[2].slice(1), | ||
]); | ||
if (current[2].length === 0) { | ||
Help.printCommands(current[0], current[1]); | ||
} else if (current[2][0] === "*") { | ||
if (typeof current[0] === "object") { | ||
if (Array.isArray(current[0])) { | ||
for (let i = 0; i < current[0].length; i++) { | ||
todo.push([current[0][i], [...current[1], i.toString()], []]); | ||
} | ||
} else { | ||
for (const key in current[0]) { | ||
if (Object.hasOwn(current[0], key)) { | ||
todo.push([ | ||
current[0][key], | ||
[...current[1], key], | ||
current[2].slice(1), | ||
]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
const sub = current[2][0]; | ||
} else { | ||
const sub = current[2][0]; | ||
|
||
if (typeof current[0] === "object") { | ||
if (Array.isArray(current[0])) { | ||
const i = parseInt(sub); | ||
todo.push([ | ||
current[0][i], | ||
[...current[1], i.toString()], | ||
current[2].slice(1), | ||
]); | ||
} else { | ||
todo.push([ | ||
current[0][sub], | ||
[...current[1], sub], | ||
current[2].slice(1), | ||
]); | ||
if (typeof current[0] === "object") { | ||
if (Array.isArray(current[0])) { | ||
const i = parseInt(sub); | ||
todo.push([ | ||
current[0][i], | ||
[...current[1], i.toString()], | ||
current[2].slice(1), | ||
]); | ||
} else { | ||
todo.push([ | ||
current[0][sub], | ||
[...current[1], sub], | ||
current[2].slice(1), | ||
]); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
process.exit(0); | ||
} | ||
process.exit(0); | ||
} | ||
|
||
export function printCommands( | ||
script: TScript | undefined, | ||
path: string[] = [], | ||
printSelf = true, | ||
) { | ||
if (Array.isArray(script)) { | ||
script.forEach((s, i) => { | ||
printCommand(s, [...path, i.toString()]); | ||
}); | ||
} else if (typeof script === "object") { | ||
if (printSelf && path.length > 0) printCommand(script, [...path]); | ||
for (const key in script) { | ||
if (Object.hasOwn(script, key)) { | ||
printCommand(script[key], [...path, key]); | ||
static printCommands( | ||
script: TScript | undefined, | ||
path: string[] = [], | ||
printSelf = true, | ||
) { | ||
if (Array.isArray(script)) { | ||
script.forEach((s, i) => { | ||
Help.printCommand(s, [...path, i.toString()]); | ||
}); | ||
} else if (typeof script === "object") { | ||
if (printSelf && path.length > 0) Help.printCommand(script, [...path]); | ||
for (const key in script) { | ||
if (Object.hasOwn(script, key)) { | ||
Help.printCommand(script[key], [...path, key]); | ||
} | ||
} | ||
} else { | ||
Help.printCommand(script, path); | ||
} | ||
} else { | ||
printCommand(script, path); | ||
} | ||
} | ||
|
||
export function printCommand(scripts: TScript | undefined, path: string[]) { | ||
if (scripts === undefined) { | ||
console.error(`\x1b[31mScript '${path.join(".")}' not found\x1b[0m`); | ||
return; | ||
} | ||
static printCommand(scripts: TScript | undefined, path: string[]) { | ||
if (scripts === undefined) { | ||
console.error(`\x1b[31mScript '${path.join(".")}' not found\x1b[0m`); | ||
return; | ||
} | ||
|
||
if (path[path.length - 1] === "$description") return; | ||
if (path[path.length - 1] === "$description") return; | ||
|
||
const prefix = `\x1b[92m${path.join(".")}\x1b[90m - \x1b[0m`; | ||
let suffix: string; | ||
if (typeof scripts === "string") { | ||
suffix = scripts; | ||
} else if (typeof scripts === "function") { | ||
suffix = "\x1b[90m<function>\x1b[0m"; | ||
} else if (Array.isArray(scripts)) { | ||
suffix = `\x1b[90m[${scripts.length}]\x1b[0m`; | ||
} else { | ||
if (Object.hasOwn(scripts, "$description")) { | ||
suffix = `\x1b[90m${scripts.$description as string}\x1b[0m`; | ||
const prefix = `\x1b[92m${path.join(".")}\x1b[90m - \x1b[0m`; | ||
let suffix: string; | ||
if (typeof scripts === "string") { | ||
suffix = scripts; | ||
} else if (typeof scripts === "function") { | ||
suffix = "\x1b[90m<function>\x1b[0m"; | ||
} else if (Array.isArray(scripts)) { | ||
suffix = `\x1b[90m[${scripts.length}]\x1b[0m`; | ||
} else { | ||
suffix = `\x1b[90m{${Object.keys(scripts).join(", ")}}\x1b[0m`; | ||
if (Object.hasOwn(scripts, "$description")) { | ||
suffix = `\x1b[90m${scripts.$description as string}\x1b[0m`; | ||
} else { | ||
suffix = `\x1b[90m{${Object.keys(scripts).join(", ")}}\x1b[0m`; | ||
} | ||
} | ||
} | ||
|
||
console.log(`${prefix}${suffix}`); | ||
console.log(`${prefix}${suffix}`); | ||
} | ||
} | ||
|
||
export { Help }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.