Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verbose output option #6

Merged
merged 6 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ npm i @j9t/obsohtml

The script accepts a folder path as a command line option, which can be specified in both short form (`-f`) and long form (`--folder`). The folder path can be either absolute or relative.

The script can be run in “verbose” mode by appending `-v` or `--verbose` to the command. This will show information about files and directories that were skipped.

##### Example Commands

Use the default directory (user home directory):
Expand Down Expand Up @@ -46,7 +48,7 @@ Download or fork [the source repository](https://github.com/j9t/obsohtml).

#### Execution

The script accepts a folder path as a command line option, which can be specified in both short form (`-f`) and long form (`--folder`). The folder path can be either absolute or relative.
As mentioned above, the script accepts a folder (`-f`, `--folder`) and can be run in “verbose” mode (`-v`, `--verbose`).

##### Example Commands

Expand Down
32 changes: 23 additions & 9 deletions bin/obsohtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ async function findObsolete(filePath) {
}

// Function to walk through the project directory, excluding node_modules directories
function walkDirectory(directory) {
function walkDirectory(directory, verbose) {
const MAX_PATH_LENGTH = 255; // Adjust this value based on your OS limits
let files;

try {
files = fs.readdirSync(directory);
} catch (err) {
if (err.code === 'EPERM' || err.code === 'EACCES') {
console.warn(`Skipping directory due to permissions: ${directory}`);
if (verbose) console.warn(`Skipping directory due to permissions: ${directory}`);
return;
} else if (err.code === 'ENOENT') {
console.warn(`Skipping non-existent directory: ${directory}`);
if (verbose) console.warn(`Skipping non-existent directory: ${directory}`);
return;
} else {
throw err;
Expand All @@ -61,17 +63,27 @@ function walkDirectory(directory) {
files.forEach(file => {
const fullPath = path.join(directory, file);

if (fullPath.length > MAX_PATH_LENGTH) {
if (verbose) console.warn(`Skipping file or directory with path too long: ${fullPath}`);
return;
}

try {
const stats = fs.lstatSync(fullPath);
if (stats.isSymbolicLink()) {
if (verbose) console.warn(`Skipping symbolic link: ${fullPath}`);
return;
}
if (fs.statSync(fullPath).isDirectory()) {
if (file !== 'node_modules') {
walkDirectory(fullPath);
walkDirectory(fullPath, verbose);
}
} else if (fullPath.endsWith('.html') || fullPath.endsWith('.htm') || fullPath.endsWith('.php') || fullPath.endsWith('.js') || fullPath.endsWith('.ts')) {
findObsolete(fullPath);
}
} catch (err) {
if (err.code === 'ENOENT') {
console.warn(`Skipping non-existent file or directory: ${fullPath}`);
if (verbose) console.warn(`Skipping non-existent file or directory: ${fullPath}`);
} else {
throw err;
}
Expand All @@ -80,16 +92,18 @@ function walkDirectory(directory) {
}

// Main function to execute the script
async function main(projectDirectory = defaultProjectDirectory) {
await walkDirectory(projectDirectory);
async function main(projectDirectory = defaultProjectDirectory, verbose = false) {
await walkDirectory(projectDirectory, verbose);
}

// Define command line options
program
.option('-f, --folder <path>', 'specify the project directory', defaultProjectDirectory)
.option('-v, --verbose', 'enable verbose output')
.parse(process.argv);

// Get the project directory from command line arguments or use the default
// Get the project directory and verbose flag from command line arguments or use the default
const options = program.opts();
const projectDirectory = options.folder;
main(projectDirectory);
const verbose = options.verbose;
main(projectDirectory, verbose);
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@j9t/obsohtml",
"description": "Find obsolete HTML elements and attributes",
"author": "Jens Oliver Meiert",
"version": "1.4.0",
"version": "1.5.0",
"license": "CC-BY-SA-4.0",
"repository": {
"type": "git",
Expand Down
Loading