Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pehbehbeh committed Nov 17, 2016
2 parents e6ab7dc + 5aacb41 commit 85547e1
Show file tree
Hide file tree
Showing 7 changed files with 1,201 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/node_modules
/npm-debug.log
2 changes: 2 additions & 0 deletions bin/sass-lint-vue
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../lib/cli')(process.argv)
23 changes: 23 additions & 0 deletions lib/cli.js
@@ -0,0 +1,23 @@
#!/usr/bin/env node
const program = require('commander')
const packageDetails = require('./../package.json')
const Linter = require('./linter.js')

const run = (args) => {
program
.version(packageDetails.version)
.description(packageDetails.description)
.usage('[options] <paths ...>')
.arguments('<paths>')
.parse(process.argv)

if (!program.args.length) {
program.help()
}

const linter = new Linter()

linter.checkPaths(program.args)
}

module.exports = run
80 changes: 80 additions & 0 deletions lib/linter.js
@@ -0,0 +1,80 @@
const walk = require('walk')
const fs = require('fs')
const path = require('path')
const htmlparser = require('htmlparser')
const cheerio = require('cheerio')
const sassLint = require('sass-lint')
const Reporter = require('./reporter.js')

class Linter {
constructor (options) {
this.lintErrors = []
}

checkPaths (pathsToCheck) {
pathsToCheck.forEach((pathToCheck) => {
this.checkPath(pathToCheck)
})
}

checkPath (arg) {
const walker = walk.walk(arg, { followLinks: false })
walker.on('file', this.walkerFileHandler.bind(this))
walker.on('end', this.walkerEndHandler.bind(this))
}

walkerFileHandler (root, fileStat, next) {
const filename = `${root}/${fileStat.name}`

if (filename.substr(-3) !== 'vue') {
return next()
}

fs.readFile(path.resolve(root, fileStat.name), (error, fileData) => {
if (error) {
return console.log(error)
}

const fileTemplates = this.extractFileTemplates(fileData)

fileTemplates.forEach((template) => {
const fileErrors = sassLint.lintText({
text: template,
filename: filename,
format: 'scss'
})

if (fileErrors.messages.length) {
this.lintErrors = this.lintErrors.concat(fileErrors)
}
})

next()
})
}

walkerEndHandler () {
const reporter = new Reporter()
reporter.report(this.lintErrors)
}

extractFileTemplates (fileData) {
let templates = []

const handler = new htmlparser.DefaultHandler((error, dom) => {
if (error) {
return console.log(error)
}

const $ = cheerio.load(dom)
templates = templates.concat($('style[lang="sass"]').text())
templates = templates.concat($('style[lang="scss"]').text())
})

var parser = new htmlparser.Parser(handler)
parser.parseComplete(fileData)
return templates
}
}

module.exports = Linter
38 changes: 38 additions & 0 deletions lib/reporter.js
@@ -0,0 +1,38 @@
const table = require('text-table')
const chalk = require('chalk')

class Reporter {
report (results) {
let output = '\n'
let totalErrors = 0
let totalWarnings = 0

results.forEach((result) => {
totalErrors += result.errorCount
totalWarnings += result.warningCount

output += chalk.underline(result.filePath) + '\n'

output += table(result.messages.map((msg) => {
return [
'',
`${msg.line}:${msg.column}`,
(msg.severity === 2) ? chalk.red(msg.ruleId) : chalk.yellow(msg.ruleId),
msg.message
]
}))

output += '\n\n'
})

if (totalErrors > 0) {
output += chalk.red.bold(`\u2716 errors: ${totalErrors}`)
output += ' | '
output += chalk.yellow.bold(`warnings ${totalWarnings}`)
console.log(output)
process.exit(1)
}
}
}

module.exports = Reporter
37 changes: 37 additions & 0 deletions package.json
@@ -0,0 +1,37 @@
{
"name": "sass-lint-vue",
"version": "0.1.0",
"description": "Command line tool to lint Sass styles in Vue single file components.",
"keywords": [
"lint",
"linting",
"linter",
"sass",
"scss",
"vue"
],
"homepage": "https://github.com/sourceboat/sass-lint-vue",
"bugs": "https://github.com/sourceboat/sass-lint-vue/issues",
"author": "Sourceboat <info@sourceboat.com>",
"contributors": [
"Phil-Bastian Berndt <me@pbb.io> (https://pbb.io/)"
],
"license": "MIT",
"repository": {
"url": "https://github.com/sourceboat/sass-lint-vue",
"type": "git"
},
"main": "lib/linter.js",
"bin": {
"sass-lint-vue": "./bin/sass-lint-vue"
},
"dependencies": {
"chalk": "^1.1.3",
"cheerio": "^0.22.0",
"commander": "^2.9.0",
"htmlparser": "^1.7.7",
"sass-lint": "^1.10.2",
"text-table": "^0.2.0",
"walk": "^2.3.9"
}
}

0 comments on commit 85547e1

Please sign in to comment.