版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # ✨ giget
  2. [![npm version][npm-version-src]][npm-version-href]
  3. [![npm downloads][npm-downloads-src]][npm-downloads-href]
  4. [![Github Actions][github-actions-src]][github-actions-href]
  5. [![Codecov][codecov-src]][codecov-href]
  6. > Download templates and git repositories with pleasure!
  7. ## Features
  8. ✔ Support popular git providers (GitHub, GitLab, Bitbucket, Sourcehut) out of the box.
  9. ✔ Built-in and custom [template registry](#template-registry).
  10. ✔ Fast cloning using tarball gzip without depending on local `git` and `tar`.
  11. ✔ Works online and offline with disk cache support.
  12. ✔ Custom template provider support with programmatic usage.
  13. ✔ Support extracting with a subdir.
  14. ✔ Authorization support to download private templates
  15. ## Usage (CLI)
  16. ```bash
  17. npx giget@latest <template> [<dir>] [...options]
  18. ```
  19. ### Arguments
  20. - **template**: Template name or a a URI describing provider, repository, subdir, and branch/ref. (See [Examples](#examples))
  21. - **dir**: A relative or absolute path where to extract the template.
  22. ### Options
  23. - `--force`: Clone to existing directory even if exists.
  24. - `--offline`: Do not attempt to download and use cached version.
  25. - `--prefer-offline`: Use cache if exists otherwise try to download.
  26. - `--force-clean`: ⚠️ Remove any existing directory or file recusively before cloning.
  27. - `--shell`: ⚠️ Open a new shell with current working directory in cloned dir. (Experimental).
  28. - `--registry`: URL to a custom registry. (Can be overriden with `GIGET_REGISTRY` environment variable).
  29. - `--no-registry`: Disable registry lookup and functionality.
  30. - `--verbose`: Show verbose debugging info.
  31. - `--cwd`: Set current working directory to resolve dirs relative to it.
  32. - `--auth`: Custom Authorization token to use for downloading template. (Can be overriden with `GIGET_AUTH` environment variable).
  33. ### Examples
  34. ```sh
  35. # Clone nuxt starter from giget template registry
  36. npx giget@latest nuxt
  37. # Clone the main branch of github.com/unjs/template to unjs-template directory
  38. npx giget@latest gh:unjs/template
  39. # Clone to myProject directory
  40. npx giget@latest gh:unjs/template myProject
  41. # Clone dev branch
  42. npx giget@latest gh:unjs/template#dev
  43. # Clone /test directory from main branch
  44. npx giget@latest gh:unjs/template/test
  45. # Clone from gitlab
  46. npx giget@latest gitlab:unjs/template
  47. # Clone from bitbucket
  48. npx giget@latest bitbucket:unjs/template
  49. # Clone from sourcehut
  50. npx giget@latest sourcehut:pi0/unjs-template
  51. ```
  52. ## Template Registry
  53. Giget has a built-in HTTP registry system for resolving templates. This way you can support template name shortcuts and meta-data. Default registry is served from [unjs/giget/templates](./templates/).
  54. If you want to add your template to the built-in registry, just drop a PR to add it to the [./templates](./templates) directory. Slugs are added on first-come first-served basis but this might change in the future.
  55. ### Custom Registry
  56. A custom registry should provide an endpoint with dynamic path `/:template.json` that returns a JSON response with keys same as [custom providers](#custom-providers).
  57. - `name`: (required) Name of the template.
  58. - `tar` (required) Link to the tar download link.
  59. - `defaultDir`: (optional) Default cloning directory.
  60. - `url`: (optional) Webpage of the template.
  61. - `subdir`: (optional) Directory inside the tar file.
  62. - `headers`: (optional) Custom headers to send while downloading template.
  63. Because of the simplicity, you can even use a GitHub repository as template registry but also you can build something more powerful by bringing your own API.
  64. ## Usage (Programmatic)
  65. Install package:
  66. ```sh
  67. # npm
  68. npm install giget
  69. # yarn
  70. yarn install giget
  71. # pnpm
  72. pnpm install giget
  73. ```
  74. Import:
  75. ```js
  76. // ESM
  77. import { downloadTemplate } from "giget";
  78. // CommonJS
  79. const { downloadTemplate } = require("giget");
  80. ```
  81. ### `downloadTemplate(source, options?)`
  82. **Example:**
  83. ```js
  84. const { source, dir } = await downloadTemplate("github:unjs/template");
  85. ```
  86. **Options:**
  87. - `source`: (string) Input source in format of `[provider]:repo[/subpath][#ref]`.
  88. - `options`: (object) Options are usually inferred from the input string. You can customize them.
  89. - `dir`: (string) Destination directory to clone to. If not provided, `user-name` will be used relative to the current directory.
  90. - `provider`: (string) Either `github`, `gitlab`, `bitbucket` or `sourcehut`. The default is `github`.
  91. - `repo`: (string) Name of repository in format of `{username}/{reponame}`.
  92. - `ref`: (string) Git ref (branch or commit or tag). The default value is `main`.
  93. - `subdir`: (string) Directory of the repo to clone from. The default value is none.
  94. - `force`: (boolean) Extract to the exisiting dir even if already exsists.
  95. - `forceClean`: (boolean) ⚠️ Clean ups any existing directory or file before cloning.
  96. - `offline`: (boolean) Do not attempt to download and use cached version.
  97. - `preferOffline`: (boolean) Use cache if exists otherwise try to download.
  98. - `providers`: (object) A map from provider name to custom providers. Can be used to override built-ins too.
  99. - `registry`: (string or false) Set to `false` to disable registry. Set to a URL string (without trailing slash) for custom registry. (Can be overriden with `GIGET_REGISTRY` environment variable).
  100. - `cwd`: (string) Current working directory to resolve dirs relative to it.
  101. - `auth`: (string) Custom Authorization token to use for downloading template. (Can be overriden with `GIGET_AUTH` environment variable).
  102. **Return value:**
  103. The return value is a promise that resolves to the resolved template.
  104. - `dir`: (string) Path to extracted dir.
  105. - `source`: (string) Normalized version of the input source without provider.
  106. - [other provider template keys]
  107. - `url`: (string) URL of repostiroy that can be opened in browser. Useful for logging.
  108. ## Custom Providers
  109. Using programmatic method, you can make your own custom template providers.
  110. ```ts
  111. import type { TemplateProvider } from "giget";
  112. const rainbow: TemplateProvider = async (input, { auth }) => {
  113. return {
  114. name: "rainbow",
  115. version: input,
  116. headers: { authorization: auth },
  117. url: `https://rainbow.template/?variant=${input}`,
  118. tar: `https://rainbow.template/dl/rainbow.${input}.tar.gz`,
  119. };
  120. };
  121. const { source, dir } = await downloadRepo("rainbow:one", {
  122. providers: { rainbow },
  123. });
  124. ```
  125. ### Custom Registry Providers
  126. You can define additional [custom registry](#custom-registry) providers using `registryProvider` utility and register to `providers`.
  127. ```ts
  128. import { registryProvider } from "giget";
  129. const themes = registryProvider(
  130. "https://raw.githubusercontent.com/unjs/giget/main/templates"
  131. );
  132. const { source, dir } = await downloadRepo("themes:test", {
  133. providers: { themes },
  134. });
  135. ```
  136. ## Related projects
  137. Giget wouldn't be possible without inspiration from former projects. In comparison, giget does not depend on any local command which increases stability and performance, supports custom template providers, auth and many more features out of the box.
  138. - https://github.com/samsonjs/gitter
  139. - https://github.com/tiged/tiged
  140. - https://github.com/Rich-Harris/degit
  141. ## 💻 Development
  142. - Clone this repository
  143. - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
  144. - Install dependencies using `pnpm install`
  145. - Run interactive tests using `pnpm dev`
  146. ## License
  147. Made with 💛
  148. Published under [MIT License](./LICENSE).
  149. <!-- Badges -->
  150. [npm-version-src]: https://img.shields.io/npm/v/giget?style=flat-square
  151. [npm-version-href]: https://npmjs.com/package/giget
  152. [npm-downloads-src]: https://img.shields.io/npm/dm/giget?style=flat-square
  153. [npm-downloads-href]: https://npmjs.com/package/giget
  154. [github-actions-src]: https://img.shields.io/github/workflow/status/unjs/giget/ci/main?style=flat-square
  155. [github-actions-href]: https://github.com/unjs/giget/actions?query=workflow%3Aci
  156. [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/giget/main?style=flat-square
  157. [codecov-href]: https://codecov.io/gh/unjs/giget