|
| 1 | +const fs = require('fs-extra'); |
| 2 | +const chalk = require('chalk'); |
| 3 | +const path = require('path'); |
| 4 | +const logSymbols = require('log-symbols'); |
| 5 | + |
| 6 | +const { EXT, CONFIG } = require('../constants'); |
| 7 | + |
| 8 | +class FileService { |
| 9 | + constructor(fileName) { |
| 10 | + this.fileName = fileName; |
| 11 | + this.dirPath = path.join(CONFIG.path, this.fileName); |
| 12 | + } |
| 13 | + |
| 14 | + getFilePath(ext, type) { |
| 15 | + type = type ? `.${type}` : ''; |
| 16 | + return `${this.dirPath}/${this.fileName}${type}.${ext}`; |
| 17 | + } |
| 18 | + |
| 19 | + createDir() { |
| 20 | + return fs.mkdirp(this.dirPath); |
| 21 | + } |
| 22 | + |
| 23 | + async genJs(template) { |
| 24 | + const filePath = this.getFilePath(EXT.component); |
| 25 | + if (!(await fs.pathExists(filePath))) { |
| 26 | + await fs.writeFile(filePath, template); |
| 27 | + console.log( |
| 28 | + logSymbols.success, |
| 29 | + chalk.green(`Successfully generated component file ${chalk.white(filePath)}`) |
| 30 | + ); |
| 31 | + } else { |
| 32 | + console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + async genStyle(template) { |
| 37 | + const filePath = this.getFilePath(EXT.style, 'style'); |
| 38 | + if (!(await fs.pathExists(filePath))) { |
| 39 | + await fs.writeFile(filePath, template); |
| 40 | + console.log( |
| 41 | + logSymbols.success, |
| 42 | + chalk.green(`Successfully generated style file ${chalk.white(filePath)}`) |
| 43 | + ); |
| 44 | + } else { |
| 45 | + console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + async genTest(template) { |
| 50 | + const filePath = this.getFilePath(EXT.component, 'test'); |
| 51 | + if (!(await fs.pathExists(filePath))) { |
| 52 | + await fs.writeFile(filePath, template); |
| 53 | + console.log( |
| 54 | + logSymbols.success, |
| 55 | + chalk.green(`Successfully generated test file ${chalk.white(filePath)}`) |
| 56 | + ); |
| 57 | + } else { |
| 58 | + console.log(logSymbols.warning, chalk.yellow(`File already exists ${chalk.white(filePath)}`)); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +module.exports = { |
| 64 | + FileService, |
| 65 | +}; |
0 commit comments