Skip to content

Commit

Permalink
feat: introduce log-level config option
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 25, 2023
1 parent fb95190 commit 3fb6584
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 92 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ Alternatively some options may be set via CLI flags.

### Options

| Option | Type | CLI Flag | Description |
| ------ | ---- | -------- | ----------- |
| dryRun | `boolean` | `--dry-run` | Dry run mode. |
| debug | `boolean` | `--debug` | Output debugging information. |
| silent | `boolean` | `--silent` | Do not print configuration information. |
| extends | `String \| Array` | N/A | List of modules or file paths containing a shareable configuration. If multiple shareable configurations are set, they will be imported in the order defined with each configuration option taking precedence over the options defined in the previous. |
| sequentialInit | `boolean` | `--sequential-init` | Avoid hypothetical concurrent initialization collisions. |
| sequentialPrepare | `boolean` | `--sequential-prepare` | Avoid hypothetical concurrent preparation collisions. **True by default.** |
| firstParent | `boolean` | `--first-parent` | Apply commit filtering to current branch only. |
| ignorePrivate | `boolean` | `--ignore-private` | Exclude private packages. **True by default.** |
| ignorePackages | `String \| Array` | `--ignore-packages` | Packages list to be ignored on bumping process (appended to the ones that already exist at package.json workspaces). If using the CLI flag, supply a comma seperated list of strings. |
| tagFormat | `String` | `--tag-format` | Format to use when creating tag names. Should include "name" and "version" vars. Default: `"${name}@${version}"` which generates "package-name@1.0.0" |
| deps | `Object` | N/A | Depedency handling, see below for possible values. |
| Option | Type | CLI Flag | Description |
|-------------------|-------------------|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| dryRun | `boolean` | `--dry-run` | Dry run mode. |
| logLevel | `String` | `--log-level` | Sets the internal logger verbosity level: `error, warn, info, debug, trace`. Defaults to `info`. |
| debug | `boolean` | `--debug` | Output debugging information. Shortcut for `--logLevel=debug`. |
| silent | `boolean` | `--silent` | Do not print configuration information. Shortcut for `--logLevel=error`. |
| extends | `String \| Array` | N/A | List of modules or file paths containing a shareable configuration. If multiple shareable configurations are set, they will be imported in the order defined with each configuration option taking precedence over the options defined in the previous. |
| sequentialInit | `boolean` | `--sequential-init` | Avoid hypothetical concurrent initialization collisions. |
| sequentialPrepare | `boolean` | `--sequential-prepare` | Avoid hypothetical concurrent preparation collisions. **True by default.** |
| firstParent | `boolean` | `--first-parent` | Apply commit filtering to current branch only. |
| ignorePrivate | `boolean` | `--ignore-private` | Exclude private packages. **True by default.** |
| ignorePackages | `String \| Array` | `--ignore-packages` | Packages list to be ignored on bumping process (appended to the ones that already exist at package.json workspaces). If using the CLI flag, supply a comma seperated list of strings. |
| tagFormat | `String` | `--tag-format` | Format to use when creating tag names. Should include "name" and "version" vars. Default: `"${name}@${version}"` which generates "package-name@1.0.0" |
| deps | `Object` | N/A | Dependency handling, see below for possible values. |

### `deps` Options

Expand Down
6 changes: 3 additions & 3 deletions lib/createInlinePluginCreator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import dbg from "debug";
import getCommitsFiltered from "./getCommitsFiltered.js";
import { updateManifestDeps, resolveReleaseType } from "./updateDeps.js";
import { logger } from "./logger.js";

const debug = dbg("msr:inlinePlugin");
const { debug } = logger.withScope("msr:inlinePlugin");

/**
* Create an inline plugin creator for a multirelease.
Expand Down Expand Up @@ -49,7 +49,7 @@ function createInlinePluginCreator(packages, multiContext, flags) {
Object.assign(context.options, context.options._pkgOptions);

// And bind the actual logger.
Object.assign(pkg.loggerRef, context.logger);
Object.assign(pkg.fakeLogger, context.logger);

const res = await plugins.verifyConditions(context);
pkg._ready = true;
Expand Down
4 changes: 2 additions & 2 deletions lib/getCommitsFiltered.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import getStream from "get-stream";
import { execa } from "execa";
import { check, ValueError } from "./blork.js";
import cleanPath from "./cleanPath.js";
import dbg from "debug";
import { logger } from "./logger.js";

const debug = dbg("msr:commitsFilter");
const { debug } = logger.withScope("msr:commitsFilter");

/**
* Retrieve the list of commits on the current branch since the commit sha associated with the last release, or all the commits of the current branch if there is no last released version.
Expand Down
5 changes: 3 additions & 2 deletions lib/getConfigSemantic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import semanticGetConfig from "semantic-release/lib/get-config.js";
import { WritableStreamBuffer } from "stream-buffers";
import { logger } from "./logger.js";
import signale from "signale";

const { Signale } = signale;
Expand All @@ -14,7 +15,7 @@ const { Signale } = signale;
*
* @internal
*/
async function getConfigSemantic({ cwd, env, stdout, stderr, logger }, options) {
async function getConfigSemantic({ cwd, env, stdout, stderr }, options) {
try {
// Blackhole logger (so we don't clutter output with "loaded plugin" messages).
const blackhole = new Signale({ stream: new WritableStreamBuffer() });
Expand All @@ -24,7 +25,7 @@ async function getConfigSemantic({ cwd, env, stdout, stderr, logger }, options)
} catch (error) {
// Log error and rethrow it.
// istanbul ignore next (not important)
logger.error(`Error in semantic-release getConfig(): %0`, error);
logger.failure(`Error in semantic-release getConfig(): %0`, error);
// istanbul ignore next (not important)
throw error;
}
Expand Down
29 changes: 0 additions & 29 deletions lib/getLogger.js

This file was deleted.

7 changes: 0 additions & 7 deletions lib/glob.js

This file was deleted.

68 changes: 68 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import dbg from "debug";
import singnale from "signale";

const { Signale } = singnale;
const severityOrder = ["error", "warn", "info", "debug", "trace"];
const assertLevel = (level, limit) => severityOrder.indexOf(level) <= severityOrder.indexOf(limit);
const aliases = {
failure: "error",
log: "info",
success: "info",
complete: "info",
};

export const logger = {
prefix: "msr:",
config: {
_level: "info",
_stderr: process.stderr,
_stdout: process.stdout,
_signale: {},
set level(l) {
if (assertLevel(l, "debug")) {
dbg.enable("msr:");
}
if (assertLevel(l, "trace")) {
dbg.enable("semantic-release:");
}
this._level = l;
},
get level() {
return this._level;
},
set stdio([stderr, stdout]) {
this._stdout = stdout;
this._stderr = stderr;
this._signale = new Signale({
config: { displayTimestamp: true, displayLabel: false },
// scope: "multirelease",
stream: stdout,
types: {
error: { color: "red", label: "", stream: [stderr] },
log: { color: "magenta", label: "", stream: [stdout], badge: "•" },
success: { color: "green", label: "", stream: [stdout] },
complete: { color: "green", label: "", stream: [stdout], badge: "🎉" },
},
});
},
get stdio() {
return [this._stderr, this._stdout];
},
},
withScope(prefix) {
return {
...this,
prefix,
debug: dbg(prefix || this.prefix),
};
},
...[...severityOrder, ...Object.keys(aliases)].reduce((m, l) => {
m[l] = function (...args) {
if (assertLevel(aliases[l] || l, this.config.level)) {
(this.config._signale[l] || console[l] || (() => {}))(this.prefix, ...args);
}
};
return m;
}, {}),
debug: dbg("msr:"),
};
12 changes: 7 additions & 5 deletions lib/multiSemanticRelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { uniq, template, sortBy } from "lodash-es";
import { topo } from "@semrel-extra/topo";
import { dirname, join } from "path";
import { check } from "./blork.js";
import getLogger from "./getLogger.js";
import { logger } from "./logger.js";
import getConfig from "./getConfig.js";
import getConfigMultiSemrel from "./getConfigMultiSemrel.js";
import getConfigSemantic from "./getConfigSemantic.js";
Expand Down Expand Up @@ -52,6 +52,9 @@ async function multiSemanticRelease(
{ cwd = process.cwd(), env = process.env, stdout = process.stdout, stderr = process.stderr } = {},
_flags
) {
// Setup logger.
logger.config.stdio = [stderr, stdout];

// Check params.
if (paths) {
check(paths, "paths: string[]");
Expand Down Expand Up @@ -92,7 +95,6 @@ async function multiSemanticRelease(
paths = paths || Object.values(_packages).map((pkg) => pkg.manifestPath);

// Start.
const logger = getLogger({ stdout, stderr });
logger.complete(`Started multirelease! Loading ${paths.length} packages...`);

// Load packages from paths.
Expand Down Expand Up @@ -164,14 +166,14 @@ async function getPackage(path, { globalOptions, inputOptions, env, cwd, stdout,
const finalOptions = Object.assign({}, globalOptions, pkgOptions, inputOptions);

// Make a fake logger so semantic-release's get-config doesn't fail.
const logger = { error() {}, log() {} };
const fakeLogger = { error() {}, log() {} };

// Use semantic-release's internal config with the final options (now we have the right `options.plugins` setting) to get the plugins object and the options including defaults.
// We need this so we can call e.g. plugins.analyzeCommit() to be able to affect the input and output of the whole set of plugins.
const { options, plugins } = await getConfigSemantic({ cwd: dir, env, stdout, stderr, logger }, finalOptions);
const { options, plugins } = await getConfigSemantic({ cwd: dir, env, stdout, stderr }, finalOptions);

// Return package object.
return { path, dir, name, manifest, deps, options, plugins, loggerRef: logger };
return { path, dir, name, manifest, deps, options, plugins, fakeLogger: fakeLogger };
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/updateDeps.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { writeFileSync } from "fs";
import dbg from "debug";
import semver from "semver";
import { isObject, isEqual, transform } from "lodash-es";
import recognizeFormat from "./recognizeFormat.js";
import getManifest from "./getManifest.js";
import { getHighestVersion, getLatestVersion } from "./utils.js";
import { getTags } from "./git.js";
import { logger } from "./logger.js";

const debug = dbg("msr:updateDeps");
const { debug } = logger.withScope("msr:updateDeps");

/**
* Resolve next package version.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"execa": "^6.1.0",
"get-stream": "^6.0.1",
"git-log-parser": "^1.2.0",
"globby": "13.1.4",
"lodash-es": "^4.17.21",
"meow": "^10.1.2",
"promise-events": "^0.2.4",
Expand Down
29 changes: 1 addition & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2956,17 +2956,6 @@ fast-glob@^3.1.1:
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.2.11:
version "3.2.11"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"
integrity sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.2.12:
version "3.2.12"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.12.tgz#7f39ec99c2e6ab030337142da9e0c18f37afae80"
Expand Down Expand Up @@ -3272,17 +3261,6 @@ globals@^13.19.0:
dependencies:
type-fest "^0.20.2"

globby@13.1.4:
version "13.1.4"
resolved "https://registry.yarnpkg.com/globby/-/globby-13.1.4.tgz#2f91c116066bcec152465ba36e5caa4a13c01317"
integrity sha512-iui/IiiW+QrJ1X1hKH5qwlMQyv34wJAYwH1vrf8b9kBA4sNiif3gKsMHa+BrdnOpEudWjpotfa7LrTzB1ERS/g==
dependencies:
dir-glob "^3.0.1"
fast-glob "^3.2.11"
ignore "^5.2.0"
merge2 "^1.4.1"
slash "^4.0.0"

globby@^11.0.0, globby@^11.0.1:
version "11.0.4"
resolved "https://registry.yarnpkg.com/globby/-/globby-11.0.4.tgz#2cbaff77c2f2a62e71e9b2813a67b97a3a3001a5"
Expand Down Expand Up @@ -4618,7 +4596,7 @@ merge-stream@^2.0.0:
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==

merge2@^1.3.0, merge2@^1.4.1:
merge2@^1.3.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==
Expand Down Expand Up @@ -5906,11 +5884,6 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

slash@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-4.0.0.tgz#2422372176c4c6c5addb5e2ada885af984b396a7"
integrity sha512-3dOsAHXXUkQTpOYcoAxLIorMTp4gIQr5IW3iVb7A7lFIp0VHhnynm9izx6TssdrIcVIESAlVjtnO2K8bg+Coew==

smart-buffer@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/smart-buffer/-/smart-buffer-4.2.0.tgz#6e1d71fa4f18c05f7d0ff216dd16a481d0e8d9ae"
Expand Down

0 comments on commit 3fb6584

Please sign in to comment.