import inquirer from "inquirer"; import camelcase from "camelcase"; import addItem from "./addItem"; import { TemplateType } from "./types"; import { isGriddoProject } from "./utils"; if (!isGriddoProject()) { console.log("⚠️ This script must be run in a Griddo project."); process.exit(1); } const templateType = { static: { label: "Static" }, detail: { label: "Detail", mode: "detail" }, list: { label: "List", mode: "list" } }; const questions: inquirer.QuestionCollection = [ { type: "list", name: "element", message: "Select a Griddo DX element", choices: [ { value: "component", name: "Component" }, { value: "module", name: "Module" }, { value: "template", name: "Template" }, new inquirer.Separator(), { value: "object", name: "Object (only schema)" }, new inquirer.Separator(), { value: "element", name: "Element (only UI)" } ] }, { type: "list", name: "templateType", message: "Select the template type", choices: Object.keys(templateType).map(value => ({ value, name: templateType[value].label })), when: function(answer: any) { return answer.element === "template"; } }, { type: "list", name: "special", message: "Select the page type", choices: [ { value: "", name: "Standard page" }, new inquirer.Separator("- Special templates -"), { value: "404", name: "404 page" }, { value: "sitemap", name: "Site Map" } ], when: function(answer: any) { return answer.templateType === "static"; } }, { type: "input", name: "niceName", message: "Type a nice name", validate: function(answer: any) { return answer.length > 0; } }, { type: "input", name: "name", message: "Type a component name", default: function(answer: any) { return camelcase(answer.niceName, { pascalCase: true }); } }, { type: "confirm", name: "storybook", message: "Create Storybook story file?", default: true, when: function(answer: any) { return answer.element !== "object"; } }, // { // type: "confirm", // name: "loki", // message: "Create Storybook Loki story file?", // default: false // }, { type: "confirm", name: "end", message: "Everything is alright?", default: false } ]; inquirer .prompt(questions) .then((answers: inquirer.Answers) => { console.log(); if (answers.end) { const type: TemplateType = answers.element === "template" ? { value: answers.templateType, ...templateType[answers.templateType] } : undefined; if (answers.special) { type["special"] = answers.special; } addItem({ element: answers.element, name: answers.name, niceName: answers.niceName, storybook: answers.storybook, type }); console.log( `🎉 Your brand new ${answers.element} \`${answers.name}\` has been created!` ); } else { console.log(`Nothing happened. Bye 👋`); } console.log(); }) .catch(error => { if (error.isTtyError) { console.log("Tty error"); } else { console.log(error); } });