|
| 1 | +const fs = require("fs"); |
| 2 | + |
| 3 | +/** |
| 4 | + * Ensures package names are namespaced into @ocode |
| 5 | + */ |
| 6 | +function normalizePackageName(str) { |
| 7 | + if (str.startsWith("@ocode")) { |
| 8 | + return str; |
| 9 | + } else { |
| 10 | + return `@ocode/${str}`; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +module.exports = function(plop) { |
| 15 | + // create your generators here |
| 16 | + plop.setGenerator("new-component", { |
| 17 | + description: "scaffold a new preact component with @ocode constants", |
| 18 | + prompts: [ |
| 19 | + { |
| 20 | + type: "input", |
| 21 | + name: "name", |
| 22 | + message: "What is the naem of this component?", |
| 23 | + validate: function(value) { |
| 24 | + const desiredPackageName = value; |
| 25 | + if (/.+/.test(desiredPackageName)) { |
| 26 | + const packages = fs.readdirSync("./packages"); |
| 27 | + if (packages.includes(desiredPackageName)) { |
| 28 | + return "name conflicts with existing package"; |
| 29 | + } |
| 30 | + return true; |
| 31 | + } |
| 32 | + return "name is required"; |
| 33 | + } |
| 34 | + }, { |
| 35 | + type: "input", |
| 36 | + name: "description", |
| 37 | + message: "What does this component do?", |
| 38 | + validate: function(value) { |
| 39 | + if (/.+/.test(value)) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + return "description is required"; |
| 43 | + } |
| 44 | + } |
| 45 | + ], |
| 46 | + actions: [ |
| 47 | + { |
| 48 | + type: "add", |
| 49 | + path: "packages/{{dashCase name}}/package.json", |
| 50 | + templateFile: "templates/package.json.tpl" |
| 51 | + }, |
| 52 | + { |
| 53 | + type: "add", |
| 54 | + path: "packages/{{dashCase name}}/src/index.js", |
| 55 | + templateFile: "templates/index.js.tpl" |
| 56 | + }, |
| 57 | + { |
| 58 | + type: "add", |
| 59 | + path: "packages/{{dashCase name}}/stories.js", |
| 60 | + templateFile: "templates/stories.js.tpl" |
| 61 | + } |
| 62 | + ] |
| 63 | + }); |
| 64 | +}; |
0 commit comments