Skip to content

Commit 0aea0f1

Browse files
fix(cli): load linkinator.config.json by default (#611)
1 parent 9e4bbac commit 0aea0f1

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

src/config.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ export type Flags = {
2222
urlRewriteReplace?: string;
2323
};
2424

25+
const validConfigExtensions = ['.js', '.mjs', '.cjs', '.json'];
26+
type ConfigExtensions = (typeof validConfigExtensions)[number];
27+
2528
export async function getConfig(flags: Flags) {
2629
// Check to see if a config file path was passed
27-
const configPath = flags.config || 'linkinator.config.json';
28-
let config: Flags = {};
29-
30+
let config: Flags;
3031
if (flags.config) {
31-
config = await parseConfigFile(configPath);
32+
config = await parseConfigFile(flags.config);
33+
} else {
34+
config = (await tryGetDefaultConfig()) || {};
3235
}
3336

3437
// `meow` is set up to pass boolean flags as `undefined` if not passed.
@@ -47,8 +50,20 @@ export async function getConfig(flags: Flags) {
4750
return config;
4851
}
4952

50-
const validConfigExtensions = ['.js', '.mjs', '.cjs', '.json'];
51-
type ConfigExtensions = (typeof validConfigExtensions)[number];
53+
/**
54+
* Attempt to load `linkinator.config.json`, assuming the user hasn't
55+
* passed a specific path to a config.
56+
* @returns The contents of the default config if present, or an empty config.
57+
*/
58+
async function tryGetDefaultConfig() {
59+
const defaultConfigPath = path.join(process.cwd(), 'linkinator.config.json');
60+
try {
61+
const config = await parseConfigFile(defaultConfigPath);
62+
return config;
63+
} catch (e) {
64+
return {};
65+
}
66+
}
5267

5368
async function parseConfigFile(configPath: string): Promise<Flags> {
5469
const typeOfConfig = getTypeOfConfig(configPath);

src/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {
1414
import { Queue } from './queue.js';
1515
import { startWebServer } from './server.js';
1616

17-
export { getConfig } from './config.js';
18-
1917
export enum LinkState {
2018
OK = 'OK',
2119
BROKEN = 'BROKEN',
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
Just a page
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"format": "json"
3+
}

test/test.cli.ts

+13
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ describe('cli', function () {
110110
assert.ok(output.links);
111111
});
112112

113+
it('should look for linkinator.config.json in the cwd', async () => {
114+
const response = await execa(node, ['../../../build/src/cli.js', '.'], {
115+
cwd: 'test/fixtures/defaultconfig',
116+
});
117+
let output: { passed: boolean };
118+
try {
119+
output = JSON.parse(response.stdout);
120+
assert.strictEqual(output.passed, true);
121+
} catch (e) {
122+
assert.fail('Expected JSON output');
123+
}
124+
});
125+
113126
it('should not show links if --silent', async () => {
114127
const response = await execa(node, [
115128
linkinator,

0 commit comments

Comments
 (0)