Skip to content

Instantly share code, notes, and snippets.

@daviddulak
Last active October 25, 2017 19:25
Show Gist options
  • Save daviddulak/7d965aeca71c17ad63c127c63c051a74 to your computer and use it in GitHub Desktop.
Save daviddulak/7d965aeca71c17ad63c127c63c051a74 to your computer and use it in GitHub Desktop.
This searches for unused images in a project
var TextSearch = require('rx-text-search');
var walk = require('walk');
var colors = require('colors');
var Promise = require('bluebird');
// *************************************************
// Required Node Packages
//
// "rx-text-search": "1.0.0",
// "walk": "2.3.9",
// "colors": "1.1.2"
// "bluebird": "3.5.0",
//
// *************************************************
// *************************************************
// *************************************************
// Configuration
// *************************************************
// *************************************************
let searchLocations = ['./src/ui-kit/assets/', './src/assets/']; // Search these directories for files
let fileExtensionMatchArray = ['png', 'jpg', 'gif']; // These are the file type to check if they are unused
let fileIgnoreArray = []; // Ignore files with these strings in their name
let fileNameStringRemovalArray = ['@2x', '@3x']; // These strings will be removed from the file name when matching
let searchForUsageInThisDirectory = 'src'; // This is the directory to search for usage
let searchForUsageInTheseFileTypes = '**/*.js'; // example '**/*.js'
let filesArray = [];
let unusedFilesArray = [];
// Match against file types
function fileTypeMatch(filename) {
let isMatch = false;
fileExtensionMatchArray.forEach((str) => {
if (filename.indexOf('.'+str) !== -1) {
isMatch = true;
}
});
return isMatch;
}
// Ignore files that contain these strings
function fileIgnore(filename) {
let shouldIgnore = false;
fileIgnoreArray.forEach((str) => {
if (filename.indexOf(str) !== -1) {
shouldIgnore = true;
}
});
return shouldIgnore;
}
// Remove these strings from the filename
function fileNameStringRemoval(filename) {
fileNameStringRemovalArray.forEach((str) => {
filename = filename.replace(str, '');
});
return filename;
}
// Is the file already accounted for in the array
function inArray(ar, name, location) {
let isInArray = false;
ar.forEach((obj) => {
if (name === obj.fileName && location === obj.location) {
isInArray = true;
}
});
return isInArray;
}
// Get file list from this directory
function getFileList(locations, index = 0) {
return new Promise(function (resolve, reject) {
var walker = walk.walk(locations[index], { followLinks: false });
walker.on('file', function(root, stat, next) {
// Add this file to the list of files
let fileName = fileNameStringRemoval(stat.name);
let location = root.replace('//', '/');
if (fileTypeMatch(fileName) && !fileIgnore(fileName) && !inArray(filesArray, fileName, location)) {
filesArray.push({
"fileName": fileName,
"location": location
});
}
next();
});
walker.on('end', function() {
resolve(filesArray);
});
});
}
// Get all file from the array of locations
function getFileListRecursive(locations) {
return new Promise(function (resolve, reject) {
let iterator = 0;
let iterationCount = locations.length;
function loop() {
if (iterator === iterationCount) {
//done
resolve(filesArray);
return;
} else {
getFileList(locations, iterator).then(()=>{
iterator++;
loop();
});
}
}
loop();
});
}
// Check to see if this file is used
function checkIfUnused(files, index = 0) {
return new Promise(function (resolve, reject) {
TextSearch.findAsPromise(files[index]['fileName'], searchForUsageInTheseFileTypes, {cwd: searchForUsageInThisDirectory})
.then(function (result) {
if (result.length > 0) {
//console.log('found');
} else {
unusedFilesArray.push(files[index]);
}
resolve();
})
.catch(function (err) {
console.log('Error searching for file: '+ files[index]);
resolve();
});
});
}
// Check all files if they are used
function checkIfUnusedRecursive(files) {
return new Promise(function (resolve, reject) {
let iterator = 0;
let iterationCount = files.length;
function loop() {
if (iterator === iterationCount) {
//done
resolve(unusedFilesArray);
return;
} else {
checkIfUnused(files, iterator).then(()=>{
iterator++;
loop();
});
}
}
loop();
});
}
// *************************************************
// *************************************************
// Start the process
// *************************************************
// *************************************************
getFileListRecursive(searchLocations).then((files) => {
checkIfUnusedRecursive(files).then((unused) => {
console.log(`****** Items found: ${unused.length} ******`.bold.yellow);
unused.forEach((obj) => {
let name = `${obj.location}/${obj.fileName}`;
name = name.replace('//', '/');
console.log(`${name}`.blue);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment