Skip to content

Commit

Permalink
feature(init): write to .dependency-cruiser.cjs when package type equ…
Browse files Browse the repository at this point in the history
…als module (sverweij#479)
  • Loading branch information
sverweij authored Aug 4, 2021
1 parent 9f6b697 commit 7be9128
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 4 deletions.
4 changes: 4 additions & 0 deletions doc/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ and ✖'s in the output should be a thing of the past.
**A**: Rename your .dependency-cruiser.js to .dependency-cruiser.cjs
> As of version 10.1.0 the `--init` generator automatically does this for you.
## Features
### Q: How do I enable TypeScript, CoffeeScript or LiveScript in dependency-cruiser?
Expand Down Expand Up @@ -410,6 +412,8 @@ module.exports = {
};
```
> Newer versions of the `--init` generator automatically do this for you.
## Expanding dependency-cruiser
### Q: How do I add a new output format?
Expand Down
19 changes: 19 additions & 0 deletions src/cli/init-config/environment-helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fs = require("fs");
const path = require("path");
const _has = require("lodash/has");
const _get = require("lodash/get");
const { DEFAULT_CONFIG_FILE_NAME } = require("../defaults");

const LIKELY_SOURCE_FOLDERS = ["src", "lib", "app", "bin", "sources"];
const LIKELY_TEST_FOLDERS = ["test", "spec", "tests", "specs", "bdd"];
Expand Down Expand Up @@ -45,6 +47,18 @@ function babelIsConfiguredInManifest() {
return lReturnValue;
}

function isTypeModule() {
let lReturnValue = false;

try {
lReturnValue = _get(readManifest(), "type", "commonjs") === "module";
} catch (pError) {
// silently ignore - we'll return false anyway then
}

return lReturnValue;
}

function getFolderNames(pFolderName) {
return fs
.readdirSync(pFolderName, "utf8")
Expand Down Expand Up @@ -117,12 +131,16 @@ const getTestFolderCandidates = getFolderCandidates(LIKELY_TEST_FOLDERS);
const getMonoRepoPackagesCandidates = getFolderCandidates(
LIKELY_PACKAGES_FOLDERS
);
function getDefaultConfigFileName() {
return isTypeModule() ? ".dependency-cruiser.cjs" : DEFAULT_CONFIG_FILE_NAME;
}

module.exports = {
readManifest,
fileExists,
toSourceLocationArray,
isLikelyMonoRepo,
isTypeModule,
hasTestsWithinSource,
getFolderCandidates,
getBabelConfigCandidates,
Expand All @@ -134,4 +152,5 @@ module.exports = {
getSourceFolderCandidates,
getTestFolderCandidates,
getMonoRepoPackagesCandidates,
getDefaultConfigFileName,
};
3 changes: 2 additions & 1 deletion src/cli/init-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
getWebpackConfigCandidates,
hasTSConfigCandidates,
getTSConfigCandidates,
getDefaultConfigFileName,
} = require("./environment-helpers");
const {
writeRunScriptsToManifest,
Expand Down Expand Up @@ -80,7 +81,7 @@ module.exports = (pInit) => {
/* c8 ignore stop */
} else {
const lNormalizedInitConfig = normalizeInitOptions(getOneshotConfig(pInit));
if (!fileExists($defaults.DEFAULT_CONFIG_FILE_NAME)) {
if (!fileExists(getDefaultConfigFileName())) {
writeConfig(buildConfig(lNormalizedInitConfig));
}

Expand Down
8 changes: 5 additions & 3 deletions src/cli/init-config/write-config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const fs = require("fs");
const figures = require("figures");
const chalk = require("chalk");
const { DEFAULT_CONFIG_FILE_NAME } = require("../defaults");
const { fileExists } = require("./environment-helpers");
const {
fileExists,
getDefaultConfigFileName,
} = require("./environment-helpers");

/**
* Write a .dependency-cruiser config to the current directory
Expand All @@ -18,7 +20,7 @@ const { fileExists } = require("./environment-helpers");
*/
module.exports = function writeConfig(
pConfig,
pFileName = DEFAULT_CONFIG_FILE_NAME
pFileName = getDefaultConfigFileName()
) {
if (fileExists(pFileName)) {
throw new Error(`A '${pFileName}' already exists here - leaving it be.\n`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,57 @@ describe("cli/init-config/environment-helpers - hasWebpackConfigCandidates", ()
expect(helpers.hasWebpackConfigCandidates()).to.equal(false);
});
});

describe("cli/init-config/environment-helpers - isTypeModule", () => {
const WORKINGDIR = process.cwd();
afterEach("tear down", () => {
process.chdir(WORKINGDIR);
});
it("Returns false when there is no manifest", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module-empty");
expect(helpers.isTypeModule()).to.equal(false);
});
it("Returns false when there's no type in the manifest", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module-not-present");
expect(helpers.isTypeModule()).to.equal(false);
});
it("Returns false when the type in the manifest equals commonjs", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module-commonjs");
expect(helpers.isTypeModule()).to.equal(false);
});
it("Returns true when the type in the manifest equals module", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module");
expect(helpers.isTypeModule()).to.equal(true);
});
});

describe("cli/init-config/environment-helpers - getDefaultConfigFileName", () => {
const WORKINGDIR = process.cwd();
afterEach("tear down", () => {
process.chdir(WORKINGDIR);
});
it("Returns false when there is no manifest", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module-empty");
expect(helpers.getDefaultConfigFileName()).to.equal(
".dependency-cruiser.js"
);
});
it("Returns false when there's no type in the manifest", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module-not-present");
expect(helpers.getDefaultConfigFileName()).to.equal(
".dependency-cruiser.js"
);
});
it("Returns false when the type in the manifest equals commonjs", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module-commonjs");
expect(helpers.getDefaultConfigFileName()).to.equal(
".dependency-cruiser.js"
);
});
it("Returns true when the type in the manifest equals module", () => {
process.chdir("test/cli/init-config/fixtures/is-type-module");
expect(helpers.getDefaultConfigFileName()).to.equal(
".dependency-cruiser.cjs"
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions test/cli/init-config/fixtures/is-type-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}

0 comments on commit 7be9128

Please sign in to comment.