|
| 1 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 2 | +const fs = require('fs') |
| 3 | +const path = require('path') |
| 4 | + |
| 5 | +const chalk = require('chalk') |
| 6 | +const inquirer = require('inquirer') |
| 7 | +const shell = require('shelljs') |
| 8 | + |
| 9 | +const logger = { |
| 10 | + log: msg => console.log(msg), // eslint-disable-line no-console |
| 11 | + success: msg => console.log(chalk.green(msg)), // eslint-disable-line no-console |
| 12 | +} |
| 13 | + |
| 14 | +const templateBase = path.resolve(__dirname, '../templates') |
| 15 | + |
| 16 | +module.exports = () => { |
| 17 | + // ask for the user's input |
| 18 | + logger.log('Please fill the following values…') |
| 19 | + |
| 20 | + // scan the templates folder for variants |
| 21 | + inquirer |
| 22 | + .prompt([ |
| 23 | + { |
| 24 | + type: 'list', |
| 25 | + name: 'template', |
| 26 | + message: 'Which template do you want to use', |
| 27 | + choices: shell.ls(templateBase), |
| 28 | + }, |
| 29 | + ]) |
| 30 | + .then(({ template }) => { |
| 31 | + const templatePath = `${templateBase}/${template}` |
| 32 | + |
| 33 | + // prompt variables |
| 34 | + const variables = require(`${templatePath}/_variables`) // eslint-disable-line |
| 35 | + |
| 36 | + inquirer.prompt(variables).then(answers => { |
| 37 | + const { name } = answers |
| 38 | + const targetPath = path.resolve(__dirname, `../packages/${name}/`) |
| 39 | + |
| 40 | + // Remove Apache-2.0 licence file if another is selected |
| 41 | + if (answers.licence !== 'Apache-2.0') { |
| 42 | + shell.rm(`${templatePath}/LICENCE`) |
| 43 | + } |
| 44 | + |
| 45 | + // copy template |
| 46 | + if (fs.existsSync(templatePath)) { |
| 47 | + logger.log('Copying files…') |
| 48 | + shell.mkdir('-p', targetPath) |
| 49 | + shell.cp('-R', `${templatePath}/*`, targetPath) |
| 50 | + logger.success('✔ The files have been copied!') |
| 51 | + } |
| 52 | + |
| 53 | + // Remove variables file from the target directory |
| 54 | + if (fs.existsSync(`${targetPath}/_variables.js`)) { |
| 55 | + shell.rm(`${targetPath}/_variables.js`) |
| 56 | + } |
| 57 | + |
| 58 | + // Replace variable values in all files |
| 59 | + shell.ls('-Rl', targetPath).forEach(entry => { |
| 60 | + if (entry.isFile()) { |
| 61 | + // Replace '[VARIABLE]` with the corresponding variable value from the prompt |
| 62 | + variables.forEach(variable => { |
| 63 | + shell.sed( |
| 64 | + '-i', |
| 65 | + `\\[${variable.name.toUpperCase()}\\]`, |
| 66 | + answers[variable.name], |
| 67 | + `${targetPath}/${entry.name}`, |
| 68 | + ) |
| 69 | + }) |
| 70 | + |
| 71 | + // Insert current year in files |
| 72 | + shell.sed( |
| 73 | + '-i', |
| 74 | + '\\[YEAR\\]', |
| 75 | + new Date().getFullYear(), |
| 76 | + `${targetPath}/${entry.name}`, |
| 77 | + ) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + logger.log(chalk.green('✔ Success!')) |
| 82 | + }) |
| 83 | + }) |
| 84 | +} |
0 commit comments