lerna-module-data.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. Original: https://github.com/npm/init-package-json/blob/c649fbe/default-input.js
  3. The contents of this file are compiled into a function with this signature:
  4. (function(
  5. yes,
  6. filename,
  7. dirname,
  8. basename,
  9. package,
  10. config,
  11. prompt,
  12. __filename,
  13. __dirname,
  14. __basename,
  15. module,
  16. require,
  17. exports
  18. ) {
  19. Because of the `package` parameter, we can't "use strict",
  20. as `package` is a reserved word in strict mode.
  21. Conveniently, all of these parameters are also available on `this`.
  22. We exploit this fact to avoid eslint breaking on the reserved word.
  23. */
  24. const validateLicense = require("validate-npm-package-license");
  25. const validateName = require("validate-npm-package-name");
  26. const npa = require("npm-package-arg");
  27. const semver = require("semver");
  28. const niceName = (rudeName) =>
  29. rudeName
  30. .replace(/^node-|[.-]js$/g, "")
  31. .replace(" ", "-")
  32. .toLowerCase();
  33. let name = this.package.name || this.basename;
  34. let spec;
  35. try {
  36. spec = npa(name);
  37. } catch (e) {
  38. spec = {};
  39. }
  40. let scope = this.config.get("scope");
  41. if (scope) {
  42. if (scope.charAt(0) !== "@") {
  43. scope = `@${scope}`;
  44. }
  45. if (spec.scope) {
  46. name = `${scope}/${spec.name.split("/")[1]}`;
  47. } else {
  48. name = `${scope}/${name}`;
  49. }
  50. }
  51. exports.name = this.yes
  52. ? name
  53. : this.prompt("package name", niceName(name), (data) => {
  54. const its = validateName(data);
  55. if (its.validForNewPackages) {
  56. return data;
  57. }
  58. const errors = (its.errors || []).concat(its.warnings || []);
  59. const er = new Error(`Sorry, ${errors.join(" and ")}.`);
  60. er.notValid = true;
  61. return er;
  62. });
  63. const version = this.package.version || this.config.get("init-version") || "1.0.0";
  64. exports.version = this.yes
  65. ? version
  66. : this.prompt("version", version, (data) => {
  67. if (semver.valid(data)) {
  68. return data;
  69. }
  70. const er = new Error(`Invalid version: "${data}"`);
  71. er.notValid = true;
  72. return er;
  73. });
  74. if (this.config.get("private")) {
  75. exports.private = true;
  76. }
  77. if (!this.package.description) {
  78. exports.description = this.yes ? this.config.get("description") : this.prompt("description");
  79. }
  80. if (!this.package.keywords) {
  81. const keywords = this.config.get("keywords") || "";
  82. exports.keywords = this.yes
  83. ? keywords
  84. : this.prompt("keywords", keywords, (data) => {
  85. if (!data) {
  86. return undefined;
  87. }
  88. if (Array.isArray(data)) {
  89. // eslint-disable-next-line no-param-reassign
  90. data = data.join(" ");
  91. }
  92. if (typeof data !== "string") {
  93. return data;
  94. }
  95. return data.split(/[\s,]+/);
  96. });
  97. }
  98. if (!this.package.author) {
  99. let authorConfig;
  100. if (this.config.get("init-author-name")) {
  101. authorConfig = {
  102. name: this.config.get("init-author-name"),
  103. email: this.config.get("init-author-email"),
  104. url: this.config.get("init-author-url"),
  105. };
  106. }
  107. exports.author = authorConfig || (this.yes ? "" : this.prompt("author"));
  108. }
  109. if (!this.package.homepage) {
  110. const homepage = this.config.get("homepage");
  111. exports.homepage = this.yes ? homepage : this.prompt("homepage", homepage);
  112. }
  113. const license = this.package.license || this.config.get("init-license") || "ISC";
  114. exports.license = this.yes
  115. ? license
  116. : this.prompt("license", license, (data) => {
  117. const its = validateLicense(data);
  118. if (its.validForNewPackages) {
  119. return data;
  120. }
  121. const errors = (its.errors || []).concat(its.warnings || []);
  122. const er = new Error(`Sorry, ${errors.join(" and ")}.`);
  123. er.notValid = true;
  124. return er;
  125. });
  126. if (!this.package.main && this.config.get("init-main")) {
  127. const mainEntry = this.config.get("init-main");
  128. exports.main = this.yes ? mainEntry : this.prompt("entry point", mainEntry);
  129. }
  130. if (!this.package.module && this.config.get("init-es-module")) {
  131. const moduleEntry = this.config.get("init-es-module");
  132. exports.module = this.yes ? moduleEntry : this.prompt("module entry", moduleEntry);
  133. }
  134. if (!this.package.bin && this.config.get("bin")) {
  135. exports.bin = this.config.get("bin");
  136. }
  137. if (!this.package.directories && this.config.get("directories")) {
  138. exports.directories = this.config.get("directories");
  139. }
  140. if (!this.package.files && this.config.get("files")) {
  141. exports.files = (cb) => {
  142. // callback MUST yield the thread for some inexplicable reason
  143. process.nextTick(cb, null, this.config.get("files"));
  144. };
  145. }
  146. if (!this.package.publishConfig && this.config.get("publishConfig")) {
  147. exports.publishConfig = this.config.get("publishConfig");
  148. }
  149. if (!this.package.repository) {
  150. exports.repository = (cb) => {
  151. let val = this.config.get("repository");
  152. if (val && val.match(/^git@github.com:/)) {
  153. val = val.replace(/^git@github.com:/, "https://github.com/");
  154. }
  155. return cb(null, this.yes ? val : this.prompt("git repository", val));
  156. };
  157. }
  158. if (!this.package.scripts) {
  159. exports.scripts = {
  160. test: 'echo "Error: run tests from root" && exit 1',
  161. };
  162. }
  163. if (!this.package.dependencies && this.config.get("dependencies")) {
  164. exports.dependencies = this.config.get("dependencies");
  165. }