Skip to content

Commit

Permalink
refactor(cli): converts hbs calling code in init-config to esm (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij authored Apr 9, 2023
1 parent 3c8c1d6 commit 3c16e7d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-check
const Handlebars = require("handlebars/runtime");
const { folderNameArrayToRE } = require("./utl.cjs");
import Handlebars from "handlebars/runtime.js";
import { folderNameArrayToRE } from "./utl.mjs";

/* eslint import/no-unassigned-import: 0 */
require("./config.js.template");
await import("./config.js.template.js");

/**
* @param {string} pString
Expand All @@ -28,12 +28,12 @@ function extensionsToString(pExtensions) {
* Creates a .dependency-cruiser config with a set of basic validations
* to the current directory.
*
* @param {import("./types").IInitConfig} pNormalizedInitOptions Options that influence the shape of
* @param {import("./types.js").IInitConfig} pNormalizedInitOptions Options that influence the shape of
* the configuration
* @returns {string} the configuration as a string
*/

module.exports = function buildConfig(pNormalizedInitOptions) {
export default function buildConfig(pNormalizedInitOptions) {
return Handlebars.templates["config.js.template.hbs"]({
...pNormalizedInitOptions,

Expand All @@ -45,4 +45,4 @@ module.exports = function buildConfig(pNormalizedInitOptions) {
pNormalizedInitOptions.resolutionExtensions
),
});
};
}
2 changes: 1 addition & 1 deletion src/cli/init-config/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check
import { PACKAGE_MANIFEST as _PACKAGE_MANIFEST } from "../defaults.mjs";
import normalizeInitOptions from "./normalize-init-options.mjs";
import buildConfig from "./build-config.cjs";
import buildConfig from "./build-config.mjs";
import writeConfig from "./write-config.mjs";
import getUserInput from "./get-user-input.mjs";
import {
Expand Down
6 changes: 0 additions & 6 deletions src/cli/init-config/utl.cjs

This file was deleted.

3 changes: 3 additions & 0 deletions src/cli/init-config/utl.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function folderNameArrayToRE(pArrayOfStrings) {
return `^(${pArrayOfStrings.join("|")})`;
}
2 changes: 1 addition & 1 deletion src/cli/init-config/write-run-scripts-to-manifest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import chalk from "chalk";
import wrapAndIndent from "../../utl/wrap-and-indent.mjs";
import { PACKAGE_MANIFEST as _PACKAGE_MANIFEST } from "../defaults.mjs";
import { readManifest } from "./environment-helpers.mjs";
import { folderNameArrayToRE } from "./utl.cjs";
import { folderNameArrayToRE } from "./utl.mjs";

const PACKAGE_MANIFEST = `./${_PACKAGE_MANIFEST}`;

Expand Down
2 changes: 1 addition & 1 deletion test/cli/init-config/create-config.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path/posix";
import { expect, use } from "chai";
import chaiJsonSchema from "chai-json-schema";
import buildConfig from "../../../src/cli/init-config/build-config.cjs";
import buildConfig from "../../../src/cli/init-config/build-config.mjs";
import normalizeInitOptions from "../../../src/cli/init-config/normalize-init-options.mjs";
import configurationSchema from "../../../src/schema/configuration.schema.mjs";
import deleteDammit from "../delete-dammit.utl.cjs";
Expand Down
8 changes: 4 additions & 4 deletions test/cli/init-config/utl.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { expect } from "chai";
import utl from "../../../src/cli/init-config/utl.cjs";
import { folderNameArrayToRE } from "../../../src/cli/init-config/utl.mjs";

describe("[U] cli/init-config/utl - folderNameArrayToRE", () => {
it("transforms an array of folder names into a regex string - empty", () => {
expect(utl.folderNameArrayToRE([])).to.equal("^()");
expect(folderNameArrayToRE([])).to.equal("^()");
});
it("transforms an array of folder names into a regex string - one entry", () => {
expect(utl.folderNameArrayToRE(["src"])).to.equal("^(src)");
expect(folderNameArrayToRE(["src"])).to.equal("^(src)");
});
it("transforms an array of folder names into a regex string - more than one entry", () => {
expect(utl.folderNameArrayToRE(["bin", "src", "lib"])).to.equal(
expect(folderNameArrayToRE(["bin", "src", "lib"])).to.equal(
"^(bin|src|lib)"
);
});
Expand Down

0 comments on commit 3c16e7d

Please sign in to comment.