unjs / giget

โœจ Download templates and git repositories with pleasure!
MIT License
446 stars 37 forks source link

refactor: use citty for cli #113

Closed Barbapapazes closed 8 months ago

Barbapapazes commented 11 months ago

๐Ÿ”— Linked issue

โ“ Type of change

๐Ÿ“š Description

I rewrite CLI to use citty instead of a home made CLI. The only drawback is that we now need to specify "clone" to clone a template from a registry and that's why it's a breaking change.

This will help use to maintain the CLI and add new features more easily. Indeed, we will be able to add new commands to support multiple registries.

Here what could come next:

import { defineCommand } from "citty";

export default defineCommand({
  meta: {
    name: "registry",
    description: "Manage registries",
  },
  subCommands: {
    // eslint-disable-next-line unicorn/prefer-top-level-await
    add: import("./registry/add").then((m) => m.default),
    // eslint-disable-next-line unicorn/prefer-top-level-await
    remove: import("./registry/remove").then((m) => m.default),
  },
});
import { defineCommand } from "citty";
import { updateUser } from "rc9"
import { consola } from "consola"

export default defineCommand({
  meta: {
    name: "add",
    description: "Add a new registry",
  },
  args: {
    name: {
      type: "positional",
      description: "Name of the registry",
    },
    url: {
      type: "positional",
      description: "URL of the registry",
    }
  },
  run: ({ args }) => {
    updateUser({ registries: { [args.name]: args.url } }, '.giget')
    consola.debug(`Added registry ${args.name} with URL ${args.url}`)
  }
})
import { defineCommand } from "citty";
import { consola } from "consola";
import { readUser, writeUser } from "rc9";

export default defineCommand({
  meta: {
    name: "remove",
    description: "Remove a registry",
  },
  args: {
    name: {
      type: "positional",
      description: "Name of the registry",
    },
  },
  run: ({ args }) => {
    const config = readUser(".giget");
    consola.debug('Read config', config)

    delete config.registries[args.name];
    consola.debug('Deleted registry', args.name)

    writeUser(config, ".giget");
    consola.debug('Wrote config', config)
  }
})

Then, with some changes in the internal logic, we could support multiple registries at the same time (related to #112).

๐Ÿ“ Checklist

codecov[bot] commented 11 months ago

Codecov Report

Attention: 78 lines in your changes are missing coverage. Please review.

Comparison is base (03053bd) 62.45% compared to head (e710ab1) 59.84%.

Files Patch % Lines
src/cli.ts 0.00% 78 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #113 +/- ## ========================================== - Coverage 62.45% 59.84% -2.61% ========================================== Files 7 7 Lines 506 528 +22 Branches 44 44 ========================================== Hits 316 316 - Misses 189 211 +22 Partials 1 1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.