Skip to content

Commit

Permalink
refactor(cli): moves validations that should be in main over there
Browse files Browse the repository at this point in the history
BREAKING CHANGE: renamed the --system option --module-systems

it's clearer and simplified the code a bit
  • Loading branch information
sverweij committed Nov 4, 2017
1 parent ae6cfc4 commit c99a463
Show file tree
Hide file tree
Showing 39 changed files with 377 additions and 356 deletions.
2 changes: 1 addition & 1 deletion .istanbul.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
instrumentation:
excludes: ["test/**/*", "src/cli/index.js", "src/report/*.template.js", "coverage/**/*", "tmp*", "utl/**/*"]
excludes: ["test/**/*", "src/report/*.template.js", "coverage/**/*", "tmp*", "utl/**/*"]
include-all-sources: true
56 changes: 40 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ clean:
src/main/index.js: \
src/extract/index.js \
src/extract/transpile/meta.js \
src/main/normalizeOptions.js \
src/main/validateOptions.js \
src/report/csvReporter.js \
src/report/dotReporter.js \
src/report/errReporter.js \
Expand Down Expand Up @@ -219,6 +221,9 @@ src/validate/readRuleSet.js: \
src/validate/validateRuleSet.js: \
src/validate/jsonschema.json

src/main/normalizeOptions.js: \
src/main/defaults.json

# cjs dependencies
ALL_SRC=src/main/index.js \
package.json \
Expand All @@ -245,6 +250,9 @@ ALL_SRC=src/main/index.js \
src/extract/transpile/meta.js \
src/extract/transpile/tryRequire.js \
src/extract/transpile/typeScriptWrap.js \
src/main/defaults.json \
src/main/normalizeOptions.js \
src/main/validateOptions.js \
src/report/csv.template.js \
src/report/csvReporter.js \
src/report/dependencyToIncidenceTransformer.js \
Expand All @@ -269,6 +277,8 @@ src/cli/formatMetaInfo.js: \
src/main/index.js: \
src/extract/index.js \
src/extract/transpile/meta.js \
src/main/normalizeOptions.js \
src/main/validateOptions.js \
src/report/csvReporter.js \
src/report/dotReporter.js \
src/report/errReporter.js \
Expand Down Expand Up @@ -357,31 +367,39 @@ src/validate/readRuleSet.js: \
src/validate/validateRuleSet.js: \
src/validate/jsonschema.json

test/cli/initRules.spec.js: \
src/cli/initRules.js \
src/validate/jsonschema.json \
test/cli/deleteDammit.utl.js
src/main/normalizeOptions.js: \
src/main/defaults.json

src/cli/initRules.js: \
src/cli/rules.starter.json

test/cli/normalizeOptions.spec.js: \
src/cli/normalizeOptions.js

test/cli/processCLI.spec.js: \
src/cli/processCLI.js \
test/cli/index.spec.js: \
src/cli/index.js \
test/cli/deleteDammit.utl.js \
test/utl/testutensils.js

src/cli/processCLI.js: \
src/cli/index.js: \
src/cli/defaults.json \
src/cli/formatMetaInfo.js \
src/cli/initRules.js \
src/cli/normalizeOptions.js \
src/cli/validateParameters.js \
src/cli/validateFileShizzle.js \
src/main/index.js

test/cli/validateParameters.spec.js: \
src/cli/validateParameters.js
src/cli/initRules.js: \
src/cli/defaults.json \
src/cli/rules.starter.json

src/cli/normalizeOptions.js: \
src/cli/defaults.json

test/cli/initRules.spec.js: \
src/cli/initRules.js \
src/validate/jsonschema.json \
test/cli/deleteDammit.utl.js

test/cli/normalizeOptions.spec.js: \
src/cli/normalizeOptions.js

test/cli/validateFileShizzle.spec.js: \
src/cli/validateFileShizzle.js

test/extract/dependencyEndsUpAtFrom.spec.js: \
src/extract/dependencyEndsUpAtFrom.js
Expand Down Expand Up @@ -432,6 +450,12 @@ test/main/main.spec.js: \
src/extract/jsonschema.json \
src/main/index.js

test/main/normalizeOptions.spec.js: \
src/main/normalizeOptions.js

test/main/validateOptions.spec.js: \
src/main/validateOptions.js

test/report/dotReporter.spec.js: \
src/report/dotReporter.js

Expand Down
18 changes: 9 additions & 9 deletions bin/dependency-cruise
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"use strict";

const program = require("commander");
const processCLI = require("../src/cli/processCLI");
const processCLI = require("../src/cli");
const $package = require("../package.json");
const semver = require("semver");

Expand All @@ -18,22 +18,22 @@ if (!semver.satisfies(process.versions.node, $package.engines.node)) {
program
.version($package.version)
.option("-i, --info", `shows what languages and extensions
dependency-cruiser supports`)
dependency-cruiser supports`)
.option("-v, --validate [file]", `validate with rules in [file]
(default: .dependency-cruiser.json)`)
(default: .dependency-cruiser.json)`)
.option("-f, --output-to <file>", `file to write output to; - for stdout
(default: -)`)
(default: -)`)
.option("-X, --do-not-follow <regex>", `a regular expression for modules to include,
but not follow further`)
but not follow further`)
.option("-x, --exclude <regex>", "a regular expression for excluding modules")
.option("-d, --max-depth <n>", `the maximum depth to cruise; 0 <= n <= 99
(default: 0, which means 'infinite depth')`)
.option("-M, --system <items>", `list of module systems (default: amd,cjs,es6)`)
(default: 0, which means 'infinite depth')`)
.option("-M, --module-systems <items>", `list of module systems (default: amd,cjs,es6)`)
.option("-T, --output-type <type>", `output type - html|dot|err|json
(default: err)`)
(default: err)`)
.option("-P, --prefix <prefix>", "prefix to use for links in the svg reporter")
.option("--init-rules", `write a .dependency-cruiser.json with basic
validations to the current folder.`)
validations to the current folder.`)
.arguments("<files-or-directories>")
.parse(process.argv);

Expand Down
8 changes: 4 additions & 4 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ const depcruise = require('dependency-cruiser');
let dependenciesInAGraphVizDotScript = depcruise(
["src"]
{
exclude : "(node_modules)",
system : ["cjs"],
outputType : "dot"
exclude : "(node_modules)",
moduleSystems : ["cjs"],
outputType : "dot"
}
);
```
Expand All @@ -56,7 +56,7 @@ These are all the options:
should not cruise
maxDepth : the maximum depth to cruise; 0 <= n <= 99
(default: 0, which means 'infinite depth')
system : an array of module systems to use for following dependencies;
moduleSystems : an array of module systems to use for following dependencies;
defaults to ["es6", "cjs", "amd"]
outputType : one of "json", "html", "dot", "csv" or "err". When left
out the function will return a javascript object as dependencies
Expand Down
62 changes: 19 additions & 43 deletions doc/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,27 @@ Usage: dependency-cruise [options] <files-or-directories>
Options:
-V, --version output the version number
-i, --info shows what languages and extensions
dependency-cruiser supports
-v, --validate [file] validate with rules in [file]
(default: .dependency-cruiser.json)
-f, --output-to <file> file to write output to; - for stdout
(default: -)
-d, --do-not-follow <regex> a regular expression for modules to include,
but not follow further
-x, --exclude <regex> a regular expression for excluding modules
-M, --system <items> list of module systems (default: amd,cjs,es6)
-T, --output-type <type> output type - html|dot|err|json
(default: err)
-P, --prefix <prefix> prefix to use for links in the svg reporter
--init-rules write a .dependency-cruiser.json with basic
validations to the current folder.
-h, --help output usage information
-V, --version output the version number
-i, --info shows what languages and extensions
dependency-cruiser supports
-v, --validate [file] validate with rules in [file]
(default: .dependency-cruiser.json)
-f, --output-to <file> file to write output to; - for stdout
(default: -)
-X, --do-not-follow <regex> a regular expression for modules to include,
but not follow further
-x, --exclude <regex> a regular expression for excluding modules
-d, --max-depth <n> the maximum depth to cruise; 0 <= n <= 99
(default: 0, which means 'infinite depth')
-M, --module-systems <items> list of module systems (default: amd,cjs,es6)
-T, --output-type <type> output type - html|dot|err|json
(default: err)
-P, --prefix <prefix> prefix to use for links in the svg reporter
--init-rules write a .dependency-cruiser.json with basic
validations to the current folder.
-h, --help output usage information
```

Usage: dependency-cruise [options] <files-or-directories>

Options:

-h, --help output usage information
-V, --version output the version number
-i, --info shows what languages and extensions
dependency-cruiser supports
-v, --validate [file] validate with rules in [file]
(default: .dependency-cruiser.json)
-f, --output-to <file> file to write output to; - for stdout
(default: -)
-X, --do-not-follow <regex> a regular expression for modules to include,
but not follow further
-x, --exclude <regex> a regular expression for excluding modules
-d, --max-depth <n> the maximum depth to cruise; 0 <= n <= 99
(default: 0, which means 'infinite depth')
-M, --system <items> list of module systems (default: amd,cjs,es6)
-T, --output-type <type> output type - html|dot|err|json
(default:err)
-P --prefix <prefix> prefix to prepend links with (e.g. in the
svg output type)
--init-rules create a .dependency-cruiser.json with basic
validations in the current folder.


## Output formats

### err
Expand Down
6 changes: 6 additions & 0 deletions src/cli/defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"PIPE_BUFFER_SIZE" : 512,
"RULES_FILE_NAME": ".dependency-cruiser.json",
"OUTPUT_TYPE": "err",
"OUTPUT_TO": "-"
}
24 changes: 9 additions & 15 deletions src/cli/processCLI.js → src/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"use strict";

const fs = require("fs");
const validateParameters = require("./validateParameters");
const normalizeOptions = require("./normalizeOptions");
const initRules = require("./initRules");
const main = require("../main");
const fs = require('fs');
const validateFileShizzle = require('./validateFileShizzle');
const normalizeOptions = require('./normalizeOptions');
const initRules = require('./initRules');
const main = require('../main');
const formatMetaInfo = require('./formatMetaInfo');


/* OS pipe buffer size in bytes - which is what ulimit -a tells me on OSX */
const PIPE_BUFFER_SIZE = 512;
const defaults = require('./defaults.json');

function writeToFile(pOutputTo, pDependencyString) {
try {
Expand Down Expand Up @@ -48,7 +45,8 @@ function writeToStdOut(pString, pBufferSize) {

function write(pOutputTo, pContent) {
if ("-" === pOutputTo) {
writeToStdOut(pContent, PIPE_BUFFER_SIZE);
// OS pipe buffer size in bytes - which is what ulimit -a tells me on OSX
writeToStdOut(pContent, defaults.PIPE_BUFFER_SIZE);
} else {
writeToFile(pOutputTo, pContent);
}
Expand All @@ -62,13 +60,9 @@ module.exports = (pFileDirArray, pOptions) => {
initRules();
process.stdout.write(`\n Successfully created '.dependency-cruiser.json'\n\n`);
} else {
validateParameters(pFileDirArray, pOptions);
validateFileShizzle(pFileDirArray, pOptions ? pOptions.validate : false);
pOptions = normalizeOptions(pOptions);

if (Boolean(pOptions.rulesFile)){
pOptions.ruleSet = fs.readFileSync(pOptions.rulesFile, 'utf8');
}

const lDependencyList = main.cruise(pFileDirArray, pOptions);

write(pOptions.outputTo, lDependencyList.dependencies);
Expand Down
14 changes: 7 additions & 7 deletions src/cli/initRules.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use strict";

const fs = require('fs');
const starterRules = require('./rules.starter.json');
const DEPENDENCY_CRUISER_CONFIG = ".dependency-cruiser.json";
const fs = require('fs');
const starterRules = require('./rules.starter.json');
const defaults = require('./defaults.json');

/*
We could have used utl.fileExists - but that one is cached.
Expand All @@ -29,19 +29,19 @@ function fileExists(pFile) {
*
*/
module.exports = () => {
if (fileExists(DEPENDENCY_CRUISER_CONFIG)) {
throw Error(`A '${DEPENDENCY_CRUISER_CONFIG}' already exists here - leaving it be.\n`);
if (fileExists(defaults.RULES_FILE_NAME)) {
throw Error(`A '${defaults.RULES_FILE_NAME}' already exists here - leaving it be.\n`);
} else {
try {
fs.writeFileSync(
DEPENDENCY_CRUISER_CONFIG,
defaults.RULES_FILE_NAME,
JSON.stringify(starterRules, null, " "),
{encoding: "utf8", flag: "w"}
);
} catch (e) {

/* istanbul ignore next */
throw Error(`ERROR: Writing to '${DEPENDENCY_CRUISER_CONFIG}' didn't work. ${e}\n`);
throw Error(`ERROR: Writing to '${defaults.RULES_FILE_NAME}' didn't work. ${e}\n`);
}
}
};
Loading

0 comments on commit c99a463

Please sign in to comment.