UNPKG

applicationinsights

Version:

Microsoft Application Insights module for Node.js

82 lines 3.27 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.getShallowFileSize = exports.getShallowDirectorySizeSync = exports.getShallowDirectorySize = exports.confirmDirExists = exports.unlinkAsync = exports.readdirAsync = exports.readFileAsync = exports.writeFileAsync = exports.appendFileAsync = exports.accessAsync = exports.mkdirAsync = exports.lstatAsync = exports.statAsync = void 0; const fs = require("fs"); const path = require("path"); const util_1 = require("util"); exports.statAsync = (0, util_1.promisify)(fs.stat); exports.lstatAsync = (0, util_1.promisify)(fs.lstat); exports.mkdirAsync = (0, util_1.promisify)(fs.mkdir); exports.accessAsync = (0, util_1.promisify)(fs.access); exports.appendFileAsync = (0, util_1.promisify)(fs.appendFile); exports.writeFileAsync = (0, util_1.promisify)(fs.writeFile); exports.readFileAsync = (0, util_1.promisify)(fs.readFile); exports.readdirAsync = (0, util_1.promisify)(fs.readdir); exports.unlinkAsync = (0, util_1.promisify)(fs.unlink); /** * Validate directory exists. */ const confirmDirExists = async (directory) => { try { const stats = await (0, exports.lstatAsync)(directory); if (!stats.isDirectory()) { throw new Error("Path existed but was not a directory"); } } catch (err) { if (err && err.code === "ENOENT") { try { await (0, exports.mkdirAsync)(directory); } catch (mkdirErr) { if (mkdirErr && mkdirErr.code !== "EEXIST") { // Handle race condition by ignoring EEXIST throw mkdirErr; } } } } }; exports.confirmDirExists = confirmDirExists; /** * Computes the size (in bytes) of all files in a directory at the root level. Asynchronously. */ const getShallowDirectorySize = async (directory) => { // Get the directory listing const files = await (0, exports.readdirAsync)(directory); let totalSize = 0; // Query all file sizes for (const file of files) { const fileStats = await (0, exports.statAsync)(path.join(directory, file)); if (fileStats.isFile()) { totalSize += fileStats.size; } } return totalSize; }; exports.getShallowDirectorySize = getShallowDirectorySize; /** * Computes the size (in bytes) of all files in a directory at the root level. Synchronously. */ const getShallowDirectorySizeSync = (directory) => { const files = fs.readdirSync(directory); let totalSize = 0; for (let i = 0; i < files.length; i++) { totalSize += fs.statSync(path.join(directory, files[i])).size; } return totalSize; }; exports.getShallowDirectorySizeSync = getShallowDirectorySizeSync; /** * Computes the size (in bytes) of a file asynchronously. */ const getShallowFileSize = async (filePath) => { const fileStats = await (0, exports.statAsync)(filePath); if (fileStats.isFile()) { return fileStats.size; } }; exports.getShallowFileSize = getShallowFileSize; //# sourceMappingURL=fileSystemHelper.js.map