@lerna/publish
Version:
Publish packages in the current project
94 lines (93 loc) • 3.51 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var profiler_exports = {};
__export(profiler_exports, {
Profiler: () => Profiler,
generateProfileOutputPath: () => generateProfileOutputPath
});
module.exports = __toCommonJS(profiler_exports);
var import_fs_extra = __toESM(require("fs-extra"));
var import_npmlog = __toESM(require("npmlog"));
var import_upath = __toESM(require("upath"));
const hrtimeToMicroseconds = (hrtime) => {
return (hrtime[0] * 1e9 + hrtime[1]) / 1e3;
};
const range = (len) => {
return Array(len).fill().map((_, idx) => idx);
};
const getTimeBasedFilename = () => {
const now = /* @__PURE__ */ new Date();
const datetime = now.toISOString().split(".")[0];
const datetimeNormalized = datetime.replace(/-|:/g, "");
return `Lerna-Profile-${datetimeNormalized}.json`;
};
function generateProfileOutputPath(outputDirectory) {
return import_upath.default.join(import_upath.default.resolve(outputDirectory || "."), getTimeBasedFilename());
}
class Profiler {
events;
logger;
outputPath;
threads;
constructor({ concurrency, log = import_npmlog.default, outputDirectory }) {
this.events = [];
this.logger = log;
this.outputPath = generateProfileOutputPath(outputDirectory);
this.threads = range(concurrency);
}
run(fn, name) {
let startTime;
let threadId;
return Promise.resolve().then(() => {
startTime = process.hrtime();
threadId = this.threads.shift();
}).then(() => fn()).then((value) => {
const duration = process.hrtime(startTime);
const event = {
name,
ph: "X",
ts: hrtimeToMicroseconds(startTime),
pid: 1,
tid: threadId,
dur: hrtimeToMicroseconds(duration)
};
this.events.push(event);
this.threads.unshift(threadId);
this.threads.sort();
return value;
});
}
output() {
return import_fs_extra.default.outputJson(this.outputPath, this.events).then(() => this.logger.info("profiler", `Performance profile saved to ${this.outputPath}`));
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Profiler,
generateProfileOutputPath
});