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

251 строка
7.1 KiB

  1. import path from "path";
  2. import fs from "fs";
  3. import inquirerDirectory from "inquirer-directory";
  4. export default function (plop) {
  5. // starting prompt can be customized to display what you want
  6. // plop.setWelcomeMessage('[CUSTOM]'.yellow + ' What can I do for you?');
  7. // helpers are passed through handlebars syntax and made
  8. // available for use in the generator templates
  9. // adds 4 dashes around some text (yes es6/es2015 is supported)
  10. plop.addHelper("dashAround", (text) => "---- " + text + " ----");
  11. // formats an array of options like you would write
  12. // it, if you were speaking (one, two, and three)
  13. plop.addHelper("wordJoin", function (words) {
  14. return words.join(", ").replace(/(:?.*),/, "$1, and");
  15. });
  16. plop.addHelper("absPath", function (p) {
  17. return path.resolve(plop.getPlopfilePath(), p);
  18. });
  19. // greet the user using a partial
  20. plop.addPartial(
  21. "salutation",
  22. "{{ greeting }}, my name is {{ properCase name }} and I am {{ age }}."
  23. );
  24. // load some additional helpers from a module installed using npm
  25. plop.load("plop-pack-fancy-comments", {
  26. prefix: "",
  27. upperCaseHeaders: true,
  28. commentStart: "",
  29. commentEnd: "",
  30. });
  31. const delayLog = (msg) => (answers) =>
  32. new Promise((resolve) => {
  33. setTimeout(() => resolve(msg), 1000);
  34. });
  35. // setGenerator creates a generator that can be run with "plop generatorName"
  36. plop.setGenerator("test", {
  37. description: "this is a test",
  38. prompts: [
  39. {
  40. type: "input",
  41. name: "name",
  42. message: "What is your name?",
  43. validate: function (value) {
  44. if (/.+/.test(value)) {
  45. return true;
  46. }
  47. return "name is required";
  48. },
  49. },
  50. {
  51. type: "input",
  52. name: "age",
  53. message: "How old are you?",
  54. validate: function (value) {
  55. var digitsOnly = /\d+/;
  56. if (digitsOnly.test(value)) {
  57. return true;
  58. }
  59. return "Invalid age! Must be a number genius!";
  60. },
  61. },
  62. {
  63. type: "checkbox",
  64. name: "toppings",
  65. message: "What pizza toppings do you like?",
  66. choices: [
  67. { name: "Cheese", value: "cheese", checked: true },
  68. { name: "Pepperoni", value: "pepperoni" },
  69. { name: "Pineapple", value: "pineapple" },
  70. { name: "Mushroom", value: "mushroom" },
  71. { name: "Bacon", value: "bacon", checked: true },
  72. ],
  73. },
  74. ],
  75. actions: [
  76. `this is a comment`,
  77. "this is another comment",
  78. delayLog("delayed thing"),
  79. delayLog("another delayed thing"),
  80. delayLog("this was also delayed"),
  81. {
  82. type: "add",
  83. path: "folder/{{dashCase name}}.txt",
  84. templateFile: "templates/temp.txt",
  85. abortOnFail: true,
  86. },
  87. function customAction(answers) {
  88. // move the current working directory to the plop file path
  89. // this allows this action to work even when the generator is
  90. // executed from inside a subdirectory
  91. process.chdir(plop.getPlopfilePath());
  92. // custom function can be synchronous or async (by returning a promise)
  93. var existsMsg = "psst {{name}}, change-me.txt already exists";
  94. var copiedMsg = "hey {{name}}, I copied change-me.txt for you";
  95. var changeFileName = "change-me.txt";
  96. var changeFilePath =
  97. plop.getDestBasePath() + "/folder/" + changeFileName;
  98. // you can use plop.renderString to render templates
  99. existsMsg = plop.renderString(existsMsg, answers);
  100. copiedMsg = plop.renderString(copiedMsg, answers);
  101. if (fs.existsSync(changeFilePath)) {
  102. // returned value shows up in the console
  103. return existsMsg;
  104. } else {
  105. // do a synchronous copy via node fs
  106. fs.writeFileSync(
  107. changeFilePath,
  108. fs.readFileSync("templates/" + changeFileName)
  109. );
  110. return copiedMsg;
  111. }
  112. },
  113. {
  114. type: "modify",
  115. path: "folder/change-me.txt",
  116. pattern: /(-- APPEND ITEMS HERE --)/gi,
  117. template: "$1\r\n{{name}}: {{age}}",
  118. },
  119. {
  120. type: "modify",
  121. path: "folder/change-me.txt",
  122. pattern: /(-- PREPEND ITEMS HERE --)/gi,
  123. templateFile: "templates/part.txt",
  124. },
  125. {
  126. type: "modify",
  127. path: "folder/change-me.txt",
  128. pattern: /## replace name here ##/gi,
  129. template: "replaced => {{dashCase name}}",
  130. },
  131. {
  132. type: "modify",
  133. path: "folder/change-me.txt",
  134. skip(data) {
  135. if (!data.toppings.includes("mushroom")) {
  136. // Skip this action
  137. return "Skipped replacing mushrooms";
  138. } else {
  139. // Continue with this action
  140. return;
  141. }
  142. },
  143. transform(fileContents, data) {
  144. return fileContents.replace(/mushrooms/g, "pepperoni");
  145. },
  146. },
  147. ],
  148. });
  149. // adding a custom inquirer prompt type
  150. plop.addPrompt("directory", inquirerDirectory);
  151. plop.setGenerator("custom-prompt", {
  152. description: "custom inquirer prompt example",
  153. prompts: [
  154. {
  155. type: "input",
  156. name: "fileName",
  157. message: "Pick a file name:",
  158. validate: function (value) {
  159. if (/.+/.test(value)) {
  160. return true;
  161. }
  162. return "file name is required";
  163. },
  164. },
  165. {
  166. type: "directory",
  167. name: "path",
  168. message: "where would you like to put this component?",
  169. basePath: plop.getPlopfilePath(),
  170. },
  171. ],
  172. actions: [
  173. function (data) {
  174. console.log(data);
  175. return "yay";
  176. },
  177. {
  178. type: "add",
  179. path: "{{absPath path}}/{{fileName}}.txt",
  180. template: "{{absPath path}}/{{fileName}} plopped!",
  181. },
  182. ],
  183. });
  184. // test with dynamic actions, regarding responses to prompts
  185. plop.setGenerator("dynamic actions", {
  186. description: "another test using an actions function",
  187. prompts: [
  188. {
  189. type: "input",
  190. name: "name",
  191. message: "What is your name?",
  192. validate: function (value) {
  193. if (/.+/.test(value)) {
  194. return true;
  195. }
  196. return "name is required";
  197. },
  198. },
  199. {
  200. type: "confirm",
  201. name: "hasPotatoes",
  202. message: "Do you want potatoes with your burger?",
  203. },
  204. ],
  205. actions: function (data) {
  206. var actions = [
  207. {
  208. type: "add",
  209. path: "folder/{{dashCase name}}-burger.txt",
  210. templateFile: "templates/burger.txt",
  211. abortOnFail: true,
  212. },
  213. ];
  214. if (data.hasPotatoes) {
  215. actions = actions.concat([
  216. {
  217. type: "add",
  218. path: "folder/{{dashCase name}}-potatoes.txt",
  219. templateFile: "templates/potatoes.txt",
  220. abortOnFail: true,
  221. },
  222. {
  223. type: "modify",
  224. path: "folder/{{dashCase name}}-burger.txt",
  225. pattern: /(!\n)/gi,
  226. template: "$1Your potatoes: {{dashCase name}}-potatoes.txt",
  227. },
  228. ]);
  229. }
  230. return actions;
  231. },
  232. });
  233. }