Skip to content

Commit

Permalink
refactor(report): converts to esm (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij authored Apr 9, 2023
1 parent e33a80f commit 3c8c1d6
Show file tree
Hide file tree
Showing 69 changed files with 504 additions and 484 deletions.
4 changes: 2 additions & 2 deletions .dependency-cruiser-known-violations.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@
},
{
"type": "module",
"from": "src/utl/wrap-and-indent.js",
"to": "src/utl/wrap-and-indent.js",
"from": "src/utl/wrap-and-indent.mjs",
"to": "src/utl/wrap-and-indent.mjs",
"rule": {
"severity": "info",
"name": "utl-module-not-shared-enough"
Expand Down
1 change: 1 addition & 0 deletions .dependency-cruiser.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"^\\.yarn/cache",
"^os$",
"^path$",
"^path/posix$",
"^src/meta\\.js$",
"^src/graph-utl",
"^src/utl"
Expand Down
30 changes: 5 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"./sample-reporter-plugin": "./configs/plugins/stats-reporter-plugin.js",
"./sample-3d-reporter-plugin": "./configs/plugins/3d-reporter-plugin.js",
"./mermaid-reporter-plugin": "./src/report/mermaid.js"
"./mermaid-reporter-plugin": "./src/report/mermaid.mjs"
},
"types": "types/dependency-cruiser.d.ts",
"files": [
Expand Down Expand Up @@ -153,15 +153,15 @@
"acorn-loose": "8.3.0",
"acorn-walk": "8.2.0",
"ajv": "8.12.0",
"chalk": "^4.1.2",
"chalk": "5.2.0",
"commander": "10.0.0",
"enhanced-resolve": "5.12.0",
"figures": "^3.2.0",
"figures": "5.0.0",
"get-stream": "^6.0.1",
"glob": "9.3.2",
"handlebars": "4.7.7",
"ignore": "5.2.4",
"indent-string": "^4.0.0",
"indent-string": "5.0.0",
"interpret": "^3.1.1",
"is-installed-globally": "0.4.0",
"json5": "2.2.3",
Expand All @@ -174,7 +174,7 @@
"teamcity-service-messages": "0.1.14",
"tsconfig-paths-webpack-plugin": "4.0.1",
"watskeburt": "0.10.1",
"wrap-ansi": "^7.0.0"
"wrap-ansi": "8.1.0"
},
"devDependencies": {
"@babel/core": "7.21.4",
Expand Down Expand Up @@ -219,21 +219,6 @@
},
"upem": {
"policies": [
{
"package": "chalk",
"policy": "wanted",
"because": "version 5 only exports esm - and we use cjs and don't transpile"
},
{
"package": "figures",
"policy": "wanted",
"because": "version 4 only exports esm - and we use cjs and don't transpile"
},
{
"package": "indent-string",
"policy": "wanted",
"because": "version 5 only exports esm - and we use cjs and don't transpile"
},
{
"package": "interpret",
"policy": "wanted",
Expand All @@ -243,11 +228,6 @@
"package": "rechoir",
"policy": "wanted",
"because": "we want to keep rechoir ~similar to what webpack-cli uses (which is ^0.8.0 since 2022-11-15). See https://github.com/webpack/webpack-cli/blame/master/packages/webpack-cli/package.json"
},
{
"package": "wrap-ansi",
"policy": "wanted",
"because": "version 8 only exports esm - and we use cjs and don't transpile"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/cli/format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default async function format(pResultFile, pOptions) {

const lResult = await getStream(getInStream(pResultFile));

const lReportingResult = _format(JSON.parse(lResult), lOptions);
const lReportingResult = await _format(JSON.parse(lResult), lOptions);

write(lOptions.outputTo, lReportingResult.output);
return lReportingResult.exitCode;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/init-config/write-run-scripts-to-manifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { writeFileSync } from "node:fs";
import figures from "figures";
import chalk from "chalk";
import wrapAndIndent from "../../utl/wrap-and-indent.js";
import wrapAndIndent from "../../utl/wrap-and-indent.mjs";
import { PACKAGE_MANIFEST as _PACKAGE_MANIFEST } from "../defaults.mjs";
import { readManifest } from "./environment-helpers.mjs";
import { folderNameArrayToRE } from "./utl.cjs";
Expand All @@ -21,7 +21,7 @@ const EXPERIMENTAL_SCRIPT_DOC = [
name: "depcruise:html",
headline: "npm run depcruise:html",
description:
"validates against the rules in .dependency-cruiser.js and writes it to 'dependendency-violation-report.html' with a friendly layout",
"validates against the rules in .dependency-cruiser.js and writes it to 'dependency-violation-report.html' with a friendly layout",
},
{
name: "depcruise:graph",
Expand Down
9 changes: 5 additions & 4 deletions src/main/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-return-await */
/* eslint-disable no-magic-numbers */

import Ajv from "ajv";
Expand Down Expand Up @@ -41,13 +42,13 @@ function validateResultAgainstSchema(pResult) {
}
}
/** @type {import("../../types/dependency-cruiser.js").format} */
export function format(pResult, pFormatOptions = {}) {
export async function format(pResult, pFormatOptions = {}) {
const lFormatOptions = normalizeFormatOptions(pFormatOptions);
validateFormatOptions(lFormatOptions);

validateResultAgainstSchema(pResult);

return reportWrap(pResult, lFormatOptions);
return await reportWrap(pResult, lFormatOptions);
}

/** @type {import("../../types/dependency-cruiser.js").cruise} */
Expand Down Expand Up @@ -78,7 +79,7 @@ export async function cruise(

if (lCache.canServeFromCache(lCruiseOptions, lCachedResults)) {
bus.emit("progress", "cache: reporting from cache", c(8));
return reportWrap(lCachedResults, lCruiseOptions);
return await reportWrap(lCachedResults, lCruiseOptions);
}
}

Expand Down Expand Up @@ -121,7 +122,7 @@ export async function cruise(
}

bus.emit("progress", "reporting", c(8));
return reportWrap(lCruiseResult, lCruiseOptions);
return await reportWrap(lCruiseResult, lCruiseOptions);
}

export {
Expand Down
2 changes: 1 addition & 1 deletion src/main/options/validate.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import has from "lodash/has.js";
import merge from "lodash/merge.js";
import safeRegex from "safe-regex";
import report from "../../report/index.js";
import report from "../../report/index.mjs";

const MODULE_SYSTEM_LIST_RE = /^((cjs|amd|es6|tsd)(,|$))+$/gi;
const VALID_DEPTH_RE = /^\d{1,2}$/g;
Expand Down
6 changes: 3 additions & 3 deletions src/main/report-wrap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import summarize from "../enrich/summarize/index.js";
import consolidateToPattern from "../graph-utl/consolidate-to-pattern.js";
import compare from "../graph-utl/compare.js";
import stripSelfTransitions from "../graph-utl/strip-self-transitions.js";
import report from "../report/index.js";
import report from "../report/index.mjs";

/**
*
Expand Down Expand Up @@ -45,8 +45,8 @@ function reSummarizeResults(pResult, pFormatOptions) {
* @param {import("../../types/dependency-cruiser.js").IFormatOptions} pFormatOptions
* @returns {import("../../types/dependency-cruiser.js").IReporterOutput}
*/
export default function reportWrap(pResult, pFormatOptions) {
const lReportFunction = report.getReporter(pFormatOptions.outputType);
export default async function reportWrap(pResult, pFormatOptions) {
const lReportFunction = await report.getReporter(pFormatOptions.outputType);
const lReportOptions =
pResult.summary.optionsUsed?.reporterOptions?.[pFormatOptions.outputType] ??
{};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const memoize = require("lodash/memoize");
const randomString = require("./random-string");
import memoize from "lodash/memoize.js";
import randomString from "./random-string.mjs";

function replace(pElement, pIndex, pWordList) {
return pIndex === 0 ? pWordList.shift() || randomString(pElement) : pElement;
Expand Down Expand Up @@ -45,7 +45,7 @@ function replaceFromWordList(pPathElement, pWordList, pCached) {
* @param {boolean} pCached caches the replaced value, with pPathElement as the key
* @return {string} the path element, anonymized if it isn't whitelisted
*/
function anonymizePathElement(
export function anonymizePathElement(
pPathElement,
pWordList = [],
pWhiteListRE = /^$/g,
Expand All @@ -56,14 +56,12 @@ function anonymizePathElement(
: replaceFromWordList(pPathElement, pWordList, pCached);
}

module.exports = anonymizePathElement;

/**
* Clears the path element => replaced value cache
* Here for testing purposes.
*
* @returns {void}
*/
module.exports.clearCache = () => {
export function clearCache() {
replaceCached.cache.clear();
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const anonymizePathElement = require("./anonymize-path-element");
import { anonymizePathElement } from "./anonymize-path-element.mjs";

const WHITELIST_RE =
export const WHITELIST_RE =
// eslint-disable-next-line security/detect-unsafe-regex, unicorn/no-unsafe-regex
/^(|[.]+|~|bin|apps?|cli|src|libs?|configs?|components?|fixtures?|helpers?|i18n|index\.(jsx?|[mc]js|d\.ts|tsx?|vue|coffee|ls)|_?_?mocks?_?_?|node_modules|packages?|package\.json|scripts?|services?|sources?|specs?|_?_?tests?_?_?|types?|uti?ls?)$/;

Expand All @@ -21,7 +21,7 @@ const WHITELIST_RE =
* modifier on it.
* @returns {string} - anonymized path
*/
module.exports = function anonymizePath(
export function anonymizePath(
pPath,
pWordList = [],
pWhiteListRE = WHITELIST_RE
Expand All @@ -32,6 +32,4 @@ module.exports = function anonymizePath(
anonymizePathElement(pPathElement, pWordList, pWhiteListRE)
)
.join("/");
};

module.exports.WHITELIST_RE = WHITELIST_RE;
}
36 changes: 16 additions & 20 deletions src/report/anon/index.js → src/report/anon/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const clone = require("lodash/clone");
const get = require("lodash/get");
const has = require("lodash/has");
const anonymizePath = require("./anonymize-path");
import clone from "lodash/clone.js";
import has from "lodash/has.js";
import { anonymizePath, WHITELIST_RE } from "./anonymize-path.mjs";

function anonymizePathArray(pPathArray, pWordList) {
return (pPathArray || []).map((pPath) => anonymizePath(pPath, pWordList));
Expand Down Expand Up @@ -33,9 +32,9 @@ function anonymizeReaches(pReachesArray, pWordList) {

/**
*
* @param {import("../../../types/cruise-result").IModule[]} pModules
* @param {import("../../../types/cruise-result.js").IModule[]} pModules
* @param {string[]} pWordList
* @returns {import("../../../types/cruise-result").IModule[]}
* @returns {import("../../../types/cruise-result.js").IModule[]}
*/
function anonymizeModules(pModules, pWordList) {
return pModules.map((pModule) => {
Expand All @@ -59,9 +58,9 @@ function anonymizeModules(pModules, pWordList) {
}
/**
*
* @param {import("../../../types/cruise-result").IFolder[]} pFolders
* @param {import("../../../types/cruise-result.js").IFolder[]} pFolders
* @param {string[]} pWordList
* @returns {import("../../../types/cruise-result").IFolder[]}
* @returns {import("../../../types/cruise-result.js").IFolder[]}
*/
function anonymizeFolders(pFolders, pWordList) {
return pFolders.map((pFolder) => {
Expand Down Expand Up @@ -110,9 +109,9 @@ function anonymizeViolations(pViolations, pWordList) {
}
/**
*
* @param {import("../../../types/cruise-result").ICruiseResult} pResults
* @param {import("../../../types/cruise-result.js").ICruiseResult} pResults
* @param {string[]} pWordList
* @returns {import("../../../types/cruise-result").ICruiseResult}
* @returns {import("../../../types/cruise-result.js").ICruiseResult}
*/
function anonymize(pResults, pWordList) {
const lResults = clone(pResults);
Expand All @@ -133,7 +132,7 @@ function sanitizeWordList(pWordList) {
return pWordList
.map((pString) => pString.replace(/[^a-zA-Z-]/g, "_"))
.filter((pString) => pString.match(/^[a-zA-Z-_]+$/g))
.filter((pString) => !pString.match(anonymizePath.WHITELIST_RE));
.filter((pString) => !pString.match(WHITELIST_RE));
}

/**
Expand All @@ -150,23 +149,20 @@ function sanitizeWordList(pWordList) {
* (note: the algorith _removes_ elements from pWordList to prevent duplicates,
* so if the word list is precious to you - pass a clone)
*
* @param {import("../../../types/cruise-result").ICruiseResult} pResults - the output of a dependency-cruise adhering to ../schema/cruise-result.schema.json
* @param {import("../../../types/cruise-result.js").ICruiseResult} pResults - the output of a dependency-cruise adhering to ../schema/cruise-result.schema.json
* @param {{wordlist?: String[]}} pAnonymousReporterOptions of words to use as replacement strings. If
* not passed the reporter uses the string passed
* in the options (reporterOptions.anon.wordlist)
* or - if that doesn't exist - the empty array
* @returns {import("../../..").IReporterOutput} - output: the results in JSON format (hence adhering to the same json schema)
* @returns {import("../../../types/dependency-cruiser.js").IReporterOutput} - output: the results in JSON format (hence adhering to the same json schema)
* exitCode: 0
*/
module.exports = function reportAnonymous(pResults, pAnonymousReporterOptions) {
export default function reportAnonymous(pResults, pAnonymousReporterOptions) {
/** @type {{wordlist?: String[]}} */
let lAnonymousReporterOptions = pAnonymousReporterOptions || {};
if (!has(lAnonymousReporterOptions, "wordlist")) {
lAnonymousReporterOptions.wordlist = get(
pResults,
"summary.optionsUsed.reporterOptions.anon.wordlist",
[]
);
lAnonymousReporterOptions.wordlist =
pResults?.summary?.optionsUsed?.reporterOptions?.anon?.wordlist ?? [];
}
return {
output: JSON.stringify(
Expand All @@ -176,4 +172,4 @@ module.exports = function reportAnonymous(pResults, pAnonymousReporterOptions) {
),
exitCode: 0,
};
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const random = require("lodash/random");
import random from "lodash/random.js";

const NUMBER = 0;
const SEPARATOR = 1;
Expand Down Expand Up @@ -39,6 +39,7 @@ function getRandomChar(pChar) {
return lLowerCaseChars[random(0, lLowerCaseChars.length - 1)];
}
}

/**
* Returns a random string with the same length as pString
* acii characters - respecting case & numbers + leaving separators
Expand All @@ -52,6 +53,6 @@ function getRandomChar(pChar) {
* @param {string} pString - any string
* @return {string} - a random string with above specs
*/
module.exports = function getRandomString(pString) {
export default function getRandomString(pString) {
return Array.from(pString).map(getRandomChar).join("");
};
}
8 changes: 4 additions & 4 deletions src/report/baseline.js → src/report/baseline.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const DEFAULT_JSON_INDENT = 2;
/**
* Sample plugin
*
* @param {import('../..').ICruiseResult} pCruiseResult -
* @param {import('../../types/dependency-cruiser').ICruiseResult} pCruiseResult -
* the output of a dependency-cruise adhering to dependency-cruiser's
* cruise result schema
* @return {import('../..').IReporterOutput} -
* @return {import('../../types/dependency-cruiser').IReporterOutput} -
* output: some stats on modules and dependencies in json format
* exitCode: 0
*/
module.exports = function baseline(pCruiseResult) {
export default function baseline(pCruiseResult) {
return {
output: JSON.stringify(
pCruiseResult.summary.violations,
Expand All @@ -19,4 +19,4 @@ module.exports = function baseline(pCruiseResult) {
),
exitCode: 0,
};
};
}
Loading

0 comments on commit 3c8c1d6

Please sign in to comment.