diff --git a/src/config.ts b/src/config.ts index 5e84bdf..b799331 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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. @@ -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,