Skip to content

Commit

Permalink
fix(cli): load linkinator.config.json by default (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Jul 1, 2024
1 parent 9e4bbac commit 0aea0f1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
27 changes: 21 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ export type Flags = {
urlRewriteReplace?: string;
};

const validConfigExtensions = ['.js', '.mjs', '.cjs', '.json'];
type ConfigExtensions = (typeof validConfigExtensions)[number];

export async function getConfig(flags: Flags) {
// Check to see if a config file path was passed
const configPath = flags.config || 'linkinator.config.json';
let config: Flags = {};

let config: Flags;
if (flags.config) {
config = await parseConfigFile(configPath);
config = await parseConfigFile(flags.config);
} else {
config = (await tryGetDefaultConfig()) || {};
}

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

const validConfigExtensions = ['.js', '.mjs', '.cjs', '.json'];
type ConfigExtensions = (typeof validConfigExtensions)[number];
/**
* Attempt to load `linkinator.config.json`, assuming the user hasn't
* passed a specific path to a config.
* @returns The contents of the default config if present, or an empty config.
*/
async function tryGetDefaultConfig() {
const defaultConfigPath = path.join(process.cwd(), 'linkinator.config.json');
try {
const config = await parseConfigFile(defaultConfigPath);
return config;
} catch (e) {
return {};
}
}

async function parseConfigFile(configPath: string): Promise<Flags> {
const typeOfConfig = getTypeOfConfig(configPath);
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
import { Queue } from './queue.js';
import { startWebServer } from './server.js';

export { getConfig } from './config.js';

export enum LinkState {
OK = 'OK',
BROKEN = 'BROKEN',
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/defaultconfig/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
Just a page
</body>
</html>
3 changes: 3 additions & 0 deletions test/fixtures/defaultconfig/linkinator.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"format": "json"
}
13 changes: 13 additions & 0 deletions test/test.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ describe('cli', function () {
assert.ok(output.links);
});

it('should look for linkinator.config.json in the cwd', async () => {
const response = await execa(node, ['../../../build/src/cli.js', '.'], {
cwd: 'test/fixtures/defaultconfig',
});
let output: { passed: boolean };
try {
output = JSON.parse(response.stdout);
assert.strictEqual(output.passed, true);
} catch (e) {
assert.fail('Expected JSON output');
}
});

it('should not show links if --silent', async () => {
const response = await execa(node, [
linkinator,
Expand Down

0 comments on commit 0aea0f1

Please sign in to comment.