Skip to content

Commit

Permalink
feat: support Logger and Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
jayree committed Jun 11, 2024
1 parent 7380926 commit ceca209
Show file tree
Hide file tree
Showing 6 changed files with 934 additions and 143 deletions.
3 changes: 2 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Debug from 'debug';
export default function printChangeLog(cacheDir: string, pluginRootPath: string, debug?: Debug.Debugger): Promise<string | undefined>;
import type { Logger } from '@salesforce/core';
export default function printChangeLog(cacheDir: string, pluginRootPath: string, loggerOrDebug?: Logger | Debug.Debugger): Promise<string | undefined>;
33 changes: 28 additions & 5 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"type": "module",
"devDependencies": {
"@commitlint/config-conventional": "^19.2.2",
"@salesforce/core": "^7.3.12",
"@salesforce/dev-config": "^4.1.0",
"@salesforce/prettier-config": "^0.0.3",
"@types/debug": "^4.1.12",
Expand All @@ -32,7 +33,6 @@
"eslint-plugin-prettier": "^5.1.3",
"husky": "^9.0.11",
"mocha": "^10.4.0",
"patch-package": "^8.0.0",
"prettier": "^3.3.1",
"pretty-quick": "^4.0.0",
"sinon": "18.0.0",
Expand Down Expand Up @@ -67,7 +67,6 @@
"compile": "wireit",
"format": "wireit",
"lint": "wireit",
"prepare": "patch-package",
"test": "wireit",
"test:compile": "wireit",
"test:only": "wireit"
Expand Down
34 changes: 30 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,32 @@ import fs from 'fs-extra';
import Debug from 'debug';
import TerminalRenderer from 'marked-terminal';
import { marked, Renderer } from 'marked';
import type { Logger } from '@salesforce/core';
import { parseChangeLog } from './shared/parseChangeLog.js';

export default async function printChangeLog(
cacheDir: string,
pluginRootPath: string,
debug?: Debug.Debugger,
loggerOrDebug?: Logger | Debug.Debugger,
): Promise<string | undefined> {
if (!debug) debug = Debug(`jayree:changelog`);
let logger: Logger | { debug: (txt: unknown) => void };

if (isLogger(loggerOrDebug)) {
logger = loggerOrDebug;
} else if (isDebugDebugger(loggerOrDebug)) {
logger = {
debug(txt: unknown): void {
loggerOrDebug(txt);
},
};
} else {
const debug = Debug(`jayree:changelog`);
logger = {
debug(txt: unknown): void {
debug(txt);
},
};
}

try {
const { name, version } = (await fs.readJson(join(pluginRootPath, 'package.json'))) as {
Expand All @@ -33,7 +51,7 @@ export default async function printChangeLog(
} catch (error) {
localVersion = { version: '0.0.0' };
}
debug({ pluginRootPath, cacheDir, localVersion: localVersion.version, version });
logger.debug({ pluginRootPath, cacheDir, localVersion: localVersion.version, version });
if (localVersion.version !== version) {
const { tokens, version: parsedVersion } = parseChangeLog(changelogFile, version, localVersion.version);
marked.setOptions({
Expand All @@ -44,6 +62,14 @@ export default async function printChangeLog(
return marked.parser(tokens);
}
} catch (error) {
debug(error);
logger.debug(error);
}
}

function isLogger(obj: Logger | Debug.Debugger | undefined): obj is Logger {
return (obj && 'debug' in obj && typeof obj.debug === 'function') ?? false;
}

function isDebugDebugger(obj: Logger | Debug.Debugger | undefined): obj is Debug.Debugger {
return typeof obj === 'function' && 'namespace' in obj;
}
Loading

0 comments on commit ceca209

Please sign in to comment.