|
| 1 | +/** |
| 2 | + * Wrapper for native fs module to simulate fs-extra methods. |
| 3 | + * This makes it easy to switch between native fs and fs-extra. |
| 4 | + */ |
| 5 | +import fs from 'fs'; |
| 6 | +import path from 'path'; |
| 7 | + |
| 8 | +// Promisified fs methods are available in fs.promises |
| 9 | +const fsp = fs.promises; |
| 10 | + |
| 11 | +/** |
| 12 | + * Ensures that the directory exists. If the directory structure does not exist, it is created. |
| 13 | + * @param {string} dirPath |
| 14 | + */ |
| 15 | +async function ensureDir(dirPath) { |
| 16 | + try { |
| 17 | + await fsp.mkdir(dirPath, { recursive: true }); |
| 18 | + } catch (err) { |
| 19 | + if (err.code !== 'EEXIST') throw err; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Ensures that the directory exists synchronously. |
| 25 | + * @param {string} dirPath |
| 26 | + */ |
| 27 | +function ensureDirSync(dirPath) { |
| 28 | + try { |
| 29 | + fs.mkdirSync(dirPath, { recursive: true }); |
| 30 | + } catch (err) { |
| 31 | + if (err.code !== 'EEXIST') throw err; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Checks if a file or directory exists. |
| 37 | + * @param {string} fileOrDirPath |
| 38 | + * @returns {Promise<boolean>} |
| 39 | + */ |
| 40 | +async function pathExists(fileOrDirPath) { |
| 41 | + try { |
| 42 | + await fsp.access(fileOrDirPath); |
| 43 | + return true; |
| 44 | + } catch { |
| 45 | + return false; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Copies a file or directory. The directory can have contents. |
| 51 | + * @param {string} src |
| 52 | + * @param {string} dest |
| 53 | + * @param {object} [options] |
| 54 | + */ |
| 55 | +async function copy(src, dest, options = {}) { |
| 56 | + const stats = await fsp.stat(src); |
| 57 | + if (stats.isDirectory()) { |
| 58 | + await ensureDir(dest); |
| 59 | + const entries = await fsp.readdir(src); |
| 60 | + for (const entry of entries) { |
| 61 | + const srcPath = path.join(src, entry); |
| 62 | + const destPath = path.join(dest, entry); |
| 63 | + await copy(srcPath, destPath, options); |
| 64 | + } |
| 65 | + } else { |
| 66 | + await ensureDir(path.dirname(dest)); |
| 67 | + if (options.overwrite !== false) { |
| 68 | + await fsp.copyFile(src, dest); |
| 69 | + } else { |
| 70 | + const exists = await pathExists(dest); |
| 71 | + if (!exists) { |
| 72 | + await fsp.copyFile(src, dest); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Removes a file or directory. The directory can have contents. |
| 80 | + * @param {string} fileOrDirPath |
| 81 | + */ |
| 82 | +async function remove(fileOrDirPath) { |
| 83 | + try { |
| 84 | + await fsp.rm(fileOrDirPath, { recursive: true, force: true }); |
| 85 | + } catch (err) { |
| 86 | + if (err.code !== 'ENOENT') throw err; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Reads a JSON file and parses it. |
| 92 | + * @param {string} file |
| 93 | + * @param {object|string} [options] |
| 94 | + * @returns {Promise<any>} |
| 95 | + */ |
| 96 | +async function readJson(file, options) { |
| 97 | + const content = await fsp.readFile(file, options || 'utf-8'); |
| 98 | + return JSON.parse(content); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Writes an object to a JSON file. |
| 103 | + * @param {string} file |
| 104 | + * @param {any} object |
| 105 | + * @param {object|string} [options] |
| 106 | + */ |
| 107 | +async function writeJson(file, object, options = {}) { |
| 108 | + const spaces = options.spaces || 2; |
| 109 | + const str = JSON.stringify(object, null, spaces); |
| 110 | + await fsp.writeFile(file, str, options); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Reads a JSON file synchronously and parses it. |
| 115 | + * @param {string} file |
| 116 | + * @param {object|string} [options] |
| 117 | + * @returns {any} |
| 118 | + */ |
| 119 | +function readJsonSync(file, options) { |
| 120 | + const content = fs.readFileSync(file, options || 'utf-8'); |
| 121 | + return JSON.parse(content); |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Writes an object to a JSON file synchronously. |
| 126 | + * @param {string} file |
| 127 | + * @param {any} object |
| 128 | + * @param {object|string} [options] |
| 129 | + */ |
| 130 | +function writeJsonSync(file, object, options = {}) { |
| 131 | + const spaces = options.spaces || 2; |
| 132 | + const str = JSON.stringify(object, null, spaces); |
| 133 | + fs.writeFileSync(file, str, options); |
| 134 | +} |
| 135 | + |
| 136 | +// Export a combined object containing both standard fsp methods and our custom fs-extra-like methods |
| 137 | +const fsWrapper = { |
| 138 | + ...fsp, |
| 139 | + // Original sync methods if needed |
| 140 | + existsSync: fs.existsSync, |
| 141 | + readFileSync: fs.readFileSync, |
| 142 | + writeFileSync: fs.writeFileSync, |
| 143 | + readdirSync: fs.readdirSync, |
| 144 | + statSync: fs.statSync, |
| 145 | + // Custom fs-extra methods |
| 146 | + ensureDir, |
| 147 | + ensureDirSync, |
| 148 | + pathExists, |
| 149 | + copy, |
| 150 | + remove, |
| 151 | + readJson, |
| 152 | + writeJson, |
| 153 | + readJsonSync, |
| 154 | + writeJsonSync, |
| 155 | +}; |
| 156 | + |
| 157 | +export default fsWrapper; |
0 commit comments