Skip to content

Commit

Permalink
refactor(init-config): removes superfluous function; touches up the t…
Browse files Browse the repository at this point in the history
…yping a bit
  • Loading branch information
sverweij committed Dec 23, 2022
1 parent c482c0d commit 82e9f0f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/cli/init-config/build-config.js
Expand Up @@ -9,9 +9,9 @@ require("./config.js.template");
* Creates a .dependency-cruiser config with a set of basic validations
* to the current directory.
*
* @returns {void} Nothing
* @param {any} pNormalizedInitOptions Options that influence the shape of
* @param {import("../../../types/init-config").IInitConfig} pNormalizedInitOptions Options that influence the shape of
* the configuration
* @returns {string} the configuration as a string
*/

module.exports = function buildConfig(pNormalizedInitOptions) {
Expand Down
9 changes: 6 additions & 3 deletions src/cli/init-config/environment-helpers.js
Expand Up @@ -24,9 +24,12 @@ function readManifest(pManifestFileName = "./package.json") {
return JSON.parse(readFileSync(pManifestFileName, "utf8"));
}

/*
We could have used utl.fileExists - but that one is cached.
Not typically what we want for this util.
/**
* We could have used utl.fileExists - but that one is cached.
* Not typically what we want for this util.
*
* @param {import("fs").PathLike} pFile
* @returns {boolean}
*/
function fileExists(pFile) {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/init-config/get-user-input.js
Expand Up @@ -15,7 +15,7 @@ const {
hasWebpackConfigCandidates,
getWebpackConfigCandidates,
} = require("./environment-helpers");
const { validateLocation } = require("./inquirer-validators");
const { validateLocation } = require("./validators");

function toPromptChoice(pString) {
return {
Expand All @@ -24,7 +24,7 @@ function toPromptChoice(pString) {
};
}

/** @type {import('@types/prompts').PromptObject[]} */
/** @type {import('prompts').PromptObject[]} */
const QUESTIONS = [
{
name: "isMonoRepo",
Expand Down
21 changes: 14 additions & 7 deletions src/cli/init-config/index.js
@@ -1,3 +1,4 @@
// @ts-check
const $defaults = require("../defaults");
const normalizeInitOptions = require("./normalize-init-options");
const buildConfig = require("./build-config");
Expand Down Expand Up @@ -29,8 +30,8 @@ const PACKAGE_MANIFEST = `./${$defaults.PACKAGE_MANIFEST}`;
* @param {import("../../../types/init-config").OneShotConfigIDType} pOneShotConfigId
* @return {import("../../../types/init-config").IPartialInitConfig} an initialization configuration
*/
function getOneshotConfig(pOneShotConfigId) {
/** @type {"../../../types/init-config").IPartialInitConfig} */
function getOneShotConfig(pOneShotConfigId) {
/** @type {import("../../../types/init-config").IPartialInitConfig} */
const lBaseConfig = {
isMonoRepo: isLikelyMonoRepo(),
combinedDependencies: false,
Expand All @@ -44,7 +45,8 @@ function getOneshotConfig(pOneShotConfigId) {
useBabelConfig: hasBabelConfigCandidates(),
babelConfig: getBabelConfigCandidates().shift(),
};
const lOneshotConfigs = {
/** @type {Record<import("../../../types/init-config").OneShotConfigIDType, import("../../../types/init-config").IPartialInitConfig>} */
const lOneShotConfigs = {
preset: {
configType: "preset",
preset: "dependency-cruiser/configs/recommended-strict",
Expand All @@ -58,20 +60,23 @@ function getOneshotConfig(pOneShotConfigId) {
};

// eslint-disable-next-line security/detect-object-injection
return lOneshotConfigs[pOneShotConfigId] || lBaseConfig;
return lOneShotConfigs[pOneShotConfigId] || lBaseConfig;
}

/**
*
* @param {import("../../../types/init-config").IInitConfig} pNormalizedInitConfig
*/
function manifestIsUpdateable(pNormalizedInitConfig) {
function manifestIsUpdatable(pNormalizedInitConfig) {
return (
pNormalizedInitConfig.updateManifest &&
pNormalizedInitConfig.sourceLocation.length > 0
);
}

/**
* @param {boolean|import("../../../types/init-config").OneShotConfigIDType} pInit
*/
module.exports = function initConfig(pInit) {
/* c8 ignore start */
if (pInit === true) {
Expand All @@ -83,13 +88,15 @@ module.exports = function initConfig(pInit) {
process.stderr.write(`\n ERROR: ${pError.message}\n`);
});
/* c8 ignore stop */
} else if (pInit === false) {
// do nothing; deliberately left empty
} else {
const lNormalizedInitConfig = normalizeInitOptions(getOneshotConfig(pInit));
const lNormalizedInitConfig = normalizeInitOptions(getOneShotConfig(pInit));
if (!fileExists(getDefaultConfigFileName())) {
writeConfig(buildConfig(lNormalizedInitConfig));
}

if (manifestIsUpdateable(lNormalizedInitConfig)) {
if (manifestIsUpdatable(lNormalizedInitConfig)) {
writeRunScriptsToManifest(lNormalizedInitConfig);
}
}
Expand Down
@@ -1,12 +1,5 @@
const fs = require("fs");
const { toSourceLocationArray, fileExists } = require("./environment-helpers");

function validateFileExistence(pInput) {
return (
fileExists(pInput) ||
`hmm, '${pInput}' doesn't seem to exist - could you try again?`
);
}
const { toSourceLocationArray } = require("./environment-helpers");

function validateLocation(pLocations) {
for (const lLocation of toSourceLocationArray(pLocations)) {
Expand All @@ -23,6 +16,5 @@ function validateLocation(pLocations) {
}

module.exports = {
validateFileExistence,
validateLocation,
};
2 changes: 1 addition & 1 deletion src/cli/init-config/write-config.js
Expand Up @@ -11,7 +11,7 @@ const {
*
* @returns {void} Nothing
* @param {string} pConfig - dependency-cruiser configuration
* @param {string} pFileName - name of the file to write to
* @param {import("fs").PathOrFileDescriptor} pFileName - name of the file to write to
* @throws {Error} An error object with the root cause of the problem
* as a description:
* - file already exists
Expand Down
@@ -1,37 +1,5 @@
/* eslint-disable no-unused-expressions */
import { expect } from "chai";
import validators from "../../../src/cli/init-config/inquirer-validators.js";

describe("[U] cli/init-config/inquirer-validators - validateFileExistence", () => {
const WORKING_DIR = process.cwd();
const lFixturesDirectory =
"test/cli/init-config/__fixtures__/validate-file-existence";

beforeEach("set up", () => {
process.chdir(lFixturesDirectory);
});

afterEach("tear down", () => {
process.chdir(WORKING_DIR);
});

it("returns an error message when presented with an empty string", () => {
expect(validators.validateFileExistence("")).to.equal(
`hmm, '' doesn't seem to exist - could you try again?`
);
});

it("returns an error message when presented with a name of a non existing file", () => {
expect(validators.validateFileExistence("nope-does-not-exist")).to.equal(
`hmm, 'nope-does-not-exist' doesn't seem to exist - could you try again?`
);
});

it("returns true when presented with the name of a file that does exist", () => {
expect(validators.validateFileExistence("i-have-bytes-therefore-i-exist"))
.to.be.true;
});
});
import validators from "../../../src/cli/init-config/validators.js";

describe("[U] cli/init-config/inquirer-validators - validateLocation", () => {
const WORKING_DIR = process.cwd();
Expand Down
19 changes: 8 additions & 11 deletions types/init-config.d.ts
Expand Up @@ -2,8 +2,6 @@ export type OneShotConfigIDType = "preset" | "yes" | "experimental-scripts";

export type ConfigTypeType = "preset" | "self-contained";

export type ExternalModuleResolutionStrategyType = "yarn-pnp" | "node_modules";

export interface IInitConfig {
/**
* Whether or not the current folder houses a mono repo
Expand All @@ -14,6 +12,14 @@ export interface IInitConfig {
* package.jsons of parent folders
*/
combinedDependencies: boolean;
/**
* Whether or not to use a TypeScript config
*/
useJsConfig: boolean;
/**
* The file name of the TypeScript config to use
*/
jsConfig?: string;
/**
* Whether or not to use a TypeScript config
*/
Expand All @@ -27,15 +33,6 @@ export interface IInitConfig {
* compilation to javascript
*/
tsPreCompilationDeps: boolean;
/**
* Whether or not to use Yarn plug'n play for module resolution (only useful
* when the package.json + repo are prepared for this)
*/
useYarnPnP: boolean;
/**
*
*/
externalModuleResolutionStrategy: ExternalModuleResolutionStrategyType;
/**
* Whether or not to use a Webpack config
*/
Expand Down

0 comments on commit 82e9f0f

Please sign in to comment.