linkinator
Advanced tools
Comparing version 4.0.3 to 4.1.0
import { promises as fs } from 'fs'; | ||
import path from 'path'; | ||
export async function getConfig(flags) { | ||
// check to see if a config file path was passed | ||
const configPath = flags.config || 'linkinator.config.json'; | ||
let configData; | ||
try { | ||
configData = await fs.readFile(configPath, { encoding: 'utf8' }); | ||
} | ||
catch (e) { | ||
if (flags.config) { | ||
console.error(`Unable to find config file ${flags.config}`); | ||
throw e; | ||
} | ||
} | ||
let config = {}; | ||
if (configData) { | ||
config = JSON.parse(configData); | ||
if (flags.config) { | ||
config = await parseConfigFile(configPath); | ||
} | ||
@@ -34,2 +25,33 @@ // `meow` is set up to pass boolean flags as `undefined` if not passed. | ||
} | ||
const validConfigExtensions = ['.js', '.mjs', '.cjs', '.json']; | ||
async function parseConfigFile(configPath) { | ||
const typeOfConfig = getTypeOfConfig(configPath); | ||
switch (typeOfConfig) { | ||
case '.json': | ||
return readJsonConfigFile(configPath); | ||
case '.js': | ||
case '.mjs': | ||
case '.cjs': | ||
return importConfigFile(configPath); | ||
} | ||
throw new Error(`Config file ${configPath} is invalid`); | ||
} | ||
function getTypeOfConfig(configPath) { | ||
// Returning json in case file doesn't have an extension for backward compatibility | ||
const configExtension = path.extname(configPath) || '.json'; | ||
if (validConfigExtensions.includes(configExtension)) { | ||
return configExtension; | ||
} | ||
throw new Error(`Config file should be either of extensions ${validConfigExtensions.join(',')}`); | ||
} | ||
async function importConfigFile(configPath) { | ||
const config = (await import(`file://${path.resolve(process.cwd(), configPath)}`)).default; | ||
return config; | ||
} | ||
async function readJsonConfigFile(configPath) { | ||
const configFileContents = await fs.readFile(configPath, { | ||
encoding: 'utf-8', | ||
}); | ||
return JSON.parse(configFileContents); | ||
} | ||
//# sourceMappingURL=config.js.map |
{ | ||
"name": "linkinator", | ||
"description": "Find broken links, missing images, etc in your HTML. Scurry around your site and find all those broken links.", | ||
"version": "4.0.3", | ||
"version": "4.1.0", | ||
"license": "MIT", | ||
@@ -42,3 +42,3 @@ "repository": "JustinBeckwith/linkinator", | ||
"@types/mime": "^3.0.0", | ||
"@types/mocha": "^9.0.0", | ||
"@types/mocha": "^10.0.0", | ||
"@types/node": "^16.11.7", | ||
@@ -45,0 +45,0 @@ "@types/server-destroy": "^1.0.1", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107309
1451