@knapsack/app
Version:
Build Design Systems with Knapsack
184 lines (182 loc) • 6.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.log = exports.updateLogMetadata = void 0;
exports.reportError = reportError;
exports.setLogVerbose = setLogVerbose;
exports.setupUpdateNotifier = setupUpdateNotifier;
/**
* Copyright (C) 2018 Basalt
This file is part of Knapsack.
Knapsack is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
Knapsack is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with Knapsack; if not, see <https://www.gnu.org/licenses>.
*/
const update_notifier_1 = __importDefault(require("update-notifier"));
const file_utils_1 = require("@knapsack/file-utils");
const cjs_1 = require("@knapsack/logger/cjs");
const utils_1 = require("../lib/utils");
(0, cjs_1.intro)('Knapsack');
const disableTelemetryEnvVar = 'KS_DISABLE_TELEMETRY';
const isTelemetryDisabled = process.env[disableTelemetryEnvVar];
const howToMsg = isTelemetryDisabled
? `Currently disabled due to env var "${disableTelemetryEnvVar}" being set.`
: `Set env var "${disableTelemetryEnvVar}=true" to disable.`;
(0, cjs_1.write)({
msg: `Knapsack Bug Tracking and Telemetry status: "${isTelemetryDisabled ? 'off' : 'on'}". ${howToMsg}`,
msgColor: 'dim',
symbol: 'debug',
});
const showStackTracesEnvVarName = 'KS_SHOW_STACK_TRACES';
const showStackTraces = !!process.env[showStackTracesEnvVarName];
const showStackTracesMsg = `Set env var "${showStackTracesEnvVarName}=true" to show stack traces in errors. ${showStackTraces ? 'Currently enabled.' : 'Currently disabled.'}`;
(0, cjs_1.write)({
msg: showStackTracesMsg,
msgColor: 'dim',
symbol: 'debug',
});
const sendToDatadog = (0, cjs_1.createSendToDatadog)({
env: utils_1.isInMonorepo ? 'development' : 'production',
ddsource: 'nodejs',
service: 'app-client',
version: (_a = (0, file_utils_1.findUpPkgJson)(__dirname).pkg) === null || _a === void 0 ? void 0 : _a.version,
keyType: 'publicClientKey',
key: 'pub9a17ae4826c927450539436644ad7eef',
enabled: !utils_1.isInMonorepo && !isTelemetryDisabled,
});
let showVerbose = process.env.KNAPSACK_LOG_LEVEL === 'verbose';
let logMetadata = {};
const updateLogMetadata = (metadata) => {
logMetadata = { ...logMetadata, ...metadata };
};
exports.updateLogMetadata = updateLogMetadata;
function reportError(err, extraData) {
sendToDatadog({
status: 'error',
message: err.message,
error: err,
...logMetadata,
metadata: {
...(logMetadata.metadata || {}),
...(extraData || {}),
},
});
}
/**
* Turns a deeply nested Errors with `cause` into an array of messages strings
* Handles cycles in the cause chain to prevent infinite recursion.
* @example
* ```ts
* getErrorCausesMsg({
* cause: new Error('Boom', { cause: new Error('Boom 2') }),
* msgs: [],
* });
* // ['Boom', 'Boom 2']
* ```
*/
function getErrorCausesMsg({ cause, msgs, seen = new Set(), }) {
if (cause instanceof Error) {
if (seen.has(cause)) {
// Cycle detected, stop recursion
msgs.push('[circular cause detected]');
return msgs;
}
seen.add(cause);
return getErrorCausesMsg({
cause: cause.cause,
msgs: [...msgs, cause.message],
seen,
});
}
return msgs;
}
function error(err, extra, prefix) {
/**
* Any null-ish values will be removed
*/
const outputs = [];
if (err instanceof Error) {
if (showStackTraces) {
outputs.push(err);
}
else {
outputs.push(err.message, ...getErrorCausesMsg({
cause: err.cause,
msgs: [],
}));
}
}
else {
// we want single line errors to be colored
outputs.push(err.split('\n').length > 1 ? err : cjs_1.colors.error(err));
}
outputs.push(extra);
(0, cjs_1.write)({
symbol: 'error',
msg: (0, cjs_1.formatLogOutput)(outputs),
prefix,
});
reportError(err instanceof Error ? err : new Error(err), { extra });
}
function info(msg, extra, prefix = '') {
(0, cjs_1.write)({ msg: (0, cjs_1.formatLogOutput)([msg, extra]), symbol: 'info', prefix });
}
function warn(msg, extra, prefix = '') {
(0, cjs_1.write)({ msg: (0, cjs_1.formatLogOutput)([msg, extra]), symbol: 'warn', prefix });
}
function verbose(msg, extra, prefix = '') {
if (!showVerbose)
return;
(0, cjs_1.write)({
msg: (0, cjs_1.formatLogOutput)([msg, extra], {
maxPrettyLines: 30,
maxNonPrettyDepth: 11,
}),
symbol: 'debug',
prefix,
});
}
/**
* Log deeply nested object
*/
function inspect(obj, name = 'inspect', depth = 7) {
console.log();
console.log(`Start: ${name} ================`);
console.log((0, cjs_1.formatJson)(obj, { maxNonPrettyDepth: depth }));
console.log(`End: ${name} ----------------`);
console.log();
}
function setLogVerbose(isVerbose) {
showVerbose = isVerbose;
}
exports.log = {
info,
warn,
error,
verbose,
inspect,
};
function setupUpdateNotifier(pkg) {
if (!pkg.name)
return;
const notifier = (0, update_notifier_1.default)({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24 * 7, // 1 week
});
notifier.notify({
isGlobal: false,
defer: false,
message: 'Update available {currentVersion} → {latestVersion}\nRun "npx @knapsack/update@latest" to update.',
});
}
//# sourceMappingURL=log.js.map