|
| 1 | +#!/usr/bin/env node |
| 2 | +const inquirer = require("inquirer"); |
| 3 | +const fs = require("fs"); |
| 4 | + |
| 5 | +const CHOICES = fs.readdirSync(`${__dirname}/templates`); |
| 6 | + |
| 7 | +const QUESTIONS = [ |
| 8 | + { |
| 9 | + name: "project-choice", |
| 10 | + type: "list", |
| 11 | + message: "What project template would you like to generate?", |
| 12 | + choices: CHOICES |
| 13 | + }, |
| 14 | + { |
| 15 | + name: "project-name", |
| 16 | + type: "input", |
| 17 | + message: "Project name:", |
| 18 | + validate: function(input) { |
| 19 | + if (/^([A-Za-z\-\_\d])+$/.test(input)) return true; |
| 20 | + else |
| 21 | + return "Project name may only include letters, numbers, underscores and hashes."; |
| 22 | + } |
| 23 | + } |
| 24 | +]; |
| 25 | + |
| 26 | +const CURR_DIR = process.cwd(); |
| 27 | + |
| 28 | + |
| 29 | +function createDirectoryContents(templatePath, newProjectPath) { |
| 30 | + const filesToCreate = fs.readdirSync(templatePath); |
| 31 | + |
| 32 | + filesToCreate.forEach(file => { |
| 33 | + const origFilePath = `${templatePath}/${file}`; |
| 34 | + |
| 35 | + // get stats about the current file |
| 36 | + const stats = fs.statSync(origFilePath); |
| 37 | + |
| 38 | + if (stats.isFile()) { |
| 39 | + const contents = fs.readFileSync(origFilePath, "utf8"); |
| 40 | + |
| 41 | + const writePath = `${CURR_DIR}/${newProjectPath}/${file}`; |
| 42 | + fs.writeFileSync(writePath, contents, "utf8"); |
| 43 | + } |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +inquirer.prompt(QUESTIONS).then(answers => { |
| 48 | + const projectChoice = answers["project-choice"]; |
| 49 | + const projectName = answers["project-name"]; |
| 50 | + const templatePath = `${__dirname}/templates/${projectChoice}`; |
| 51 | + |
| 52 | + fs.mkdirSync(`${CURR_DIR}/${projectName}`); |
| 53 | + |
| 54 | + createDirectoryContents(templatePath, projectName); |
| 55 | +}); |
0 commit comments