Skip to content

feat: load config from deno.json #4327

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
49 changes: 25 additions & 24 deletions @commitlint/load/src/load.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
const cwd = await gitBootstrap("fixtures/extends-empty");
const actual = await load(
{ plugins: ["example", "@scope/example"] },
{ cwd },
{ cwd }
);

expect(actual.plugins).toMatchObject({
Expand All @@ -144,7 +144,7 @@
test: expect.any(Function),
},
},
}),
})
);
});

Expand Down Expand Up @@ -180,15 +180,15 @@
"is-positive": expect.any(Function),
},
},
}),
})
);
});

test("uses seed with parserPreset", async () => {
const cwd = await gitBootstrap("fixtures/parser-preset");
const { parserPreset: actual } = await load(
{ parserPreset: "./conventional-changelog-custom" },
{ cwd },
{ cwd }
);

expect(actual).toBeDefined();
Expand Down Expand Up @@ -240,6 +240,7 @@
"commitlint.config.cjs",
"commitlint.config.js",
"commitlint.config.mjs",
"deno.json",
"package.json",
"package.yaml",
".commitlintrc",
Expand All @@ -257,7 +258,7 @@
.map((filename) => ({ filename, isEsm: false })),
...configFiles
.filter((filename) =>
[".mjs", ".js"].some((ext) => filename.endsWith(ext)),
[".mjs", ".js"].some((ext) => filename.endsWith(ext))
)
.map((filename) => ({ filename, isEsm: true })),
];
Expand All @@ -269,19 +270,19 @@
filename: string;
isEsm: boolean;
}): string | NodeJS.ArrayBufferView => {
if (filename === "package.json") {
if (filename === "package.json" || filename === "deno.json") {
const configPath = path.join(
__dirname,
`../fixtures/${template}-config/.commitlintrc.json`,
`../fixtures/${template}-config/.commitlintrc.json`
);
const commitlint = JSON.parse(
readFileSync(configPath, { encoding: "utf-8" }),
readFileSync(configPath, { encoding: "utf-8" })
);
return JSON.stringify({ commitlint });
} else if (filename === "package.yaml") {
const configPath = path.join(
__dirname,
`../fixtures/${template}-config/.commitlintrc.yaml`,
`../fixtures/${template}-config/.commitlintrc.yaml`
);
const yaml = readFileSync(configPath, { encoding: "utf-8" });
return `commitlint:\n${yaml.replace(/^/gm, " ")}`;
Expand All @@ -300,15 +301,15 @@
const esmBootstrap = (cwd: string) => {
const packageJsonPath = path.join(cwd, "package.json");
const packageJSON = JSON.parse(
readFileSync(packageJsonPath, { encoding: "utf-8" }),
readFileSync(packageJsonPath, { encoding: "utf-8" })
);

writeFileSync(
packageJsonPath,
JSON.stringify({
...packageJSON,
type: "module",
}),
})
);
};

Expand All @@ -321,7 +322,7 @@
// Skip ESM tests for the extends suite until resolve-extends supports ESM
.filter(({ isEsm }) => template !== "extends" || !isEsm)
// Skip ESM tests if dynamic await is not supported; Jest will crash with a seg fault error
.filter(({ isEsm }) => isDynamicAwaitSupported() || !isEsm),
.filter(({ isEsm }) => isDynamicAwaitSupported() || !isEsm)
)("$filename, ESM: $isEsm", async ({ filename, isEsm }) => {
const cwd = await gitBootstrap(`fixtures/${templateFolder}`);

Expand All @@ -331,12 +332,12 @@

writeFileSync(
path.join(cwd, filename),
getConfigContents({ filename, isEsm }),
getConfigContents({ filename, isEsm })
);

const actual = await load({}, { cwd });

expect(actual).toMatchObject({

Check failure on line 340 in @commitlint/load/src/load.test.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 18)

AssertionError: expected { extends: [], …(8) } to match object { …(4) } (5 matching properties omitted from actual) - Expected + Received { - "extends": [ - "./first-extended", - ], + "extends": [], "formatter": "@commitlint/format", "plugins": {}, - "rules": { - "one": [ - 1, - "always", - ], - "two": [ - 2, - "never", - ], - "zero": [ - 0, - "never", - ], - }, + "rules": {}, } ❯ @commitlint/load/src/load.test.ts:340:18

Check failure on line 340 in @commitlint/load/src/load.test.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 18)

AssertionError: expected { extends: [], …(8) } to match object { …(4) } (5 matching properties omitted from actual) - Expected + Received { "extends": [], "formatter": "@commitlint/format", "plugins": {}, - "rules": { - "one": [ - 1, - "always", - ], - "two": [ - 2, - "never", - ], - "zero": [ - 0, - "never", - ], - }, + "rules": {}, } ❯ @commitlint/load/src/load.test.ts:340:18

Check failure on line 340 in @commitlint/load/src/load.test.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 20)

AssertionError: expected { extends: [], …(8) } to match object { …(4) } (5 matching properties omitted from actual) - Expected + Received { - "extends": [ - "./first-extended", - ], + "extends": [], "formatter": "@commitlint/format", "plugins": {}, - "rules": { - "one": [ - 1, - "always", - ], - "two": [ - 2, - "never", - ], - "zero": [ - 0, - "never", - ], - }, + "rules": {}, } ❯ @commitlint/load/src/load.test.ts:340:18

Check failure on line 340 in @commitlint/load/src/load.test.ts

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 20)

AssertionError: expected { extends: [], …(8) } to match object { …(4) } (5 matching properties omitted from actual) - Expected + Received { "extends": [], "formatter": "@commitlint/format", "plugins": {}, - "rules": { - "one": [ - 1, - "always", - ], - "two": [ - 2, - "never", - ], - "zero": [ - 0, - "never", - ], - }, + "rules": {}, } ❯ @commitlint/load/src/load.test.ts:340:18

Check failure on line 340 in @commitlint/load/src/load.test.ts

View workflow job for this annotation

GitHub Actions / NodeJS installed from stock Ubuntu-LTS packages (not external sources)

AssertionError: expected { extends: [], …(8) } to match object { …(4) } (5 matching properties omitted from actual) - Expected + Received { - "extends": [ - "./first-extended", - ], + "extends": [], "formatter": "@commitlint/format", "plugins": {}, - "rules": { - "one": [ - 1, - "always", - ], - "two": [ - 2, - "never", - ], - "zero": [ - 0, - "never", - ], - }, + "rules": {}, } ❯ @commitlint/load/src/load.test.ts:340:18

Check failure on line 340 in @commitlint/load/src/load.test.ts

View workflow job for this annotation

GitHub Actions / NodeJS installed from stock Ubuntu-LTS packages (not external sources)

AssertionError: expected { extends: [], …(8) } to match object { …(4) } (5 matching properties omitted from actual) - Expected + Received { "extends": [], "formatter": "@commitlint/format", "plugins": {}, - "rules": { - "one": [ - 1, - "always", - ], - "two": [ - 2, - "never", - ], - "zero": [ - 0, - "never", - ], - }, + "rules": {}, } ❯ @commitlint/load/src/load.test.ts:340:18
formatter: "@commitlint/format",
extends: isExtendsTemplate ? ["./first-extended"] : [],
plugins: {},
Expand Down Expand Up @@ -510,11 +511,11 @@

expect(actual.parserPreset).toBeDefined();
expect(actual.parserPreset!.name).toBe(
"conventional-changelog-conventionalcommits",
"conventional-changelog-conventionalcommits"
);
expect(typeof actual.parserPreset!.parserOpts).toBe("object");
expect((actual.parserPreset!.parserOpts as any).headerPattern).toEqual(
/^(\w*)(?:\((.*)\))?!?: (.*)$/,
/^(\w*)(?:\((.*)\))?!?: (.*)$/
);
});

Expand All @@ -526,16 +527,16 @@
expect(actual.parserPreset!.name).toBe("conventional-changelog-angular");
expect(typeof actual.parserPreset!.parserOpts).toBe("object");
expect((actual.parserPreset!.parserOpts as any).headerPattern).toEqual(
/^(\w*)(?:\((.*)\))?: (.*)$/,
/^(\w*)(?:\((.*)\))?: (.*)$/
);
});

test("recursive resolves parser preset from conventional atom", async () => {
const cwd = await gitBootstrap(
"fixtures/recursive-parser-preset-conventional-atom",
"fixtures/recursive-parser-preset-conventional-atom"
);
await npm.installModules(
path.resolve(cwd, "first-extended", "second-extended"),
path.resolve(cwd, "first-extended", "second-extended")
);

const actual = await load({}, { cwd });
Expand All @@ -544,23 +545,23 @@
expect(actual.parserPreset!.name).toBe("conventional-changelog-atom");
expect(typeof actual.parserPreset!.parserOpts).toBe("object");
expect((actual.parserPreset!.parserOpts as any).headerPattern).toEqual(
/^(:.*?:) (.*)$/,
/^(:.*?:) (.*)$/
);
});

test("resolves parser preset from conventional commits without factory support", async () => {
const cwd = await npmBootstrap(
"fixtures/parser-preset-conventional-without-factory",
"fixtures/parser-preset-conventional-without-factory"
);
const actual = await load({}, { cwd });

expect(actual.parserPreset).toBeDefined();
expect(actual.parserPreset!.name).toBe(
"conventional-changelog-conventionalcommits",
"conventional-changelog-conventionalcommits"
);
expect(typeof actual.parserPreset!.parserOpts).toBe("object");
expect((actual.parserPreset!.parserOpts as any).headerPattern).toEqual(
/^(\w*)(?:\((.*)\))?!?: (.*)$/,
/^(\w*)(?:\((.*)\))?!?: (.*)$/
);
});

Expand All @@ -569,7 +570,7 @@
const actual = await load({}, { cwd });

expect(actual.helpUrl).toStrictEqual(
"https://github.com/conventional-changelog/commitlint",
"https://github.com/conventional-changelog/commitlint"
);
});

Expand All @@ -578,6 +579,6 @@
const actual = await load({}, { cwd });

expect(actual.helpUrl).toStrictEqual(
"https://github.com/conventional-changelog/commitlint/#what-is-commitlint",
"https://github.com/conventional-changelog/commitlint/#what-is-commitlint"
);
});
8 changes: 6 additions & 2 deletions @commitlint/load/src/utils/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const searchStrategy = "global";

export async function loadConfig(
cwd: string,
configPath?: string,
configPath?: string
): Promise<LoadConfigResult | null> {
let tsLoaderInstance: Loader | undefined;
const tsLoader: Loader = (...args) => {
Expand All @@ -42,6 +42,7 @@ export async function loadConfig(
searchPlaces: [
// cosmiconfig overrides default searchPlaces if any new search place is added (For e.g. `*.ts` files),
// we need to manually merge default searchPlaces from https://github.com/davidtheclark/cosmiconfig#searchplaces
"deno.json",
"package.json",
"package.yaml",
`.${moduleName}rc`,
Expand Down Expand Up @@ -102,5 +103,8 @@ export const isEsmModule = (cwd: string) => {
}

const packageJSON = readFileSync(packagePath, { encoding: "utf-8" });
return JSON.parse(packageJSON)?.type === "module";
return (
JSON.parse(packageJSON)?.type === "module" ||
existsSync(path.join(cwd, "deno.json"))
);
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ These can be modified by [your own configuration](#config).
- `commitlint.config.mjs`
- `commitlint.config.ts`
- `commitlint.config.cts`
- `commitlint` field in `deno.json`
- `commitlint` field in `package.json`
- `commitlint` field in [`package.yaml`](https://github.com/pnpm/pnpm/pull/1799)
- Packages: [cli](./@commitlint/cli), [core](./@commitlint/core)
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/local-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
echo "npx --no commitlint --edit `$1" > .husky/commit-msg
```

As an alternative you can create a script inside `package.json`
As an alternative you can create a script inside `package.json` or `deno.json`

```sh
npm pkg set scripts.commitlint="commitlint --edit"
Expand All @@ -59,7 +59,7 @@ echo "yarn commitlint --edit \$1" > .husky/commit-msg
echo "yarn commitlint --edit `$1" > .husky/commit-msg
```

As an alternative you can create a script inside `package.json`
As an alternative you can create a script inside `package.json` or `deno.json`

```sh
npm pkg set scripts.commitlint="commitlint --edit"
Expand All @@ -82,7 +82,7 @@ echo "pnpm dlx commitlint --edit \$1" > .husky/commit-msg
echo "pnpm dlx commitlint --edit `$1" > .husky/commit-msg
```

As an alternative you can create a script inside `package.json`
As an alternative you can create a script inside `package.json` or `deno.json`

```sh
npm pkg set scripts.commitlint="commitlint --edit"
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Configuration files are resolved using [cosmiconfig](https://github.com/cosmicon

## Config via `package.json`

You can add a `commitlint` field in `package.json` (or [`package.yaml`](https://github.com/pnpm/pnpm/pull/1799)) with an object that follows the below structure.
You can add a `commitlint` field in `package.json`, [`package.yaml`](https://github.com/pnpm/pnpm/pull/1799)) or `deno.json` with an object that follows the below structure.

## Config option CLI

Expand Down
Loading