版博士V2.0程序
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 2 gadiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const fse = require('fs-extra')
  2. const { showDir, showExt, moduleTypes } = require('./shared/base.cjs')
  3. /**
  4. * 自动创建
  5. * @param {import('plop').NodePlopAPI} plop
  6. */
  7. function create(plop) {
  8. let exist = null
  9. let modulePath = null
  10. plop.setGenerator('controller', {
  11. description: '自动创建',
  12. prompts: [
  13. {
  14. name: 'type',
  15. type: 'list',
  16. default: 'component',
  17. message: '您希望生成哪种类型的模块?',
  18. choices: moduleTypes,
  19. },
  20. {
  21. name: 'isMarkdown',
  22. type: 'confirm',
  23. message: '是否 markdown 类型?',
  24. default: false,
  25. // 如果是 page 类型需要询问是否为 markdown 类型
  26. when({ type }) {
  27. return type === 'page'
  28. },
  29. },
  30. {
  31. name: 'name',
  32. type: 'input',
  33. message({ type }) {
  34. return `请输入 ${type} 的命名`
  35. },
  36. },
  37. {
  38. name: 'shouldReset',
  39. type: 'confirm',
  40. default: false,
  41. message({ type }) {
  42. return `目标 ${type} 已存在,是否重置?`
  43. },
  44. // 确认模块是否已存在,是则询问是否重置
  45. when({ type, name, isMarkdown }) {
  46. const dir = showDir(type)
  47. const ext = showExt(type, isMarkdown)
  48. modulePath = `src/${dir}/${name}.${ext}`
  49. exist = fse.pathExistsSync(modulePath)
  50. if (exist) {
  51. return true
  52. }
  53. },
  54. },
  55. ],
  56. actions(answer) {
  57. const { type, shouldReset } = answer
  58. if (exist && !shouldReset) {
  59. throw new Error(`${type} 创建失败`)
  60. }
  61. return [
  62. {
  63. type: 'add',
  64. force: true,
  65. path: `../${modulePath}`,
  66. templateFile: `./template/${type}.hbs`,
  67. },
  68. ]
  69. },
  70. })
  71. }
  72. module.exports = create