UNPKG

blox-npm-templates

Version:

Npx generator for blox apps by NimbleWork Inc(Formely Digite Inc)

139 lines (118 loc) 3.85 kB
#! /usr/bin/env node console.log("Generator Started!"); const inquirer = require('inquirer'); const fs = require('fs'); const CURR_DIR = process.cwd(); const TEMPLATES = fs.readdirSync(`${__dirname}/templates`); const QUESTIONS = [ { name: 'project-choice', type: 'list', message: 'What project template would you like to generate?', choices: TEMPLATES }, { name: 'git-repo', type: 'input', message: 'What is git repo name?', validate: function (input) { if (input && input.trim()) { return true } else { return 'Git repo name must be specified.'; } } }, { name: 'comp-name', type: 'input', message: 'What is component name?', validate: function (input) { if (input && input.trim()) { return true } else { return 'Component name must be specified.'; } } }, { name: 'author-name', type: 'input', message: 'Author name:', validate: function (input) { if (input && input.trim()) { return true } else { return 'Author name must be specified.'; } } }, { name: 'author-email', type: 'input', message: 'Author email:', validate: function (input) { if ( input && input.trim() ) { if ( input.trim().toLowerCase().match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/) ) { return true; } else { return 'Author email is not correct.'; } } else { return 'Author email must be specified.'; } } } ]; inquirer .prompt(QUESTIONS) .then(answers => { const project = answers['project-choice']; processAnswers(answers) }); function processAnswers(answers) { const projectChoice = answers['project-choice']; const projectName = answers['project-name']; const gitRepoName = answers['git-repo'] const templatePath = `${__dirname}/templates/${projectChoice}`; fs.mkdirSync(`${CURR_DIR}/${gitRepoName}`); createDirectoryContents(templatePath, gitRepoName, answers); } function createDirectoryContents (templatePath, newProjectPath, userInputs) { const filesToCreate = fs.readdirSync(templatePath); filesToCreate.forEach(file => { const originalFilePath = `${templatePath}/${file}`; // get stats about the current file const stats = fs.statSync(originalFilePath); if ( stats.isFile() ) { const contents = processFileContent(file, fs.readFileSync(originalFilePath, 'utf8'), userInputs); const fileName = processFileName(file, userInputs); const writePath = `${CURR_DIR}/${newProjectPath}/${fileName}`; fs.writeFileSync(writePath, contents, 'utf8'); } else if ( stats.isDirectory() ) { fs.mkdirSync(`${CURR_DIR}/${newProjectPath}/${file}`); // recursive call createDirectoryContents(`${templatePath}/${file}`, `${newProjectPath}/${file}`, userInputs); } }); } function processFileName(fileName, userInputs) { if ( fileName === 'component-ci.groovy' ) { return `${userInputs['git-repo']}-ci.groovy`; } else { return fileName; } } function processFileContent(fileName, content, userInputs) { let processedContent = content; if ( fileName === 'package.json' ) { processedContent = processedContent .replace('#package-name#', userInputs['comp-name'].trim()) .replace('#author-name#', userInputs['author-name'].trim()) .replace('#author-email#', userInputs['author-email'].trim()) } else if ( fileName === 'component-ci.groovy' ) { processedContent = processedContent .replace(new RegExp('#COMPONENT-NAME#', 'g'), userInputs['comp-name'].trim()) } return processedContent; }