dependency-cruiser 10.3.0-beta-4 → 10.3.0-beta-8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env node
2
+ const validateNodeEnvironment = require("../src/cli/validate-node-environment");
3
+
4
+ function formatError(pError) {
5
+ process.stderr.write(pError.message);
6
+ process.exitCode = 1;
7
+ }
8
+
9
+ try {
10
+ validateNodeEnvironment();
11
+
12
+ // importing things only after the validateNodeEnv check so we can show an understandable
13
+ // error. Otherwise, on unsupported platforms we would show a stack trace, which is
14
+ // not so nice
15
+ /* eslint-disable node/global-require */
16
+ const program = require("commander");
17
+ const { version } = require("../src/meta.js");
18
+ const cli = require("../src/cli");
19
+
20
+ program
21
+ .description(
22
+ "Writes all known violations of rules in a .dependency-cruiser.js to a file.\nAlias for depcruise -c -T baseline -f .dependency-cruiser-known-violations.json [files-or-directories]\nDetails: https://github.com/sverweij/dependency-cruiser"
23
+ )
24
+ .option("-c, --config [file]", "read rules and options from [file]", true)
25
+ .option(
26
+ "-f, --output-to [file]",
27
+ "file to write output to; - for stdout",
28
+ ".dependency-cruiser-known-violations.json"
29
+ )
30
+ .version(version)
31
+ .arguments("<files-or-directories>")
32
+ .parse(process.argv);
33
+
34
+ if (Boolean(program.args[0])) {
35
+ process.exitCode = cli(program.args, {
36
+ ...program.opts(),
37
+ outputType: "baseline",
38
+ });
39
+ } else {
40
+ program.help();
41
+ }
42
+ } catch (pError) {
43
+ formatError(pError);
44
+ process.exitCode = 1;
45
+ }
@@ -28,7 +28,7 @@ try {
28
28
  )
29
29
  .option(
30
30
  "-T, --output-type <type>",
31
- "output type; e.g. err, err-html, dot, ddot, archi, flat or json",
31
+ "output type; e.g. err, err-html, dot, ddot, archi, flat, baseline or json",
32
32
  "err"
33
33
  )
34
34
  .option(
@@ -26,7 +26,7 @@ try {
26
26
  )
27
27
  .option(
28
28
  "-T, --output-type <type>",
29
- "output type; e.g. err, err-html, dot, ddot, archi, flat or json\n(default: err)"
29
+ "output type; e.g. err, err-html, dot, ddot, archi, flat, baseline or json\n(default: err)"
30
30
  )
31
31
  .option(
32
32
  "-f, --output-to <file>",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dependency-cruiser",
3
- "version": "10.3.0-beta-4",
3
+ "version": "10.3.0-beta-8",
4
4
  "description": "Validate and visualize dependencies. With your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.",
5
5
  "keywords": [
6
6
  "static analysis",
@@ -39,6 +39,7 @@
39
39
  "dependency-cruiser": "bin/dependency-cruise.js",
40
40
  "dependency-cruise": "bin/dependency-cruise.js",
41
41
  "depcruise": "bin/dependency-cruise.js",
42
+ "depcruise-baseline": "bin/depcruise-baseline.js",
42
43
  "depcruise-fmt": "bin/depcruise-fmt.js",
43
44
  "depcruise-wrap-stream-in-html": "bin/wrap-stream-in-html.js"
44
45
  },
@@ -73,7 +74,7 @@
73
74
  "check:full": "npm-run-all check test:glob test:yarn-pnp",
74
75
  "depcruise": "node ./bin/dependency-cruise.js src bin test configs types tools --config --ignore-known",
75
76
  "depcruise:all": "node ./bin/dependency-cruise.js src bin test configs types tools --config",
76
- "depcruise:baseline": "node ./bin/dependency-cruise.js src bin test configs types tools --config --output-type baseline --progress --output-to .dependency-cruiser-known-violations.json",
77
+ "depcruise:baseline": "node ./bin/depcruise-baseline.js src bin test configs types tools",
77
78
  "depcruise:explain": "node ./bin/dependency-cruise.js src bin test configs types tools --output-type err-long --config --progress none",
78
79
  "depcruise:graph:dev": "node ./bin/dependency-cruise.js bin src --prefix vscode://file/$(pwd)/ --config --output-type dot --progress cli-feedback | dot -T svg | node ./bin/wrap-stream-in-html.js | browser",
79
80
  "depcruise:graph:doc": "npm-run-all depcruise:graph:doc:json --parallel depcruise:graph:doc:fmt-* depcruise:graph:doc:samples",
@@ -121,8 +122,8 @@
121
122
  "test:yarn-pnp:run": "cd test/integration/yarn-pnp.testing-ground && yarn dependency-cruise:json",
122
123
  "test:yarn-pnp:test": "cd test/integration/yarn-pnp.testing-ground && yarn test",
123
124
  "test:yarn-pnp:cleanup": "shx rm -rf test/integration/yarn-pnp.testing-ground dependency-cruiser*.tgz",
124
- "test:load": "hyperfine --warmup 3 --runs 30 \"bin/dependency-cruise.js --validate -- src bin test configs types tools\"",
125
- "test:load:short": "hyperfine --warmup 1 --runs 5 \"bin/dependency-cruise.js --validate -- src bin test configs types tools\"",
125
+ "test:load": "hyperfine --warmup 3 --runs 30 \"bin/dependency-cruise.js --ignore-known --validate -- src bin test configs types tools\"",
126
+ "test:load:short": "hyperfine --warmup 1 --runs 5 \"bin/dependency-cruise.js --ignore-known --validate -- src bin test configs types tools\"",
126
127
  "test:watch": "mocha --watch --watch-extensions=json --reporter=min test/\\*\\*/\\*.spec.js",
127
128
  "update-dependencies": "npm-run-all upem:update upem:install build:clean build lint:fix depcruise test:cover",
128
129
  "upem:install": "npm install",
package/src/cli/index.js CHANGED
@@ -1,5 +1,3 @@
1
- const { readFileSync } = require("fs");
2
- const json5 = require("json5");
3
1
  const glob = require("glob");
4
2
  const _get = require("lodash/get");
5
3
  const _clone = require("lodash/clone");
@@ -11,7 +9,7 @@ const bus = require("../utl/bus");
11
9
  const extractTSConfig = require("../config-utl/extract-ts-config");
12
10
  const extractBabelConfig = require("../config-utl/extract-babel-config");
13
11
  const extractWebpackResolveConfig = require("../config-utl/extract-webpack-resolve-config");
14
- const makeAbsolute = require("../config-utl/make-absolute");
12
+ const extractKnownViolations = require("../config-utl/extract-known-violations");
15
13
  const validateFileExistence = require("./utl/validate-file-existence");
16
14
  const normalizeCliOptions = require("./normalize-cli-options");
17
15
  const io = require("./utl/io");
@@ -39,16 +37,15 @@ function extractResolveOptions(pCruiseOptions) {
39
37
 
40
38
  function addKnownViolations(pCruiseOptions) {
41
39
  if (pCruiseOptions.knownViolationsFile) {
42
- const lKnownViolations = json5.parse(
43
- readFileSync(makeAbsolute(pCruiseOptions.knownViolationsFile), "utf-8")
40
+ const lKnownViolations = extractKnownViolations(
41
+ pCruiseOptions.knownViolationsFile
44
42
  );
43
+
45
44
  // Check against json schema is already done in src/main/options/validate
46
45
  // so here we can just concentrate on the io
47
- if (Boolean(lKnownViolations)) {
48
- let lCruiseOptions = _clone(pCruiseOptions);
49
- _set(lCruiseOptions, "ruleSet.options.knownViolations", lKnownViolations);
50
- return lCruiseOptions;
51
- }
46
+ let lCruiseOptions = _clone(pCruiseOptions);
47
+ _set(lCruiseOptions, "ruleSet.options.knownViolations", lKnownViolations);
48
+ return lCruiseOptions;
52
49
  }
53
50
  return pCruiseOptions;
54
51
  }
@@ -130,6 +127,12 @@ function runCruise(pFileDirectoryArray, pCruiseOptions) {
130
127
  return lReportingResult.exitCode;
131
128
  }
132
129
 
130
+ /**
131
+ *
132
+ * @param {string[]} pFileDirectoryArray
133
+ * @param {import("../../types/options").ICruiseOptions} pCruiseOptions
134
+ * @returns {number}
135
+ */
133
136
  module.exports = function executeCli(pFileDirectoryArray, pCruiseOptions) {
134
137
  pCruiseOptions = pCruiseOptions || {};
135
138
  let lExitCode = 0;
@@ -0,0 +1,27 @@
1
+ const { readFileSync } = require("fs");
2
+ const json5 = require("json5");
3
+ const makeAbsolute = require("./make-absolute");
4
+
5
+ module.exports = function extractKnownViolations(pKnownViolationsFileName) {
6
+ try {
7
+ return json5.parse(
8
+ readFileSync(makeAbsolute(pKnownViolationsFileName), "utf-8")
9
+ );
10
+ // TODO: apparently node12 native coverage doesn't see this is covered with UT
11
+ // (node 14 and 16 do), so c8 doesn't either. The ignore can be removed
12
+ // once we stop supporting node 12
13
+ /* c8 ignore start */
14
+ } catch (pError) {
15
+ if (pError instanceof SyntaxError) {
16
+ throw new SyntaxError(
17
+ `'${pKnownViolationsFileName}' should be valid json\n ${pError}`
18
+ );
19
+ }
20
+ throw pError;
21
+ }
22
+ /* c8 ignore stop */
23
+
24
+ // TODO: validate the json against the schme? (might be more clear to do it here,
25
+ // even if (in context of the cli) it's done again when validating the whole
26
+ // config
27
+ };
@@ -19,24 +19,21 @@ function softenModuleViolation(
19
19
  };
20
20
  }
21
21
 
22
- // eslint-disable-next-line max-params
23
22
  function softenDependencyViolation(
24
- pRule,
25
- pModuleSource,
26
- pDependencyResolved,
23
+ pViolationKey,
27
24
  pKnownDependencyViolations,
28
25
  pSoftenedSeverity
29
26
  ) {
30
27
  return {
31
- ...pRule,
28
+ ...pViolationKey.rule,
32
29
  severity: pKnownDependencyViolations.some(
33
30
  (pKnownError) =>
34
- pKnownError.from === pModuleSource &&
35
- pKnownError.to === pDependencyResolved &&
36
- pKnownError.rule.name === pRule.name
31
+ pKnownError.from === pViolationKey.from &&
32
+ pKnownError.to === pViolationKey.to &&
33
+ pKnownError.rule.name === pViolationKey.rule.name
37
34
  )
38
35
  ? pSoftenedSeverity
39
- : pRule.severity,
36
+ : pViolationKey.rule.severity,
40
37
  };
41
38
  }
42
39
 
@@ -51,9 +48,7 @@ function softenDependencyViolations(
51
48
  ...pDependency,
52
49
  rules: pDependency.rules.map((pRule) =>
53
50
  softenDependencyViolation(
54
- pRule,
55
- pModuleSource,
56
- pDependency.resolved,
51
+ { rule: pRule, from: pModuleSource, to: pDependency.resolved },
57
52
  pKnownDependencyViolations,
58
53
  pSoftenedSeverity
59
54
  )
@@ -129,10 +129,10 @@ function extractModuleViolations(pModules, pRuleSet) {
129
129
 
130
130
  module.exports = function summarizeModules(pModules, pRuleSet) {
131
131
  const lViolations = deDuplicateViolations(
132
- extractDependencyViolations(pModules, pRuleSet).concat(
133
- extractModuleViolations(pModules, pRuleSet)
134
- )
135
- ).sort(compare.violations);
132
+ extractDependencyViolations(pModules, pRuleSet)
133
+ .concat(extractModuleViolations(pModules, pRuleSet))
134
+ .sort(compare.violations)
135
+ );
136
136
 
137
137
  return {
138
138
  violations: lViolations,
@@ -113,5 +113,6 @@ module.exports = function gatherInitialSources(
113
113
  } else {
114
114
  return pAll.concat(pathToPosix(pFileOrDirectory));
115
115
  }
116
- }, []);
116
+ }, [])
117
+ .sort();
117
118
  };
package/src/meta.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* generated - don't edit */
2
2
 
3
3
  module.exports = {
4
- version: "10.3.0-beta-4",
4
+ version: "10.3.0-beta-8",
5
5
  engines: {
6
6
  node: "^12.20||^14||>=16",
7
7
  },
@@ -64,7 +64,7 @@ function formatSummary(pSummary) {
64
64
  pSummary
65
65
  )} dependency violations (${formatMeta(pSummary)}). ${
66
66
  pSummary.totalCruised
67
- } modules, ${pSummary.totalDependenciesCruised} dependencies cruised.\n\n`;
67
+ } modules, ${pSummary.totalDependenciesCruised} dependencies cruised.\n`;
68
68
 
69
69
  return pSummary.error > 0 ? chalk.red(lMessage) : lMessage;
70
70
  }
@@ -82,6 +82,19 @@ function addExplanation(pRuleSet, pLong) {
82
82
  : (pViolation) => pViolation;
83
83
  }
84
84
 
85
+ function formatIgnoreWarning(pViolations) {
86
+ const lIgnoredViolations = pViolations.filter(
87
+ (pViolation) => pViolation.rule.severity === "ignore"
88
+ );
89
+
90
+ if (lIgnoredViolations.length > 0) {
91
+ return chalk.yellow(
92
+ `${figures.warning} ${lIgnoredViolations.length} known violations ignored. Run without --ignore-known to see them.\n`
93
+ );
94
+ }
95
+ return "";
96
+ }
97
+
85
98
  function report(pResults, pLong) {
86
99
  const lNonIgnorableViolations = pResults.summary.violations.filter(
87
100
  (pViolation) => pViolation.rule.severity !== "ignore"
@@ -92,14 +105,18 @@ function report(pResults, pLong) {
92
105
  pResults.summary.totalCruised
93
106
  } modules, ${
94
107
  pResults.summary.totalDependenciesCruised
95
- } dependencies cruised)\n\n`;
108
+ } dependencies cruised)\n${formatIgnoreWarning(
109
+ pResults.summary.violations
110
+ )}\n\n`;
96
111
  }
97
112
 
98
113
  return lNonIgnorableViolations
99
114
  .reverse()
100
115
  .map(addExplanation(pResults.summary.ruleSetUsed, pLong))
101
116
  .reduce((pAll, pThis) => `${pAll} ${formatViolation(pThis)}\n`, "\n")
102
- .concat(formatSummary(pResults.summary));
117
+ .concat(formatSummary(pResults.summary))
118
+ .concat(formatIgnoreWarning(pResults.summary.violations))
119
+ .concat(`\n`);
103
120
  }
104
121
 
105
122
  /**