Skip to content

Commit

Permalink
fix: replace paths with glob (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
dristpunk authored Feb 7, 2024
1 parent 7c54f04 commit d91d1e3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
13 changes: 7 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 6 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@ import path from 'path';
import { ASTKind, ASTReader, SourceUnit, compileSol, FunctionDefinition } from 'solc-typed-ast';
import { Natspec, NatspecDefinition, NodeToProcess } from './types';

export async function getSolidityFiles(dir: string): Promise<string[]> {
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<string[]> {
return files.filter((file) => file.endsWith('.sol')).map((file) => path.resolve(file));
}

export async function getProjectCompiledSources(rootPath: string, includedPath: string, excludedPaths: string[]): Promise<SourceUnit[]> {
export async function getProjectCompiledSources(rootPath: string, includedPaths: string[], excludedPaths: string[]): Promise<SourceUnit[]> {
// 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', {
Expand All @@ -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))
);
}

Expand Down
2 changes: 1 addition & 1 deletion test/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Processor', () => {
const config: Config = {
root: '.',
include: './sample-data',
exclude: [],
exclude: '',
enforceInheritdoc: false,
constructorNatspec: false,
};
Expand Down
4 changes: 2 additions & 2 deletions test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Validator', () => {
let config: Config = {
root: '.',
include: './sample-data',
exclude: [],
exclude: '',
enforceInheritdoc: false,
constructorNatspec: false,
};
Expand Down Expand Up @@ -355,7 +355,7 @@ describe('Validator', () => {
config = {
root: '.',
include: './sample-data',
exclude: [],
exclude: '',
enforceInheritdoc: true,
constructorNatspec: false,
};
Expand Down

0 comments on commit d91d1e3

Please sign in to comment.