cli.ts 959 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { runOnRustCli } from '../helpers/rust-cli'
  2. interface Args {
  3. [key: string]: string | Object
  4. }
  5. interface Cmd {
  6. pid: number
  7. promise: Promise<void>
  8. }
  9. function toKebabCase(value: string): string {
  10. return value
  11. .replace(/([a-z])([A-Z])/g, '$1-$2')
  12. .replace(/\s+/g, '-')
  13. .toLowerCase()
  14. }
  15. function runCliCommand(command: string, args: Args): Cmd {
  16. const argsArray = []
  17. for (const [argName, argValue] of Object.entries(args)) {
  18. if (argValue === false) {
  19. continue
  20. }
  21. argsArray.push(`--${toKebabCase(argName)}`)
  22. if (argValue === true) {
  23. continue
  24. }
  25. argsArray.push(
  26. typeof argValue === 'string' ? argValue : JSON.stringify(argValue)
  27. )
  28. }
  29. return runOnRustCli(command, argsArray)
  30. }
  31. export const init = (args: Args): Cmd => runCliCommand('init', args)
  32. export const dev = (args: Args): Cmd => runCliCommand('dev', args)
  33. export const build = (args: Args): Cmd => runCliCommand('build', args)