cli.mjs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env node
  2. import { relative } from 'node:path';
  3. import { defineCommand, runMain } from 'citty';
  4. import { consola } from 'consola';
  5. import { downloadTemplate, startShell } from './index.mjs';
  6. import 'node:fs/promises';
  7. import 'node:fs';
  8. import 'tar';
  9. import 'pathe';
  10. import 'defu';
  11. import 'nypm';
  12. import 'node:stream';
  13. import 'node:child_process';
  14. import 'node:os';
  15. import 'node:util';
  16. import 'node-fetch-native/proxy';
  17. const name = "giget";
  18. const version = "1.2.1";
  19. const description = "Download templates and git repositories with pleasure!";
  20. const repository = "unjs/giget";
  21. const license = "MIT";
  22. const sideEffects = false;
  23. const type = "module";
  24. const exports = {
  25. ".": {
  26. "import": {
  27. types: "./dist/index.d.mts",
  28. "default": "./dist/index.mjs"
  29. },
  30. require: {
  31. types: "./dist/index.d.cts",
  32. "default": "./dist/index.cjs"
  33. }
  34. }
  35. };
  36. const main = "./dist/index.cjs";
  37. const module = "./dist/index.mjs";
  38. const types = "./dist/index.d.ts";
  39. const bin = {
  40. giget: "./dist/cli.mjs"
  41. };
  42. const files = [
  43. "dist"
  44. ];
  45. const scripts = {
  46. build: "unbuild",
  47. dev: "vitest dev",
  48. giget: "jiti ./src/cli.ts",
  49. lint: "eslint --ext .ts,.js,.mjs,.cjs . && prettier -c src test",
  50. "lint:fix": "eslint --ext .ts,.js,.mjs,.cjs . --fix && prettier -w src test",
  51. prepack: "unbuild",
  52. play: "pnpm giget --force-clean --verbose unjs .tmp/clone",
  53. release: "pnpm test && changelogen --release && npm publish && git push --follow-tags",
  54. test: "pnpm lint && vitest run --coverage"
  55. };
  56. const dependencies = {
  57. citty: "^0.1.5",
  58. consola: "^3.2.3",
  59. defu: "^6.1.3",
  60. "node-fetch-native": "^1.6.1",
  61. nypm: "^0.3.3",
  62. ohash: "^1.1.3",
  63. pathe: "^1.1.1",
  64. tar: "^6.2.0"
  65. };
  66. const devDependencies = {
  67. "@types/node": "^20.10.5",
  68. "@types/tar": "^6.1.10",
  69. "@vitest/coverage-v8": "^1.1.0",
  70. changelogen: "^0.5.5",
  71. eslint: "^8.56.0",
  72. "eslint-config-unjs": "^0.2.1",
  73. jiti: "^1.21.0",
  74. prettier: "^3.1.1",
  75. typescript: "^5.3.3",
  76. unbuild: "^2.0.0",
  77. vitest: "^1.1.0"
  78. };
  79. const packageManager = "pnpm@8.12.1";
  80. const pkg = {
  81. name: name,
  82. version: version,
  83. description: description,
  84. repository: repository,
  85. license: license,
  86. sideEffects: sideEffects,
  87. type: type,
  88. exports: exports,
  89. main: main,
  90. module: module,
  91. types: types,
  92. bin: bin,
  93. files: files,
  94. scripts: scripts,
  95. dependencies: dependencies,
  96. devDependencies: devDependencies,
  97. packageManager: packageManager
  98. };
  99. const mainCommand = defineCommand({
  100. meta: {
  101. name: pkg.name,
  102. version: pkg.version,
  103. description: pkg.description
  104. },
  105. args: {
  106. // TODO: Make it `-t` in the next major version
  107. template: {
  108. type: "positional",
  109. description: "Template name or a a URI describing provider, repository, subdir, and branch/ref"
  110. },
  111. dir: {
  112. type: "positional",
  113. description: "A relative or absolute path where to extract the template",
  114. required: false
  115. },
  116. auth: {
  117. type: "string",
  118. description: "Custom Authorization token to use for downloading template. (Can be overriden with `GIGET_AUTH` environment variable)"
  119. },
  120. cwd: {
  121. type: "string",
  122. description: "Set current working directory to resolve dirs relative to it"
  123. },
  124. force: {
  125. type: "boolean",
  126. description: "Clone to existing directory even if exists"
  127. },
  128. forceClean: {
  129. type: "boolean",
  130. description: "Remove any existing directory or file recusively before cloning"
  131. },
  132. offline: {
  133. type: "boolean",
  134. description: "o not attempt to download and use cached version"
  135. },
  136. preferOffline: {
  137. type: "boolean",
  138. description: "Use cache if exists otherwise try to download"
  139. },
  140. shell: {
  141. type: "boolean",
  142. description: "Open a new shell with current working "
  143. },
  144. install: {
  145. type: "boolean",
  146. description: "Install dependencies after cloning"
  147. },
  148. verbose: {
  149. type: "boolean",
  150. description: "Show verbose debugging info"
  151. }
  152. },
  153. run: async ({ args }) => {
  154. if (args.verbose) {
  155. process.env.DEBUG = process.env.DEBUG || "true";
  156. }
  157. const r = await downloadTemplate(args.template, {
  158. dir: args.dir,
  159. force: args.force,
  160. forceClean: args.forceClean,
  161. offline: args.offline,
  162. preferOffline: args.preferOffline,
  163. auth: args.auth,
  164. install: args.install
  165. });
  166. const _from = r.name || r.url;
  167. const _to = relative(process.cwd(), r.dir) || "./";
  168. consola.log(`\u2728 Successfully cloned \`${_from}\` to \`${_to}\`
  169. `);
  170. if (args.shell) {
  171. startShell(r.dir);
  172. }
  173. }
  174. });
  175. runMain(mainCommand);