1/* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const path = require('path'); 17const fs = require('fs').promises 18 19module.exports = { 20 existDir, 21 mkdir, 22 readDir, 23 clearDir, 24 deleteDir, 25 delFile, 26 renameDir, 27 moveDir, 28 moveSourceToTarget, 29 writeJsonFile, 30 isExistFile, 31 recursiveCreateFolders, 32 copyFile, 33 writeFile 34} 35 36async function existDir(folderPath) { 37 try { 38 const stats = await fs.stat(folderPath); 39 return stats.isDirectory(); 40 } catch (err) { 41 if (err.code === 'ENOENT') { 42 return false; 43 } else { 44 throw err; 45 } 46 } 47} 48 49async function mkdir(folderPath) { 50 try { 51 await fs.mkdir(folderPath, { recursive: true }); 52 } catch (error) { 53 throw error; 54 } 55} 56 57async function readDir(folderPath) { 58 try { 59 const fileNames = await fs.readdir(folderPath); 60 return fileNames.map(fileName => { 61 return path.join(folderPath, fileName); 62 }); 63 } catch (err) { 64 return Promise.reject(err); 65 } 66} 67 68async function clearDir(folderPath) { 69 let folder = await readDir(folderPath); 70 try { 71 for (let i = 0; i < folder.length; i++) { 72 await fs.rm(folder[i], { recursive: true }); 73 } 74 } catch (error) { 75 console.error(error); 76 throw error; 77 } 78} 79 80async function deleteDir(folderPath) { 81 try { 82 let files = await fs.readdir(folderPath); 83 if (files.length > 0) { 84 for (const file of files) { 85 const filePath = path.join(folderPath, file); 86 await fs.rm(filePath, { recursive: true }); 87 } 88 } 89 await fs.rmdir(folderPath); 90 } catch (err) { 91 if (err.code !== 'ENOENT') throw err; 92 } 93} 94 95async function delFile(filePath) { 96 try { 97 await fs.rm(filePath, { recursive: true }); 98 } catch (err) { 99 if (err.code !== 'ENOENT') throw err; 100 } 101} 102 103async function renameDir(oldName, newName) { 104 try { 105 await fs.rename(oldName, newName); 106 } catch (error) { 107 throw error; 108 } 109} 110 111async function moveDir(src, dest) { 112 try { 113 let fileName = src.split(path.sep)[src.split(path.sep).length - 1]; 114 const target = path.join(dest, fileName); 115 await moveSourceToTarget(src, target); 116 } catch (err) { 117 if (err.code !== 'ENOENT') throw err; 118 } 119} 120 121async function moveSourceToTarget(src, dest) { 122 try { 123 await fs.rename(src, dest); 124 } catch (err) { 125 throw err; 126 } 127} 128 129async function ensureDirectoryExists(filePath) { 130 const dirname = path.dirname(filePath); 131 try { 132 await fs.mkdir(dirname, { recursive: true }); 133 } catch (err) { 134 if (err.code !== 'EEXIST') { 135 throw err; 136 } 137 } 138} 139 140async function writeJsonFile(jsonData, jsonFilePath) { 141 try { 142 await ensureDirectoryExists(jsonFilePath); 143 await fs.writeFile(jsonFilePath, jsonData, 'utf8'); 144 } catch (err) { 145 throw err; 146 } 147} 148 149async function isExistFile(filePath) { 150 try { 151 await fs.access(filePath); 152 return true; 153 } catch (error) { 154 return false; 155 } 156} 157 158async function recursiveCreateFolders(folderPath) { 159 try { 160 await fs.mkdir(folderPath, { recursive: true }); 161 } catch (err) { 162 if (err.code !== 'EEXIST') { 163 throw err; 164 } 165 } 166} 167 168async function copyFile(source, target) { 169 try { 170 const data = await fs.readFile(source); 171 await fs.writeFile(target, data); 172 } catch (error) { 173 throw error; 174 } 175} 176 177async function writeFile(filePath, fileContext) { 178 try { 179 await fs.writeFile(filePath, fileContext); 180 } catch (error) { 181 throw error; 182 } 183}