|
| 1 | +const chalk = require(`chalk`); |
| 2 | +const { pathExists } = require(`fs-extra`); |
| 3 | +const path = require(`path`); |
| 4 | +const { spinner } = require(`../../../utils`); |
| 5 | +const { file, getTemplate } = require(`../../../core`); |
| 6 | +const outputs = require(`../../outputs`); |
| 7 | + |
| 8 | +const generateConfigFiles = async config => { |
| 9 | + const fileExtension = config.projectType; |
| 10 | + const environment = config.env; |
| 11 | + const dirPath = path.join(process.cwd(), `config`, `env`, config.env); |
| 12 | + |
| 13 | + for (const fileName of config.files) { |
| 14 | + const projectFilePath = `config/env/${environment}/${fileName}.${fileExtension}`; |
| 15 | + const filePath = path.join(dirPath, `${fileName}.${fileExtension}`); |
| 16 | + |
| 17 | + spinner.start(`Checking for existing ${projectFilePath}`); |
| 18 | + const hasExistingConfigFile = await pathExists(filePath); |
| 19 | + if (hasExistingConfigFile) { |
| 20 | + spinner.stopAndPersist({ |
| 21 | + symbol: `🕵️♀️ `, |
| 22 | + text: `Detected ${chalk.yellow(`${projectFilePath}`)} \n` |
| 23 | + }); |
| 24 | + |
| 25 | + const backedUp = await file.backup(filePath); |
| 26 | + spinner.stopAndPersist({ |
| 27 | + text: `${chalk.yellow(projectFilePath)} was backed up ${ |
| 28 | + backedUp ? `successfully` : `unsuccessfuly` |
| 29 | + } \n` |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + const generatedFile = await file.generate( |
| 34 | + filePath, |
| 35 | + getTemplate(fileName, fileExtension) |
| 36 | + ); |
| 37 | + if (generatedFile) { |
| 38 | + spinner.stopAndPersist({ |
| 39 | + symbol: `⚙️ `, |
| 40 | + text: `Configured ${chalk.bold.green(`${projectFilePath}`)} \n` |
| 41 | + }); |
| 42 | + } else { |
| 43 | + outputs.error(`Unable to generate the ${fileName} configuration file`); |
| 44 | + } |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +module.exports = { |
| 49 | + generateConfigFiles |
| 50 | +}; |
0 commit comments