Skip to content

Commit

Permalink
fix(init): also shows an error if config file already exists with one…
Browse files Browse the repository at this point in the history
… shot configs
  • Loading branch information
sverweij committed Dec 31, 2023
1 parent fbba9ea commit 49fb96f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/cli/init-config/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,26 @@ export default function initConfig(pInit, pConfigFileName, pStreams) {
const lNormalizedInitConfig = normalizeInitOptions(getOneShotConfig(pInit));
const lConfigFileName = pConfigFileName || getDefaultConfigFileName();

if (!fileExists(lConfigFileName)) {
if (manifestIsUpdatable(lNormalizedInitConfig)) {
// if we're going to update the manifest, no need to complain about
// a .dependency-cruiser that might already exist, because writing
// run scripts to the manifest could still work AOK.
if (!fileExists(lConfigFileName)) {
writeConfig(
buildConfig(lNormalizedInitConfig),
lConfigFileName,
lStreams.stdout,
);
}
writeRunScriptsToManifest(lNormalizedInitConfig, {
outStream: lStreams.stdout,
});
} else {
writeConfig(
buildConfig(lNormalizedInitConfig),
lConfigFileName,
lStreams.stdout,
);
}

if (manifestIsUpdatable(lNormalizedInitConfig)) {
writeRunScriptsToManifest(lNormalizedInitConfig, {
outStream: lStreams.stdout,
});
}
}
}
23 changes: 23 additions & 0 deletions test/cli/init-config/index.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { writeFileSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { deepEqual, equal } from "node:assert/strict";
import { throws } from "node:assert";
import Ajv from "ajv";
import deleteDammit from "../delete-dammit.utl.cjs";
import {
Expand Down Expand Up @@ -72,6 +73,28 @@ describe("[I] cli/init-config/index", () => {
}
});

it("init yes borks with an error if a config file already exists", () => {
const lWhatEverOutStream = new WritableTestStream(/.*/);
process.chdir("test/cli/__fixtures__/init-config/no-config-files-exist");
const lConfig = ".dependency-cruiser-blabla.js";

try {
initConfig("yes", lConfig, {
stdout: lWhatEverOutStream,
stderr: lWhatEverOutStream,
});

throws(() => {
initConfig("yes", lConfig, {
stdout: lWhatEverOutStream,
stderr: lWhatEverOutStream,
});
}, /A '[.]dependency-cruiser-blabla[.]js' already exists here - leaving it be[.]/);
} finally {
deleteDammit(lConfig);
}
});

it("init yes in a ts project creates a self-contained js rules file with typescript things flipped to yes", async () => {
const lOutStream = new WritableTestStream(
/Successfully created '[.]dependency-cruiser[.]js'/,
Expand Down

0 comments on commit 49fb96f

Please sign in to comment.