|
| 1 | +const fs = require('fs-extra'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +class ProjectFilesManager { |
| 5 | + #projectName; |
| 6 | + |
| 7 | + constructor( |
| 8 | + /** |
| 9 | + * @type {string} |
| 10 | + */ |
| 11 | + projectName |
| 12 | + ) { |
| 13 | + this.#projectName = projectName; |
| 14 | + } |
| 15 | + |
| 16 | + static withProjectName( |
| 17 | + /** |
| 18 | + * @type {string} |
| 19 | + */ |
| 20 | + projectName |
| 21 | + ) { |
| 22 | + return new this(projectName); |
| 23 | + } |
| 24 | + |
| 25 | + getAbsoluteFilePath( |
| 26 | + /** |
| 27 | + * A file path relative to project's root path |
| 28 | + * @type {string} |
| 29 | + */ |
| 30 | + relativeFilePath |
| 31 | + ) { |
| 32 | + return path.join(process.cwd(), `${this.#projectName}/${relativeFilePath}`); |
| 33 | + } |
| 34 | + |
| 35 | + removeFiles( |
| 36 | + /** |
| 37 | + * An array of file paths relative to project's root path |
| 38 | + * @type {Array<string>} |
| 39 | + */ |
| 40 | + files |
| 41 | + ) { |
| 42 | + files.forEach((fileName) => { |
| 43 | + const absoluteFilePath = this.getAbsoluteFilePath(fileName); |
| 44 | + |
| 45 | + fs.removeSync(absoluteFilePath); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + renameFiles( |
| 50 | + /** |
| 51 | + * An array of objects containing the old and the new file names |
| 52 | + * relative to project's root path |
| 53 | + * |
| 54 | + * @type {Array<{ |
| 55 | + * oldFileName: string; |
| 56 | + * newFileName: string; |
| 57 | + * }>} |
| 58 | + */ |
| 59 | + files |
| 60 | + ) { |
| 61 | + files.forEach(({ oldFileName, newFileName }) => { |
| 62 | + const oldAbsoluteFilePath = this.getAbsoluteFilePath(oldFileName); |
| 63 | + const newAbsoluteFilePath = this.getAbsoluteFilePath(newFileName); |
| 64 | + |
| 65 | + fs.renameSync(oldAbsoluteFilePath, newAbsoluteFilePath); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + replaceFilesContent( |
| 70 | + /** |
| 71 | + * An array of objects containing the file name relative to project's |
| 72 | + * root path and the replacement patterns to be applied |
| 73 | + * |
| 74 | + * @type {Array<{ |
| 75 | + * fileName: string; |
| 76 | + * replacements: Array<{ |
| 77 | + * searchValue: string; |
| 78 | + * replaceValue: string; |
| 79 | + * }> |
| 80 | + * }>} |
| 81 | + */ |
| 82 | + files |
| 83 | + ) { |
| 84 | + files.forEach(({ fileName, replacements }) => { |
| 85 | + const absoluteFilePath = this.getAbsoluteFilePath(fileName); |
| 86 | + |
| 87 | + const contents = fs.readFileSync(absoluteFilePath, { |
| 88 | + encoding: 'utf-8', |
| 89 | + }); |
| 90 | + |
| 91 | + let replaced = contents; |
| 92 | + |
| 93 | + replacements.forEach(({ searchValue, replaceValue }) => { |
| 94 | + replaced = replaced.replace(searchValue, replaceValue); |
| 95 | + }); |
| 96 | + |
| 97 | + fs.writeFileSync(absoluteFilePath, replaced, { spaces: 2 }); |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +module.exports = ProjectFilesManager; |
0 commit comments