cli.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env node
  2. import { defineCommand, runMain } from 'citty';
  3. import { resolve } from 'pathe';
  4. import { consola } from 'consola';
  5. import { a as addDependency, i as installDependencies, r as removeDependency, d as detectPackageManager, c as dedupeDependencies, f as runScript } from './shared/nypm.Bcw9TJOu.mjs';
  6. import 'pkg-types';
  7. import 'node:module';
  8. import 'tinyexec';
  9. import 'node:fs';
  10. import 'node:fs/promises';
  11. const name = "nypm";
  12. const version = "0.6.0";
  13. const description = "Unified Package Manager for Node.js";
  14. const operationArgs = {
  15. cwd: {
  16. type: "string",
  17. description: "Current working directory"
  18. },
  19. workspace: {
  20. type: "boolean",
  21. description: "Add to workspace"
  22. },
  23. silent: {
  24. type: "boolean",
  25. description: "Run in silent mode"
  26. }
  27. };
  28. const install = defineCommand({
  29. meta: {
  30. description: "Install dependencies"
  31. },
  32. args: {
  33. ...operationArgs,
  34. name: {
  35. type: "positional",
  36. description: "Dependency name",
  37. required: false
  38. },
  39. dev: {
  40. type: "boolean",
  41. alias: "D",
  42. description: "Add as dev dependency"
  43. },
  44. global: {
  45. type: "boolean",
  46. alias: "g",
  47. description: "Add globally"
  48. },
  49. "frozen-lockfile": {
  50. type: "boolean",
  51. description: "Install dependencies with frozen lock file"
  52. }
  53. },
  54. run: async ({ args }) => {
  55. await (args._.length > 0 ? addDependency(args._, args) : installDependencies(args));
  56. }
  57. });
  58. const remove = defineCommand({
  59. meta: {
  60. description: "Remove dependencies"
  61. },
  62. args: {
  63. name: {
  64. type: "positional",
  65. description: "Dependency name",
  66. required: true
  67. },
  68. ...operationArgs
  69. },
  70. run: async ({ args }) => {
  71. await removeDependency(args.name, args);
  72. }
  73. });
  74. const detect = defineCommand({
  75. meta: {
  76. description: "Detect the current package manager"
  77. },
  78. args: {
  79. cwd: {
  80. type: "string",
  81. description: "Current working directory"
  82. }
  83. },
  84. run: async ({ args }) => {
  85. const cwd = resolve(args.cwd || ".");
  86. const packageManager = await detectPackageManager(cwd);
  87. if (packageManager?.warnings) {
  88. for (const warning of packageManager.warnings) {
  89. consola.warn(warning);
  90. }
  91. }
  92. if (!packageManager) {
  93. consola.error(`Cannot detect package manager in \`${cwd}\``);
  94. return process.exit(1);
  95. }
  96. consola.log(
  97. `Detected package manager in \`${cwd}\`: \`${packageManager.name}@${packageManager.version}\``
  98. );
  99. }
  100. });
  101. const dedupe = defineCommand({
  102. meta: {
  103. description: "Dedupe dependencies"
  104. },
  105. args: {
  106. cwd: {
  107. type: "string",
  108. description: "Current working directory"
  109. },
  110. silent: {
  111. type: "boolean",
  112. description: "Run in silent mode"
  113. },
  114. recreateLockFile: {
  115. type: "boolean",
  116. description: "Recreate lock file"
  117. }
  118. },
  119. run: async ({ args }) => {
  120. await dedupeDependencies(args);
  121. }
  122. });
  123. const run = defineCommand({
  124. meta: {
  125. description: "Run script"
  126. },
  127. args: {
  128. name: {
  129. type: "positional",
  130. description: "Script name",
  131. required: true
  132. },
  133. ...operationArgs
  134. },
  135. run: async ({ args }) => {
  136. await runScript(args.name, args);
  137. }
  138. });
  139. const main = defineCommand({
  140. meta: {
  141. name,
  142. version,
  143. description
  144. },
  145. subCommands: {
  146. install,
  147. i: install,
  148. add: install,
  149. remove,
  150. rm: remove,
  151. uninstall: remove,
  152. un: remove,
  153. detect,
  154. dedupe,
  155. run
  156. }
  157. });
  158. runMain(main);