dat-utils.js (3380B)
1 /** 2 * dat-utils (https://github.com/krismuniz/dat-utils/) 3 * Copyright (c) 2018 Kristian Muñiz 4 * Licensed under MIT (https://github.com/krismuniz/dat-utils/blob/master/LICENSE.md) 5 */ 6 7 /** 8 * @function getDirPaths 9 * @description Gets all paths necessary to complete the directory tree for a given path 10 * @param {string} path - the path to the directory to be created 11 * @returns {Array<string>} - an array of strings for each path in the directory-tree 12 */ 13 14 const getDirPaths = (path) => { 15 let dirs = path.split('/') 16 17 return dirs.map((v, i) => [...dirs.slice(1, i), v].map((v) => `/${v}`).join('')) 18 } 19 20 /** 21 * @function deepMkdir 22 * @description Adds a directory, and creates parent directories as needed 23 * @param {DatArchive} archive - the archive to verify 24 * @param {string} path - the path to the directory to be created 25 * @returns {Promise<void>} - an empty Promise :'( 26 */ 27 const deepMkdir = async (archive, path) => { 28 let dirs = getDirPaths(path).slice(0, -1) 29 30 for (let directoryPath of dirs) { 31 if (directoryPath !== '/') { 32 try { 33 await archive.readdir(directoryPath) 34 } catch (e) { 35 await archive.mkdir(directoryPath) 36 } 37 } 38 } 39 40 try { 41 await archive.readdir(path) 42 } catch (e) { 43 await archive.mkdir(path) 44 } 45 } 46 47 /** 48 * @function deepWriteFile 49 * @description Writes a file, creates parent directories as needed 50 * @param {DatArchive} archive - the archive to verify 51 * @param {string} path - the path to the directory to be created 52 * @returns {Promise<void>} - an empty Promise :'( 53 */ 54 const deepWriteFile = async (archive, path, data, options = { encoding: 'utf8' }) => { 55 await deepMkdir(archive, path.split('/').slice(0, -1).join('/')) 56 57 return archive.writeFile(path, data, options) 58 } 59 60 /** 61 * @function fileExists 62 * @description Check if a file exists 63 * @param {DatArchive} archive - the archive to verify 64 * @param {string} path - the path to the file 65 * @returns {Promise<boolean>} - `true` or `false` (found or not found) 66 */ 67 const fileExists = async (archive, path) => { 68 try { 69 await archive.stat(path) 70 return true 71 } catch (e) { 72 return false 73 } 74 } 75 76 /** 77 * @function writeOrModifyFile 78 * @description Writes a file or modifies it if it exists 79 * @param {DatArchive} archive - the archive to verify 80 * @param {string} path - the path to the file 81 * @param {string} data - the data to be written 82 * @returns {Promise<void>} - an empty Promise :'( 83 */ 84 const writeOrModifyFile = async (archive, path, data, options = { encoding: 'utf8' }) => { 85 if (await fileExists(archive, path)) { 86 await archive.unlink(path) 87 } 88 89 return deepWriteFile(archive, path, data, options) 90 } 91 92 /** 93 * @function copyFile 94 * @description Copy a file from one archive to another 95 * @param {DatArchive} source - the archive to copy from 96 * @param {DatArchive} target - the archive to copy to 97 * @param {string} path - the path to the file to be copied 98 * @param {object} options 99 * @returns {Promise<void>} - an empty Promise :'( 100 */ 101 const copyFile = async (source, target, path, options = { encoding: 'utf8' }) => { 102 const data = await source.readFile(path, options) 103 const newPath = options.new_path || path 104 105 if (options.override) { 106 return writeOrModifyFile(target, newPath, data, options) 107 } else { 108 return deepWriteFile(target, newPath, data, options) 109 } 110 } 111 112 export { deepMkdir, deepWriteFile, fileExists, writeOrModifyFile, copyFile }