| 1 | /* |
| 2 | Copyright - 2021 - wwwouaiebe - Contact: https://www.ouaie.be/ |
| 3 | |
| 4 | This program is free software; |
| 5 | you can redistribute it and/or modify it under the terms of the |
| 6 | GNU General Public License as published by the Free Software Foundation; |
| 7 | either version 3 of the License, or any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | /* |
| 19 | Changes: |
| 20 | - v1.0.0: |
| 21 | - created |
| 22 | Doc reviewed 20211111 |
| 23 | */ |
| 24 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 25 | |
| 26 | import fs from 'fs'; |
| 27 | import theConfig from './Config.js'; |
| 28 | |
| 29 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 30 | /** |
| 31 | Write a file, creating before all the needed directories. |
| 32 | */ |
| 33 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 34 | |
| 35 | class FileWriter { |
| 36 | |
| 37 | /** |
| 38 | A property to share the last created directory |
| 39 | @type {String} |
| 40 | */ |
| 41 | |
| 42 | #currentDir; |
| 43 | |
| 44 | /** |
| 45 | The constructor |
| 46 | */ |
| 47 | |
| 48 | constructor ( ) { |
| 49 | Object.freeze ( this ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | Creates all the directories in the dirs parameter, starting from theConfig.destDir |
| 54 | @param {Array.<String>} dirs The directories to create |
| 55 | */ |
| 56 | |
| 57 | #createDirs ( dirs ) { |
| 58 | |
| 59 | this.#currentDir = theConfig.destDir; |
| 60 | |
| 61 | dirs.forEach ( |
| 62 | dir => { |
| 63 | this.#currentDir += dir + '/'; |
| 64 | try { |
| 65 | if ( ! fs.existsSync ( this.#currentDir ) ) { |
| 66 | fs.mkdirSync ( this.#currentDir ); |
| 67 | } |
| 68 | } |
| 69 | catch ( err ) { |
| 70 | console.error ( err ); |
| 71 | } |
| 72 | } |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | Write a file |
| 78 | @param {Array.<String>} dirs A list of directories to create. The file will be writed in the last created directory |
| 79 | @param {String} fileName The name of the file |
| 80 | @param {String} fileContent The content of the file |
| 81 | */ |
| 82 | |
| 83 | write ( dirs, fileName, fileContent ) { |
| 84 | this.#createDirs ( dirs ); |
| 85 | fs.writeFileSync ( this.#currentDir + fileName, fileContent ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | export default FileWriter; |
| 90 | |
| 91 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 92 |