UNPKG

applicationinsights

Version:

Microsoft Application Insights module for Node.js

70 lines 3.01 kB
"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.renameCurrentFile = exports.makeStatusDirs = exports.homedir = void 0; const path = require("path"); const fs = require("fs"); const os = require("os"); exports.homedir = os.homedir ? os.homedir() : (process.env[(process.platform === "win32") ? "USERPROFILE" : "HOME"]); /** * Zero dependencies: recursive mkdir */ function mkDirByPathSync(HOME_DIR, targetDir, { isRelativeToScript = false } = {}) { const sep = path.sep; const initDir = path.isAbsolute(targetDir) ? sep : ""; const baseDir = isRelativeToScript ? __dirname : "."; return targetDir.split(sep).reduce((parentDir, childDir) => { const curDir = path.resolve(baseDir, parentDir, childDir); try { // Don't try to recreate homedir if (HOME_DIR.indexOf(curDir) === -1) { fs.mkdirSync(curDir); } } catch (err) { if (err.code === "EEXIST") { // curDir already exists! return curDir; } // To avoid `EISDIR` error on Mac and `EACCES`-->`ENOENT` and `EPERM` on Windows. if (err.code === "ENOENT") { // Throw the original parentDir error on curDir `ENOENT` failure. throw new Error(`EACCES: permission denied, mkdir "${parentDir}"`); } const caughtErr = ["EACCES", "EPERM", "EISDIR"].indexOf(err.code) > -1; if (!caughtErr || caughtErr && curDir === path.resolve(targetDir)) { throw err; // Throw if it's just the last created dir. } } return curDir; }, initDir); } function makeStatusDirs(filepath) { try { mkDirByPathSync(exports.homedir, filepath.replace(/\\/g, path.sep).replace(/\//g, path.sep)); return true; } catch (e) { console.error("Error creating Application Insights status folder", e); return false; } } exports.makeStatusDirs = makeStatusDirs; function renameCurrentFile(filepath, filename, callback) { const fullpath = path.join(filepath, filename); const basename = path.basename(filename, path.extname(filename)); const stats = fs.stat(fullpath, (statsErr, stats) => { if (statsErr) { return callback(statsErr); } const createDate = new Date(stats.birthtime); const destfilename = `${basename}-${createDate.toISOString().replace(/[T:\.]/g, "_").replace("Z", "")}${path.extname(filename)}.old`; const destfullpath = path.join(filepath, destfilename); fs.rename(fullpath, destfullpath, (renameErr) => { if (typeof callback === "function") { callback(renameErr, destfullpath); } }); }); } exports.renameCurrentFile = renameCurrentFile; //# sourceMappingURL=fileHelpers.js.map