@adguard/aglint
Version:
Universal adblock filter list linter.
48 lines (45 loc) • 1.88 kB
JavaScript
/*
* AGLint v3.0.0 (build date: Wed, 21 May 2025 13:24:14 GMT)
* (c) 2025 AdGuard Software Ltd.
* Released under the MIT license
* https://github.com/AdguardTeam/AGLint#readme
*/
import path from 'node:path';
import cloneDeep from 'clone-deep';
import { parseConfigFile } from './config-reader.js';
import { mergeConfigs } from '../config.js';
/**
* Walks a `ScannedDirectory` object and performs an action on each file and directory
* in the tree.
*
* @param scannedDirectory The `ScannedDirectory` object to be walked
* @param events The events to be performed on each file and directory
* @param config The CLI config
* @param fix Fix fixable problems automatically
*/
async function walk(scannedDirectory, events, config, fix = false) {
// Initially the used config is the provided config
let usedConfig = config;
// But if the directory contains a config file, we merge two configs
if (scannedDirectory.configFile) {
const filePath = path.join(scannedDirectory.configFile.dir, scannedDirectory.configFile.base);
const fileConfig = await parseConfigFile(filePath);
usedConfig = mergeConfigs(usedConfig, fileConfig);
}
// Make a deep clone of the config so that it can be modified without affecting the original
// in any way
const configDeepClone = cloneDeep(usedConfig);
// Perform the directory action on the current directory (if it exists)
if (events.dir) {
await events.dir(scannedDirectory.dir, configDeepClone, fix);
}
// Perform the file action on each lintable file
for (const file of scannedDirectory.lintableFiles) {
await events.file(file, configDeepClone, fix);
}
// Recursively walk each subdirectory
for (const subDir of scannedDirectory.subdirectories) {
await walk(subDir, events, configDeepClone, fix);
}
}
export { walk };