Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replaces cruise function with signature from futureCruise BREAKING #773

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,35 @@ try {

### Utility functions

> Exposition of `extract-xxx` functions is _experimental_. It also will only
> work on node ^12.19 || >=14.7 as those are versions that support the `exports`
> entry points feature in package.json.

```typescript
import { cruise, ICruiseOptions, IReporterOutput } from ".";
import extractDepcruiseConfig from "./src/config-utl/extract-depcruise-config";
import extractTSConfig from "./src/config-utl/extract-ts-config";
import extractWebpackResolveConfig from "./src/config-utl/extract-webpack-resolve-config";
import { cruise, ICruiseOptions, IReporterOutput } from "dependency-cruiser";
import extractDepcruiseConfig from "dependency-cruiser/config-utl/extract-depcruise-config";
import extractTSConfig from "dependency-cruiser/config-utl/extract-ts-config";
import extractWebpackResolveConfig from "dependency-cruiser/config-utl/extract-webpack-resolve-config";
import extractBabelConfig from "dependency-cruiser/config-utl/extract-babel-config";

try {
const ARRAY_OF_FILES_AND_DIRS_TO_CRUISE = ["src"];

const depcruiseConfig: ICruiseOptions = extractDepcruiseConfig(
const depcruiseConfig: ICruiseOptions = await extractDepcruiseConfig(
"./.dependency-cruiser.js"
);
const webpackResolveConfig = extractWebpackResolveConfig("./webpack.conf.js");
const tsConfig = extractTSConfig("./tsconfig.json");
// const babelConfig = await extractBabelConfig("./babel.conf.json");

const cruiseResult: IReporterOutput = cruise(
ARRAY_OF_FILES_AND_DIRS_TO_CRUISE,
depcruiseConfig,
webpackResolveConfig,
tsConfig
// change since v13: in stead of passing the tsConfig directly, like so:
// tsconfig
// you now pass it into an object that also supports other types of
// compiler options, like those for babel:
{
tsConfig: tsConfig,
// babelConfig: babelConfig,
}
);

console.dir(cruiseResult.output, { depth: 10 });
Expand Down
2 changes: 1 addition & 1 deletion src/cli/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async function runCruise(pFileDirectoryArray, pCruiseOptions) {
setUpListener(lCruiseOptions);

bus.emit("start");
const lReportingResult = main.futureCruise(
const lReportingResult = main.cruise(
pFileDirectoryArray,
lCruiseOptions,
extractResolveOptions(lCruiseOptions),
Expand Down
16 changes: 3 additions & 13 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ function c(pComplete, pTotal = TOTAL_STEPS) {
return { complete: pComplete / pTotal };
}

/** @type {import("../..").futureCruise} */
/** @type {import("../..").cruise} */
// eslint-disable-next-line max-lines-per-function, max-statements
function futureCruise(
function cruise(
pFileAndDirectoryArray,
pCruiseOptions,
pResolveOptions,
Expand Down Expand Up @@ -93,7 +93,7 @@ function futureCruise(
const lNormalizedResolveOptions = normalizeResolveOptions(
pResolveOptions,
lCruiseOptions,
pTranspileOptions.tsConfig
pTranspileOptions?.tsConfig
);

bus.emit("progress", "reading files", c(5));
Expand All @@ -120,18 +120,8 @@ function futureCruise(
return reportWrap(lCruiseResult, lCruiseOptions);
}

// see [api.md](../../doc/api.md) and/ or the
// [type definition](../../types/dependency-cruiser.d.ts) for details
/** @type {import("../..").cruise} */
function cruise(pFileAndDirectoryArray, pOptions, pResolveOptions, pTSConfig) {
return futureCruise(pFileAndDirectoryArray, pOptions, pResolveOptions, {
tsConfig: pTSConfig,
});
}

module.exports = {
cruise,
futureCruise,
format,
allExtensions: meta.allExtensions,
getAvailableTranspilers: meta.getAvailableTranspilers,
Expand Down
12 changes: 6 additions & 6 deletions test/main/main.cruise.cache.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { rmSync } from "fs";
import { expect, use } from "chai";
import chaiJSONSchema from "chai-json-schema";
import cruiseResultSchema from "../../src/schema/cruise-result.schema.js";
import { futureCruise } from "../../src/main/index.js";
import { cruise } from "../../src/main/index.js";
import Cache from "../../src/cache/cache.js";

use(chaiJSONSchema);
Expand All @@ -19,7 +19,7 @@ describe("[E] main.cruise - cache", () => {
});

it("cruising fills the cache", () => {
const lResult = futureCruise(
const lResult = cruise(
["test/main/__mocks__/cache"],
{
cache: CACHE_FOLDER,
Expand All @@ -35,7 +35,7 @@ describe("[E] main.cruise - cache", () => {
});

it("cruising twice yields the same result (minus 'revisionData')", () => {
const lResult = futureCruise(
const lResult = cruise(
["test/main/__mocks__/cache"],
{
cache: CACHE_FOLDER,
Expand All @@ -48,7 +48,7 @@ describe("[E] main.cruise - cache", () => {

expect(lResult.output).to.deep.equal(lCache);

const lResultTwo = futureCruise(
const lResultTwo = cruise(
["test/main/__mocks__/cache"],
{
cache: CACHE_FOLDER,
Expand All @@ -62,7 +62,7 @@ describe("[E] main.cruise - cache", () => {
});

it("cruising twice with non-compatible arguments yields different results", () => {
const lResult = futureCruise(
const lResult = cruise(
["test/main/__mocks__/cache"],
{
cache: CACHE_FOLDER,
Expand All @@ -75,7 +75,7 @@ describe("[E] main.cruise - cache", () => {

expect(lResult.output).to.deep.equal(lOldCache);

const lResultTwo = futureCruise(
const lResultTwo = cruise(
["test/main/__mocks__/cache test/main/__mocks__/cache-too "],
{
cache: CACHE_FOLDER,
Expand Down
8 changes: 4 additions & 4 deletions test/main/main.cruise.dynamic-imports.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, use } from "chai";
import chaiJSONSchema from "chai-json-schema";
import cruiseResultSchema from "../../src/schema/cruise-result.schema.js";
import main from "../../src/main/index.js";
import { cruise } from "../../src/main/index.js";
import { createRequireJSON } from "../backwards.utl.mjs";
import normBaseDirectory from "./norm-base-directory.utl.mjs";

Expand Down Expand Up @@ -34,7 +34,7 @@ describe("[E] main.cruise - dynamic imports", () => {

it("detects dynamic dependencies in es", () => {
process.chdir("test/main/__mocks__/dynamic-imports/es");
const lResult = main.cruise(
const lResult = cruise(
["src"],
{
ruleSet: {
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("[E] main.cruise - dynamic imports", () => {

it("detects dynamic dependencies in typescript", () => {
process.chdir("test/main/__mocks__/dynamic-imports/typescript");
const lResult = main.cruise(
const lResult = cruise(
["src"],
{
ruleSet: {
Expand Down Expand Up @@ -104,7 +104,7 @@ describe("[E] main.cruise - dynamic imports", () => {

it("detects dynamic dependencies in typescript when using tsPreCompilationDeps", () => {
process.chdir("test/main/__mocks__/dynamic-imports/typescript");
const lResult = main.cruise(
const lResult = cruise(
["src"],
{
ruleSet: {
Expand Down
14 changes: 7 additions & 7 deletions test/main/main.cruise.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { expect, use } from "chai";
import chaiJSONSchema from "chai-json-schema";
import pathToPosix from "../../src/utl/path-to-posix.js";
import cruiseResultSchema from "../../src/schema/cruise-result.schema.js";
import main from "../../src/main/index.js";
import { cruise } from "../../src/main/index.js";
import { createRequireJSON } from "../backwards.utl.mjs";
import normBaseDirectory from "./norm-base-directory.utl.mjs";

Expand All @@ -31,14 +31,14 @@ function pathPosixify(pOutput) {

describe("[E] main.cruise - main", () => {
it("Returns an object when no options are passed", () => {
const lResult = main.cruise(["test/main/__mocks__/ts"]);
const lResult = cruise(["test/main/__mocks__/ts"]);

expect(pathPosixify(lResult.output)).to.deep.equal(tsFixture);
expect(lResult.output).to.be.jsonSchema(cruiseResultSchema);
});

it("Returns an object when no options are passed (absolute path)", () => {
const lResult = main.cruise(
const lResult = cruise(
[path.join(__dirname, "__mocks__", "ts")],
{},
{ bustTheCache: true }
Expand All @@ -49,7 +49,7 @@ describe("[E] main.cruise - main", () => {
});

it("processes tsx correctly", () => {
const lResult = main.cruise(
const lResult = cruise(
["test/main/__mocks__/tsx"],
{},
{ bustTheCache: true }
Expand All @@ -60,7 +60,7 @@ describe("[E] main.cruise - main", () => {
});

it("processes jsx correctly", () => {
const lResult = main.cruise(
const lResult = cruise(
["test/main/__mocks__/jsx"],
{},
{ bustTheCache: true }
Expand All @@ -70,7 +70,7 @@ describe("[E] main.cruise - main", () => {
expect(lResult.output).to.be.jsonSchema(cruiseResultSchema);
});
it("process rulesets in the form a an object instead of json", () => {
const lResult = main.cruise(
const lResult = cruise(
["test/main/__mocks__/jsx"],
{
ruleSet: {},
Expand All @@ -82,7 +82,7 @@ describe("[E] main.cruise - main", () => {
expect(lResult.output).to.be.jsonSchema(cruiseResultSchema);
});
it("Collapses to a pattern when a collapse pattern is passed", () => {
const lResult = main.cruise(
const lResult = cruise(
["test/main/__mocks__/collapse-after-cruise"],
{
ruleSet: {},
Expand Down
32 changes: 20 additions & 12 deletions test/main/main.cruise.ts-pre-compilation-deps.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ describe("[E] main.cruise - tsPreCompilationDeps", () => {
},
{ bustTheCache: true },
{
options: {
baseUrl: ".",
module: "commonjs",
tsConfig: {
options: {
baseUrl: ".",
module: "commonjs",
},
},
}
);
Expand All @@ -55,9 +57,11 @@ describe("[E] main.cruise - tsPreCompilationDeps", () => {
},
{ bustTheCache: true },
{
options: {
baseUrl: ".",
module: "es6",
tsConfig: {
options: {
baseUrl: ".",
module: "es6",
},
},
}
);
Expand All @@ -76,9 +80,11 @@ describe("[E] main.cruise - tsPreCompilationDeps", () => {
},
{ bustTheCache: true },
{
options: {
baseUrl: ".",
module: "commonjs",
tsConfig: {
options: {
baseUrl: ".",
module: "commonjs",
},
},
}
);
Expand All @@ -97,9 +103,11 @@ describe("[E] main.cruise - tsPreCompilationDeps", () => {
},
{ bustTheCache: true },
{
options: {
baseUrl: ".",
module: "es6",
tsConfig: {
options: {
baseUrl: ".",
module: "es6",
},
},
}
);
Expand Down
28 changes: 1 addition & 27 deletions types/dependency-cruiser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,6 @@ export interface IReporterOutput {
exitCode: number;
}

/**
* Cruises the specified files and files with supported extensions in
* the specified directories in the pFileDirArray and returns the result
* in an object.
*
* @param pFileAndDirectoryArray An array of (names of) files, directories and/ or glob patterns
* to start the cruise with
* @param pCruiseOptions Options that influence the way the dependencies are cruised - and
* how they are returned.
* @param pResolveOptions Options that influence how dependency references are resolved to disk.
* See https://webpack.js.org/configuration/resolve/ for the details.
* @param pTSConfig An object with with a typescript config object. Note that the
* API will not take any 'extends' keys there into account, so
* before calling make sure to flatten them out if you want them
* used (e.g. with 'dependency-cruiser/config-utl/extract-ts-config')
*/
export function cruise(
pFileAndDirectoryArray: string[],
pCruiseOptions?: ICruiseOptions,
pResolveOptions?: IResolveOptions,
pTSConfig?: any
): IReporterOutput;

export interface ITranspileOptions {
/**
* An object with with a typescript config object. Note that the
Expand All @@ -97,9 +74,6 @@ export interface ITranspileOptions {
}

/**
* /!\ Beta cruise function. Supports slightly more stuff (i.e. babel), but
* the function signature might change without notice. /!\
*
* Cruises the specified files and files with supported extensions in
* the specified directories in the pFileDirArray and returns the result
* in an object.
Expand All @@ -113,7 +87,7 @@ export interface ITranspileOptions {
* @param pTranspileOptions Object to hold options to pass to underlying transpilers
* like TypeScript or Babel
*/
export function futureCruise(
export function cruise(
pFileAndDirectoryArray: string[],
pCruiseOptions?: ICruiseOptions,
pResolveOptions?: IResolveOptions,
Expand Down