Skip to content

Commit

Permalink
Pure Node.js approach.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Nov 26, 2017
1 parent 97490cf commit 9a9824b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
29 changes: 15 additions & 14 deletions build/lint-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@

'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
RegExp.quote = (string) => string.replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&')

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)
}
Expand All @@ -31,22 +30,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(RegExp.quote(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!`)
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9a9824b

Please sign in to comment.