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

Combine Folders Feature Added #115

Closed
wants to merge 8 commits into from
Closed
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
49 changes: 31 additions & 18 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,52 @@ commander
.usage('[options] <folders...>')
.version(version, '-v, --version')
.option('-i, --ignore <ignoredVars>', 'ignore variables, comma separated')
.option('-c, --combine [isCombineDirs]', 'combine directories')
.parse(process.argv);

const ignore = commander.ignore ? commander.ignore.split(',') : [];
const combine = commander.combine ? commander.combine : false;

function main(args) {
const ignore = commander.ignore ? commander.ignore.split(',') : [];
let unusedListing = [];

console.log('Looking for unused variables');

let unusedList = [];
if (combine === true) {
unusedListing = getUnunsedVar(args);
} else {
args.forEach(arg => {
const dir = path.resolve(arg);
danny007in marked this conversation as resolved.
Show resolved Hide resolved
unusedListing = getUnunsedVar(dir);
});
}

if (unusedListing.length === 0) {
console.log('No unused variables found!');
process.exit(0);
}

args.forEach(arg => {
const dir = path.resolve(arg);
process.exit(1);
}

console.log(`Finding unused variables in "${chalk.cyan.bold(dir)}"...`);
function getUnunsedVar(argVal) {
const unusedList = [];

// eslint-disable-next-line unicorn/no-array-callback-reference
const unusedVars = fusv.find(dir, { ignore });
console.log(`Finding unused variables in "${chalk.cyan.bold(argVal)}"...`);

console.log(`${chalk.cyan.bold(unusedVars.total)} total variables.`);
// eslint-disable-next-line unicorn/no-array-callback-reference
const unusedVars = fusv.find(argVal, { ignore, combine });

unusedVars.unused.forEach(unusedVar => {
console.log(`Variable ${chalk.bold(unusedVar)} is not being used!`);
});
console.log(`${chalk.cyan.bold(unusedVars.total)} total variables.`);

// eslint-disable-next-line unicorn/prefer-spread
unusedList = unusedList.concat(unusedVars.unused);
unusedVars.unused.forEach(unusedVar => {
console.log(`Variable ${chalk.bold(unusedVar)} is not being used!`);
});

if (unusedList.length === 0) {
console.log('No unused variables found!');
process.exit(0);
}
// eslint-disable-next-line unicorn/prefer-spread
[unusedList].concat(unusedVars.unused);

process.exit(1);
return unusedList;
}

const args = commander.args.filter(arg => typeof arg === 'string');
Expand Down
33 changes: 25 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@ const escapeRegex = require('escape-string-regexp');
const parse = require('./lib/parse-variable');

const defaultOptions = {
ignore: []
ignore: [],
combine: false
};

function findUnusedVars(strDir, opts) {
const options = Object.assign(defaultOptions, opts);
const dir = path.isAbsolute(strDir) ? strDir : path.resolve(strDir);

// Trim list of ignored variables
options.ignore = options.ignore.map(val => val.trim());

if (Boolean(options.ignore) && !Array.isArray(options.ignore)) {
throw new TypeError('`ignore` should be an Array');
}

// Trim list of ignored variables
options.ignore = options.ignore.map(val => val.trim());
let sassFiles;
if (options.combine === true) {
const sassFile = [];
strDir.forEach((strPath, i) => {
sassFile[i] = getPath(strPath);

if (!(fs.existsSync(dir) && fs.statSync(dir).isDirectory())) {
throw new Error(`"${dir}": Not a valid directory!`);
// eslint-disable-next-line unicorn/prefer-spread
sassFiles = [].concat(...sassFile);
});
} else {
sassFiles = getPath(strDir);
}

// Array of all Sass files
const sassFiles = glob.sync(path.join(dir, '**/*.scss'));

// String of all Sass files' content
let sassFilesString = sassFiles.reduce((sassStr, file) => {
sassStr += fs.readFileSync(file, 'utf8');
Expand All @@ -54,6 +61,16 @@ function findUnusedVars(strDir, opts) {
};
}

function getPath(strPath) {
const dir = path.isAbsolute(strPath) ? strPath : path.resolve(strPath);

if (!(fs.existsSync(dir) && fs.statSync(dir).isDirectory())) {
throw new Error(`"${dir}": Not a valid directory!`);
}

return glob.sync(path.join(dir, '**/*.scss'));
}

module.exports = {
find: findUnusedVars
};
Loading