From a471d98c47dce281f1bfe77666507005214b13ba Mon Sep 17 00:00:00 2001 From: Dristpunk Date: Mon, 5 Feb 2024 15:51:44 +0300 Subject: [PATCH] fix: replace paths with glob --- src/main.ts | 13 +++++++------ src/types.ts | 2 +- src/utils.ts | 24 ++++++------------------ test/processor.test.ts | 2 +- test/validator.test.ts | 4 ++-- 5 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/main.ts b/src/main.ts index 7dc256a..dc900c1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import yargs from 'yargs'; import { hideBin } from 'yargs/helpers'; -import { globSync } from 'fast-glob'; +import { glob } from 'fast-glob'; import { getProjectCompiledSources } from './utils'; import { Processor } from './processor'; import { Config } from './types'; @@ -10,8 +10,10 @@ import { Validator } from './validator'; (async () => { const config: Config = getArguments(); - const excludedPaths = config.exclude.map((path) => globSync(path, { cwd: config.root })).flat(); - const sourceUnits = await getProjectCompiledSources(config.root, config.include, excludedPaths); + const includedPaths = await glob(config.include, { cwd: config.root }); + const excludedPaths = await glob(config.exclude, { cwd: config.root }); + + const sourceUnits = await getProjectCompiledSources(config.root, includedPaths, excludedPaths); if (!sourceUnits.length) return console.error('No solidity files found in the specified directory'); const validator = new Validator(config); @@ -42,10 +44,9 @@ function getArguments(): Config { required: true, }, exclude: { - type: 'array', + type: 'string', description: 'Glob patterns of files to exclude.', - default: [], - string: true, + default: '', }, root: { type: 'string', diff --git a/src/types.ts b/src/types.ts index 8d194d4..d4d4e2a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,7 +10,7 @@ import { export interface Config { include: string; // Required: Glob pattern of files to process. - exclude: string[]; // Optional: Glob patterns of files to exclude. + exclude: string; // Optional: Glob pattern of files to exclude. root: string; // Optional: Project root directory. enforceInheritdoc: boolean; // Optional: True if all external and public functions should have @inheritdoc. constructorNatspec: boolean; // Optional: True if the constructor should have natspec. diff --git a/src/utils.ts b/src/utils.ts index e1de606..b248f43 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,25 +3,13 @@ import path from 'path'; import { ASTKind, ASTReader, SourceUnit, compileSol } from 'solc-typed-ast'; import { Natspec, NatspecDefinition, NodeToProcess } from './types'; -export async function getSolidityFiles(dir: string): Promise { - let files = await fs.readdir(dir, { withFileTypes: true }); - let solidityFiles: string[] = []; - - for (const file of files) { - const res = path.resolve(dir, file.name); - if (file.isDirectory()) { - solidityFiles = solidityFiles.concat(await getSolidityFiles(res)); - } else if (file.isFile() && file.name.endsWith('.sol')) { - solidityFiles.push(res); - } - } - - return solidityFiles; +export async function getSolidityFilesAbsolutePaths(files: string[]): Promise { + return files.filter((file) => file.endsWith('.sol')).map((file) => path.resolve(file)); } -export async function getProjectCompiledSources(rootPath: string, includedPath: string, excludedPaths: string[]): Promise { +export async function getProjectCompiledSources(rootPath: string, includedPaths: string[], excludedPaths: string[]): Promise { // Fetch Solidity files from the specified directory - const solidityFiles: string[] = await getSolidityFiles(includedPath); + const solidityFiles: string[] = await getSolidityFilesAbsolutePaths(includedPaths); const remappings: string[] = await getRemappings(rootPath); const compiledFiles = await compileSol(solidityFiles, 'auto', { @@ -34,9 +22,9 @@ export async function getProjectCompiledSources(rootPath: string, includedPath: new ASTReader() .read(compiledFiles.data, ASTKind.Any, compiledFiles.files) // avoid processing files that are not in the specified directory, e.g. node modules or other imported files - .filter((sourceUnit) => isFileInDirectory(includedPath, sourceUnit.absolutePath)) + .filter((sourceUnit) => includedPaths.includes(sourceUnit.absolutePath)) // avoid processing files from excluded directories - .filter((sourceUnit) => !excludedPaths.some((excludedPath) => excludedPath === sourceUnit.absolutePath)) + .filter((sourceUnit) => !excludedPaths.includes(sourceUnit.absolutePath)) ); } diff --git a/test/processor.test.ts b/test/processor.test.ts index 22ca667..ee0148c 100644 --- a/test/processor.test.ts +++ b/test/processor.test.ts @@ -11,7 +11,7 @@ describe('Processor', () => { const config: Config = { root: '.', include: './sample-data', - exclude: [], + exclude: '', enforceInheritdoc: false, constructorNatspec: false, }; diff --git a/test/validator.test.ts b/test/validator.test.ts index 26746a6..b0b702b 100644 --- a/test/validator.test.ts +++ b/test/validator.test.ts @@ -11,7 +11,7 @@ describe('Validator', () => { let config: Config = { root: '.', include: './sample-data', - exclude: [], + exclude: '', enforceInheritdoc: false, constructorNatspec: false, }; @@ -344,7 +344,7 @@ describe('Validator', () => { config = { root: '.', include: './sample-data', - exclude: [], + exclude: '', enforceInheritdoc: true, constructorNatspec: false, };