From 6f9215b7d1acf64e1772b95d933c67d6366ff55c Mon Sep 17 00:00:00 2001 From: XhmikosR Date: Sun, 26 Nov 2017 13:35:49 +0200 Subject: [PATCH] Pure Node.js approach. --- build/lint-vars.js | 29 +++++++++++++++-------------- package-lock.json | 18 +++++++++--------- package.json | 1 + 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/build/lint-vars.js b/build/lint-vars.js index d9f76ea5f988..909f5293d788 100644 --- a/build/lint-vars.js +++ b/build/lint-vars.js @@ -10,10 +10,9 @@ 'use strict' +const fs = require('fs') const path = require('path') -const sh = require('shelljs') - -sh.config.fatal = true +const glob = require('glob') // Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37 function regExpQuote(str) { @@ -23,7 +22,7 @@ function regExpQuote(str) { let globalSuccess = true function findUnusedVars(dir) { - if (!sh.test('-d', dir)) { + if (!(fs.existsSync(dir) && fs.statSync(dir).isDirectory())) { console.log(`"${dir}": Not a valid directory!`) process.exit(1) } @@ -33,22 +32,24 @@ function findUnusedVars(dir) { // A variable to handle success/failure message in this function let unusedVarsFound = false + // Array of all Sass files' content + const sassFiles = glob.sync(path.join(dir, '**/*.scss')) // String of all Sass files' content - const sassFiles = sh.cat(path.join(dir, '**/*.scss')) - // String of all Sass variables - const variables = sassFiles.grep(/^\$[a-zA-Z0-9_-][^:]*/g) - .replace(/(\$[a-zA-Z0-9_-][^:]*).*/g, '$1') - .trim() + let sassFilesString = '' + + sassFiles.forEach((file) => { + sassFilesString += fs.readFileSync(file, 'utf8') + }) - // Convert string into an array - const variablesArr = Array.from(variables.split('\n')) + // Array of all Sass variables + const variables = sassFilesString.match(/(^\$[a-zA-Z0-9_-]+[^:])/gm) - console.log(`There's a total of ${variablesArr.length} variables.`) + console.log(`There's a total of ${variables.length} variables.`) // Loop through each variable - variablesArr.forEach((variable) => { + variables.forEach((variable) => { const re = new RegExp(regExpQuote(variable), 'g') - const count = (sassFiles.match(re) || []).length + const count = (sassFilesString.match(re) || []).length if (count === 1) { console.log(`Variable "${variable}" is only used once!`) diff --git a/package-lock.json b/package-lock.json index 1e36304d14f1..28dbe1de48b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6934,15 +6934,6 @@ "duplexer": "0.1.1" } }, - "string_decoder": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", - "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", - "dev": true, - "requires": { - "safe-buffer": "5.1.1" - } - }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", @@ -6981,6 +6972,15 @@ "function-bind": "1.1.1" } }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "dev": true, + "requires": { + "safe-buffer": "5.1.1" + } + }, "stringstream": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz", diff --git a/package.json b/package.json index 85cffc5209e2..ae787dd86f6c 100644 --- a/package.json +++ b/package.json @@ -95,6 +95,7 @@ "cross-env": "^5.1.1", "eslint": "^4.11.0", "eslint-plugin-compat": "^2.1.0", + "glob": "^7.1.2", "htmllint-cli": "^0.0.6", "jsunitsaucelabs": "^1.3.0", "karma": "^1.7.1",