async getFiles(dir, fileList = []) { const files = await fs.readdir(path.join(this.localPath(), dir)); // eslint-disable-next-line no-restricted-syntax for (const file of files) { const stat = await fs.stat(path.join(this.localPath(), dir, file)); if (stat.isDirectory()) { fileList = await this.getFiles(path.join(dir, file), fileList); } else fileList.push(path.join(dir, file)); } return fileList; }
async fileHashes(directory) { directory = directory || this.directory; let result = {}; const files = await fs.readdir(directory); // eslint-disable-next-line no-restricted-syntax for (const file of files) { const filePath = path.resolve(directory, file); if (!this.filter(filePath)) { // eslint-disable-next-line no-continue continue; } const stat = await fs.stat(filePath); if (stat.isDirectory()) { result = { ...result, ...await this.fileHashes(filePath) }; } else { result[path.relative(this.directory, filePath)] = { hash: await this.fileHash(filePath) }; } } return result; }
async readModuleFiles(modulePath) { const files = await fs.readdir(modulePath); return (await Promise.all( files.map(async file => { const fileName = path.join(modulePath, file); const stats = await fs.lstat(fileName); if (stats.isDirectory()) { return this.readModuleFiles(fileName); } else if (R.endsWith(".js", file)) { const content = await fs.readFile(fileName, "utf-8"); return [ { fileName, content, readOnly: true } ]; } else { return []; } }) )).reduce((a, b) => a.concat(b), []); }
async function enumerateTestFiles (dir) { let tests = []; if (fs.existsSync(dir)) { tests = (await fs.readdir(dir)).filter((x) => { return x.indexOf('.xctest') !== -1; }); } return tests; }
async removeOldVersions({instance}, task) { const semver = require('semver'); const versionDirs = await fs.readdir(path.join(instance.dir, 'versions')); const versions = versionDirs.filter(semver.valid).sort(semver.compare); if (versions.length <= 2) { task.skip(); return; } const promises = versions.slice(0, -2) .map(version => fs.remove(path.join(instance.dir, 'versions', version))); await Promise.all(promises); }
fs.readdir(handlers_path, (err, items) => { if(err) throw "Unable to read handlers folder"; items.forEach(item => { if(path.extname(item) == '.js'){ let handler = require(path.resolve(handlers_path, item)); handlers.push(handler); } }); });
// Routes /* @method GET @path '/posts/all' @access Authorized @desc Get all posts names. */ router.get('/posts/all', jwtMiddleware, (req, res) => { let hexoRootDir = req.app.get('config').hexo_dir; fs.readdir(path.resolve(hexoRootDir, 'source', '_posts'), (err, files) => { if (err) return res.status(500).json({'code': Status.HEXO_FAILED_TO_GET_POSTS}); else { let posts = files.filter(file => path.extname(file) === '.md'); posts = posts.map(post => path.basename(post, '.md')); return res.json(posts); } }); });
async getNextMessageId() { const files = await fs.readdir(this.pendingDir); if (files.length > 0) { const nextMessageId = files[0]; const isClaimedFile = await this.tryClaimMessage(nextMessageId); if (isClaimedFile) { return nextMessageId; } return this.getNextMessageId(); } await waitToNewFile(this.pendingDir); return this.getNextMessageId(); }
function getCover(folder){ let placeholder = 'icon/icon_512.png' htEntities.coverPic.src = placeholder fs.readdir(folder, function(err, files){ let cover = placeholder if(!err){ files.forEach(function(file){ let split = file.split('.') if(split.length === 2 && split[0].toLowerCase() === 'cover'){ cover = path.join(folder, file) } }) } htEntities.coverPic.src = cover }) }
/** * @param {object} data * @param {string} data.directory Absolute path to directory to clean * @returns {Promise<void>} */ cleanDirectory(data) { return Promise.resolve() .then(() => fs.readdir(data.directory)) .then(fileNames => { const promises = []; for (let fileName of fileNames) { if (!this.isAuxFile(fileName)) continue; promises.push(fs.remove(path.join(data.directory, fileName))); } return Promise.all(promises); }); }
/** * Wrapper for promisified `fs.readdir`. * @param {string|object} options * @returns {promise} */ readdir() { return this.fs.readdir(this.pathName, ...arguments); }
fs.readdir(self.__path, function (err, files) { if (err || files.length === 0) { return; } files.forEach(function (file) { if (file[0] != '.') { fs.stat(path.join(self.__path, file), function (err, stat) { if (!err) { if (stat.isFile() && ((new Date()).getTime() - stat.atime.getTime() > maxAge)) { self.destroy(file); } } }); } }); });
/** * Read all contend in given path * * @param {string} path */ readFolder(path) { return new Promise(resolve => { fse.readdir(path, function(err, content) { if (err) { console.log(err); resolve(false); } else { resolve(content); } }); }).then(data => { return data; }); }
/** Loads the commands for the discord bot to use in /bot/commands * into an array defined before the calling of this function * @returns {void} */ async loadCommands() { fs.readdir(`${__dirname}/../bot/commands`, (err, files) => { /** Commands are pushed to an array */ files.forEach(file => { if (file.toString().includes('.js')) { // eslint-disable-next-line global-require this.commands.push(require(`${__dirname}/../bot/commands/${file.toString()}`)); this.log.verbose(`Loaded Command: ${file.toString()}`); } }); }); }
async unclaimMessages() { const files = await fs.readdir(this.workingDir); return Promise.all(files.map((filename) => { const workingFilepath = this.getWorkingMessagePath(filename); const pendingFilepath = this.getMessagePath(filename); return fs.rename(workingFilepath, pendingFilepath); })); }