applicationinsights
Version:
Microsoft Application Insights module for Node.js
93 lines • 4.1 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.renameCurrentFile = exports.makeStatusDirs = exports.homedir = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const os = __importStar(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
;