file-dependency-analyzer
Advanced tools
Comparing version
#!/usr/bin/env node | ||
import {run} from '../index.js' | ||
import { run } from '../index.js' | ||
run(); |
@@ -5,7 +5,7 @@ import getConfig from './lib/config.js'; | ||
export const run = async () => { | ||
const config = await getConfig(); | ||
const { extensions, globalIgnorePatterns } = await getConfig(); | ||
Object.entries(config).forEach(([extension, extConfig]) => { | ||
runAnalyzer(extension, extConfig); | ||
Object.entries(extensions).forEach(([extension, extConfig]) => { | ||
runAnalyzer(extension, extConfig, globalIgnorePatterns); | ||
}); | ||
}; |
@@ -13,3 +13,3 @@ import chalk from 'chalk'; | ||
const globalIgnoredPatterns = [ | ||
const defaultIgnorePatterns = [ | ||
'node_modules', | ||
@@ -22,8 +22,21 @@ 'git', | ||
'README.md', | ||
'dist', | ||
]; | ||
export const runAnalyzer = async (extension, config) => { | ||
const { mode, analyzeComments, ignorePatterns } = config; | ||
export const runAnalyzer = async (extension, config, globalIgnorePatterns) => { | ||
const { | ||
mode = 'analyze', | ||
analyzeComments = true, | ||
ignorePatterns = [], | ||
analyzeFrom = [], | ||
analyzeIn = [], | ||
} = config; | ||
const ignoredFilePatterns = [...globalIgnoredPatterns, ...ignorePatterns]; | ||
const ignoredFilePatterns = [ | ||
...new Set([ | ||
...defaultIgnorePatterns, | ||
...ignorePatterns, | ||
...globalIgnorePatterns, | ||
]), | ||
]; | ||
@@ -34,5 +47,11 @@ const filesWithProvidedExtension = getAllFilesForExtension( | ||
ignoredFilePatterns, | ||
extension | ||
extension, | ||
analyzeFrom | ||
); | ||
const allProjectFiles = getAllProjectFiles(getCWD(), ignoredFilePatterns); | ||
const allProjectFiles = getAllProjectFiles( | ||
getCWD(), | ||
ignoredFilePatterns, | ||
analyzeIn | ||
); | ||
const dataStreams = await mapToReadableFiles(allProjectFiles); | ||
@@ -79,2 +98,6 @@ const mappedDataStreams = dataStreams.map(stream => | ||
removeFiles(notUsedFiles); | ||
console.log( | ||
chalk.magenta('The following files have been removed:'), | ||
notUsedFiles | ||
); | ||
}; |
import chalk from 'chalk'; | ||
import path from 'path'; | ||
import {readFile} from "./files.js"; | ||
import { readFile } from './files.js'; | ||
export default async function() { | ||
export default async function () { | ||
const [, , configPath] = process.argv; | ||
@@ -11,2 +11,3 @@ | ||
console.log(chalk.red('Please specify json configuration file')); | ||
process.exit(0); | ||
} | ||
@@ -17,10 +18,10 @@ | ||
if(!config) { | ||
if (!config) { | ||
console.log( | ||
chalk.yellow( | ||
'Please provide at least one config for desired extension' | ||
'Please provide at least one extension config' | ||
) | ||
); | ||
process.exit(0) | ||
process.exit(0); | ||
} | ||
@@ -27,0 +28,0 @@ |
import fs, { promises as fsPromises } from 'fs'; | ||
import path from 'path'; | ||
import chalk from 'chalk' | ||
import chalk from 'chalk'; | ||
import { getCWD } from './utils.js'; | ||
export function getAllFilesForExtension( | ||
@@ -9,3 +11,4 @@ base, | ||
ignoredPatters, | ||
ext, | ||
extension, | ||
analyzeFrom, | ||
files, | ||
@@ -24,3 +27,7 @@ result | ||
if (isIgnored) { | ||
const isNotAnalyzedFromCorrectDir = analyzeFrom.some( | ||
dir => !newBase.includes(dir) | ||
); | ||
if (isIgnored || isNotAnalyzedFromCorrectDir) { | ||
return; | ||
@@ -34,3 +41,4 @@ } | ||
ignoredPatters, | ||
ext, | ||
extension, | ||
analyzeFrom, | ||
fs.readdirSync(newBase), | ||
@@ -47,3 +55,6 @@ result | ||
if (file.substr(-1 * (ext.length + 1)) == '.' + ext) { | ||
if ( | ||
file.substr(-1 * (extension.length + 1)) === | ||
'.' + extension | ||
) { | ||
result.push(newFile); | ||
@@ -63,2 +74,3 @@ } | ||
ignoredFilePatterns, | ||
analyzeIn, | ||
files, | ||
@@ -74,7 +86,11 @@ result | ||
const isIgnored = [...ignoredFilePatterns].some(res => | ||
const isIgnored = ignoredFilePatterns.some(res => | ||
newBase.includes(res) | ||
); | ||
if (isIgnored) { | ||
const isNotAnalyzedFromCorrectDir = analyzeIn.some( | ||
dir => !newBase.includes(dir) | ||
); | ||
if (isIgnored || isNotAnalyzedFromCorrectDir) { | ||
return; | ||
@@ -87,2 +103,3 @@ } | ||
ignoredFilePatterns, | ||
analyzeIn, | ||
fs.readdirSync(newBase), | ||
@@ -106,3 +123,3 @@ result | ||
} catch (err) { | ||
console.log(chalk.red(`Cannot read file of path ${filePath}`)) | ||
console.log(chalk.red(`Cannot read file of path ${filePath}`)); | ||
} | ||
@@ -109,0 +126,0 @@ }; |
import path from 'path'; | ||
import {readFile} from './files.js' | ||
import { readFile } from './files.js'; | ||
@@ -4,0 +4,0 @@ export const getCWD = () => { |
@@ -5,9 +5,6 @@ { | ||
"author": "Rafal Poreba", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"main": "index.js", | ||
"type": "module", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "node ./bin/index.js" | ||
}, | ||
"bin": "./bin/index.js", | ||
@@ -30,3 +27,4 @@ "dependencies": { | ||
"remove", | ||
"files" | ||
"files", | ||
"javaScript" | ||
], | ||
@@ -33,0 +31,0 @@ "bugs": { |
106
README.md
@@ -1,1 +0,105 @@ | ||
Todo | ||
# File dependency analyzer | ||
Maintaining a project is not easy, especially large ones. | ||
To help, this tool is used to detect and optionally cleanup not dependent files. | ||
You can analyze and cleanup any files you want (`svg, js, etc`). Just provide configuration and you are ready to go. | ||
Tool is fully customizable, you can specify folders you want to analyze files in, or exclude folders from analyzing. | ||
## Installation | ||
--- | ||
``` | ||
npm install -D file-dependency-analyzer | ||
``` | ||
## Usage | ||
--- | ||
To run a script you need `npx` (npm is not currently supported) | ||
``` | ||
// package.json | ||
"scripts": { | ||
"analyze": npx file-dependency-analyzer ./path-to-config-file.json | ||
} | ||
``` | ||
## Config | ||
Default ignore patterns (analyzing will be excluded in below folders) | ||
``` | ||
[ | ||
'node_modules', | ||
'git', | ||
'vscode', | ||
'idea', | ||
'package.json', | ||
'package-lock.json', | ||
'README.md', | ||
'dist', | ||
] | ||
``` | ||
You can add specific `globalIgnorePatterns` in config | ||
--- | ||
| Property | Type | Default | Description | | ||
|----------------------|:--------|:------|:---------------------------------------------------------:| | ||
| `globalIgnorePatterns` | `Array` | `[]` | List of all folders/paths you want exclude from analyzing | | ||
| `extensions` | `Object` | `{}` | Object of extensions with specific configuration | | ||
### Extensions | ||
Is a key value object with specific configuration | ||
--- | ||
| Property | Type | Default | Description | | ||
|------------------|:---------|:----------|:-------------------------------------------------------------:| | ||
| `analyzeFrom` | `Array` | `[]` | List of folders where files to analyze are located | | ||
| `analyzeIn` | `Array` | `[]` | List of folders you want to analyze files | | ||
| `ignorePatterns` | `Array` | `[]` | List of folders you exclude from analyzing | | ||
| `mode` | `String` | `analyze` | `analyze` or `cleanup` (analyze and remove unused files) | | ||
| `analyzeComments` | `Boolean` | `true` | Analyzing commented dependencies | | ||
If `analyzeFrom`, `analyzeIn` are empty or not defined, files will be analyzed for all project files. | ||
Config example | ||
--- | ||
``` | ||
{ | ||
"globalIgnorePatterns": [".docker-cache"], | ||
"extensions": { | ||
"svg": { | ||
"analyzeFrom": [], <- it will search for all svg files throughout the project | ||
"analyzeIn": ["/components"], | ||
"ignorePatterns": ["/configs"], | ||
"mode": "cleanup", | ||
"analyzeComments": true | ||
}, | ||
"js": { | ||
"analyzeFrom": ["/configs", "/assets"], | ||
"analyzeIn": [], <- files will be analyzed throughout the project | ||
"ignorePatterns": [], | ||
"mode": "analyze", | ||
"analyzeComments": false | ||
} | ||
} | ||
} | ||
``` | ||
## License | ||
--- | ||
Copyright (c) 2022 Rafal Poreba. Licensed under the MIT license. |
13207
40.89%278
12.1%106
5200%9
-10%