dependency-cruiser
Version:
Validate and visualize dependencies. With your rules. JavaScript, TypeScript, CoffeeScript. ES6, CommonJS, AMD.
43 lines (41 loc) • 1.27 kB
JavaScript
import { writeFileSync } from "node:fs";
import { styleText } from "node:util";
import {
fileExists,
getDefaultConfigFileName,
} from "./environment-helpers.mjs";
/**
* Write a .dependency-cruiser config to the current directory
*
* @returns {void} Nothing
* @param {string} pConfig - dependency-cruiser configuration
* @param {import("fs").PathOrFileDescriptor} pFileName - name of the file to write to
* @param {NodeJS.WritableStream} pOutStream - the stream to write user feedback to
* @throws {Error} An error object with the root cause of the problem
* as a description:
* - file already exists
* - writing to the file doesn't work
*
*/
export default function writeConfig(
pConfig,
pFileName = getDefaultConfigFileName(),
pOutStream = process.stdout,
) {
if (fileExists(pFileName)) {
throw new Error(`A '${pFileName}' already exists here - leaving it be.\n`);
} else {
try {
writeFileSync(pFileName, pConfig);
pOutStream.write(
`\n ${styleText("green", "✔")} Successfully created '${pFileName}'\n\n`,
);
/* c8 ignore start */
} catch (pError) {
throw new Error(
`ERROR: Writing to '${pFileName}' didn't work. ${pError}\n`,
);
}
/* c8 ignore stop */
}
}