From a6208c041e59ba88e614c1dd4e9861834501f089 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Mon, 1 Jul 2024 13:38:31 -0400 Subject: [PATCH 1/2] fix(cli): load linkinator.config.json by default --- src/config.ts | 29 ++++++++++++++----- src/index.ts | 2 -- test/fixtures/defaultconfig/index.html | 5 ++++ .../defaultconfig/linkinator.config.json | 3 ++ test/test.cli.ts | 13 +++++++++ 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 test/fixtures/defaultconfig/index.html create mode 100644 test/fixtures/defaultconfig/linkinator.config.json diff --git a/src/config.ts b/src/config.ts index 5e84bdf..aba54e2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -22,15 +22,18 @@ 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. // copy the struct, and delete properties that are `undefined` so the merge // doesn't blast away config level settings. @@ -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 { const typeOfConfig = getTypeOfConfig(configPath); diff --git a/src/index.ts b/src/index.ts index bea579a..72f6382 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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', diff --git a/test/fixtures/defaultconfig/index.html b/test/fixtures/defaultconfig/index.html new file mode 100644 index 0000000..4f3d32f --- /dev/null +++ b/test/fixtures/defaultconfig/index.html @@ -0,0 +1,5 @@ + + +Just a page + + diff --git a/test/fixtures/defaultconfig/linkinator.config.json b/test/fixtures/defaultconfig/linkinator.config.json new file mode 100644 index 0000000..68474d8 --- /dev/null +++ b/test/fixtures/defaultconfig/linkinator.config.json @@ -0,0 +1,3 @@ +{ + "format": "json" +} diff --git a/test/test.cli.ts b/test/test.cli.ts index fac1a9e..99b43ce 100644 --- a/test/test.cli.ts +++ b/test/test.cli.ts @@ -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, From e1bc72bd56c335d3701d4ffeea474e72931cc317 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Mon, 1 Jul 2024 13:39:43 -0400 Subject: [PATCH 2/2] fixy --- src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index aba54e2..b799331 100644 --- a/src/config.ts +++ b/src/config.ts @@ -33,7 +33,7 @@ export async function getConfig(flags: Flags) { } else { config = (await tryGetDefaultConfig()) || {}; } - + // `meow` is set up to pass boolean flags as `undefined` if not passed. // copy the struct, and delete properties that are `undefined` so the merge // doesn't blast away config level settings.