|
| 1 | +const chalk = require("chalk"); |
| 2 | +const shell = require("shelljs"); |
| 3 | +const fs = require("fs"); |
| 4 | + |
| 5 | +const moduleMatrix = require("./moduleMatrix"); |
| 6 | + |
| 7 | +const log = console.log; |
| 8 | + |
| 9 | +const error = (msg, canExit = true) => { |
| 10 | + console.error(chalk.white.bgRed(`Error: ${msg}`)); |
| 11 | + canExit && process.exit(); |
| 12 | +}; |
| 13 | + |
| 14 | +const warning = chalk.keyword("orange"); |
| 15 | + |
| 16 | +const warn = (msg) => { |
| 17 | + console.warn(warning(`Warning: ${msg}`)); |
| 18 | +}; |
| 19 | + |
| 20 | +const createFile = (fileNameWithPath, content) => { |
| 21 | + shell.touch(fileNameWithPath); |
| 22 | + shell.ShellString(`${content}`).to(fileNameWithPath); |
| 23 | +} |
| 24 | + |
| 25 | +const tryAccess = (accessPath) => { |
| 26 | + return new Promise((resolve, reject) => { |
| 27 | + fs.access(accessPath, function (isAccessError) { |
| 28 | + const isGivenPathNotExist = isAccessError |
| 29 | + if (isGivenPathNotExist) { |
| 30 | + resolve() |
| 31 | + } else { |
| 32 | + console.error( |
| 33 | + `Sorry, unable to access the path: ${accessPath} which is already exist` |
| 34 | + ); |
| 35 | + reject() |
| 36 | + } |
| 37 | + }) |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +const moduleSetInstall = async (option = '', moduleListArray = []) => { |
| 42 | + if (!option) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + moduleListArray.forEach(moduleSet => { |
| 47 | + const moduleList = moduleMatrix[moduleSet] |
| 48 | + if (Array.isArray(moduleList)) { |
| 49 | + log(chalk.green.underline.bold(`Installing ${moduleList.join(', ')} modules`)) |
| 50 | + shell.exec(`npm i ${option} ${moduleList.join(' ')}`) |
| 51 | + } else { |
| 52 | + log(chalk.green.underline.bold(`execute ${moduleList} modules`)) |
| 53 | + shell.exec(`${moduleList}`) |
| 54 | + } |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +module.exports = { |
| 59 | + log, |
| 60 | + error, |
| 61 | + warn, |
| 62 | + createFile, |
| 63 | + tryAccess, |
| 64 | + moduleSetInstall |
| 65 | +}; |
0 commit comments