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

fix(cli): load linkinator.config.json by default #611

Merged
merged 2 commits into from
Jul 1, 2024
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
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