Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2239c13
commit 5fb9763
Showing
11 changed files
with
2,009 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module.exports = { | ||
env: { | ||
es6: true, | ||
node: true | ||
}, | ||
parserOptions: { | ||
ecmaVersion: 2017, | ||
sourceType: "module" | ||
}, | ||
extends: ["eslint:recommended", "plugin:node/recommended"], | ||
rules: { | ||
// overwritten Eslint rule recommeded rule | ||
"space-unary-ops": [ | ||
"error", | ||
{ | ||
words: true, | ||
nonwords: false | ||
} | ||
], | ||
camelcase: "off", | ||
"func-call-spacing": ["error", "never"], | ||
"eol-last": ["error", "always"], | ||
"no-console": "off", | ||
"no-alert": "error", | ||
eqeqeq: ["error", "always"], | ||
"no-eval": "error", | ||
"no-caller": "error", | ||
"no-undef": "error", | ||
"no-eq-null": "error", | ||
"no-useless-escape": "off", | ||
"no-extra-parens": "off", | ||
"no-trailing-spaces": "error", | ||
"no-multi-spaces": "error", | ||
"array-bracket-spacing": ["error", "never"], | ||
"object-curly-spacing": ["error", "always"], | ||
"no-multiple-empty-lines": ["error", { max: 1, maxEOF: 1 }], | ||
"func-call-spacing": ["error", "never"], | ||
"space-before-function-paren": ["error", "never"], | ||
semi: "error" | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env node | ||
const pkg = require("./package.json"); | ||
require("please-upgrade-node")(pkg); | ||
|
||
if (process.stdout.isTTY) { | ||
process.env.FORCE_COLOR = "1"; | ||
} | ||
|
||
(function() { | ||
const cgf = require("committed-git-files"); | ||
const Listr = require("listr"); | ||
const chalk = require("chalk"); | ||
|
||
const warning = chalk.keyword("orange"); | ||
const success = chalk.keyword("green"); | ||
const { log } = console; | ||
|
||
const { loadConfig } = require("./utils/loadConfig"); | ||
const resolveMainTask = require("./utils/resolveMainTask"); | ||
|
||
loadConfig() | ||
.then(({ config = [] }) => { | ||
// Fetching committed git files | ||
cgf((err, committedGitFiles = []) => { | ||
new Listr(resolveMainTask({ config, committedGitFiles }), { | ||
exitOnError: false, | ||
concurrent: true | ||
}) | ||
.run() | ||
.then(() => { | ||
log(success("Voila! 🎉 You code is ready to be Shipped.")); | ||
}) | ||
.catch(({ errors }) => { | ||
process.exitCode = 1; | ||
errors.forEach(err => { | ||
console.error(err.customErrorMessage); | ||
}); | ||
}); | ||
}); | ||
}) | ||
.catch(() => { | ||
warning("Loading Configuration⚙️ Failed!😑"); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"name": "lint-prepush", | ||
"version": "0.0.0", | ||
"description": "Lint committed files in a Branch", | ||
"bin": { | ||
"lint-prepush": "index.js" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"lint": "node_modules/.bin/eslint *.js", | ||
"format": "yarn lint --fix", | ||
"prepush": "lint-prepush" | ||
}, | ||
"lint-prepush": { | ||
"*.js": [ | ||
"eslint" | ||
] | ||
}, | ||
"keywords": [ | ||
"lint", | ||
"lint-prepush", | ||
"prepush", | ||
"husky" | ||
], | ||
"author": "“Theenadayalan” <“puduvai.theena@gmail.com”>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"eslint": "^5.3.0", | ||
"eslint-plugin-node": "^7.0.1" | ||
}, | ||
"dependencies": { | ||
"chalk": "^2.4.1", | ||
"committed-git-files": "0.0.1", | ||
"cosmiconfig": "^5.0.5", | ||
"dedent": "^0.7.0", | ||
"execa": "^0.10.0", | ||
"find-parent-dir": "^0.3.0", | ||
"husky": "^0.14.3", | ||
"listr": "^0.14.1", | ||
"log-symbols": "^2.2.0", | ||
"micromatch": "^3.1.10", | ||
"npm-which": "^3.0.1", | ||
"path-is-inside": "^1.0.2", | ||
"please-upgrade-node": "^3.1.1" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const execa = require("execa"); | ||
const dedent = require("dedent"); | ||
const symbols = require("log-symbols"); | ||
const npmWhich = require("npm-which")(process.cwd()); | ||
|
||
module.exports = function execTask({ command, fileList }) { | ||
let { executor, args } = resolveLinterPackage({ command, fileList }); | ||
return () => | ||
execa(executor, args, { reject: false }).then(result => { | ||
if (!result.failed) return `Passed ${command}`; | ||
throw constructErrorObject(command, result.stderr, result.stdout); | ||
}); | ||
}; | ||
|
||
function resolveLinterPackage({ command, fileList }) { | ||
let [executor, ...args] = command.split(" "); | ||
executor = npmWhich.sync(executor); | ||
args = args.concat(fileList); | ||
return { | ||
executor, | ||
args | ||
}; | ||
} | ||
|
||
function constructErrorObject(command, error, output) { | ||
const e = new Error(); | ||
e.customErrorMessage = dedent` | ||
${ | ||
symbols.error | ||
} "${command}" is having Errors😓. Please revisit them again😉 | ||
${output} | ||
${error} | ||
`; | ||
return e; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const cosmiconfig = require("cosmiconfig"); | ||
var findParentDir = require("find-parent-dir"); | ||
|
||
function loadConfig() { | ||
const explorer = cosmiconfig("lint-prepush", { | ||
searchPlaces: [ | ||
"package.json", | ||
"lint-prepush.config.js", | ||
".lintprepushrc", | ||
".lintprepushrc.js", | ||
".lintprepushrc.json", | ||
".lintprepushrc.yaml", | ||
".lintprepushrc.yml" | ||
] | ||
}); | ||
return explorer.search(); | ||
} | ||
|
||
function getGitDir() { | ||
return findParentDir.sync(process.cwd(), ".git"); | ||
} | ||
|
||
module.exports = { | ||
loadConfig, | ||
getGitDir | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const errMsg = err => (err.privateMsg !== null ? err.privateMsg : err.message); | ||
|
||
module.exports = function printErrors(errorInstance) { | ||
if (Array.isArray(errorInstance.errors)) { | ||
errorInstance.errors.forEach(lintError => { | ||
console.error(errMsg(lintError)); | ||
}); | ||
} else { | ||
console.error(errMsg(errorInstance)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const execTask = require("./execTask"); | ||
|
||
module.exports = function resolveLintTask(commandList, fileList, gitDir) { | ||
return commandList.map(command => ({ | ||
title: command, | ||
task: execTask({ | ||
command, | ||
fileList, | ||
gitDir | ||
}) | ||
})); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const Listr = require("listr"); | ||
const path = require("path"); | ||
const micromatch = require("micromatch"); | ||
const pathIsInside = require("path-is-inside"); | ||
|
||
const cwd = process.cwd(); | ||
const resolveLintTask = require("./resolveLintTask"); | ||
const { getGitDir } = require("./loadConfig"); | ||
const gitDir = getGitDir(); | ||
|
||
module.exports = function resolveMainTask(options) { | ||
return constructTaskList(options).map(task => ({ | ||
title: `Linting ${task.fileFormat} files`, | ||
task: () => | ||
new Listr(resolveLintTask(task.commandList, task.fileList, gitDir), { | ||
exitOnError: true, | ||
concurrent: false | ||
}), | ||
skip: () => { | ||
if (task.fileList.length === 0) { | ||
return `No files found with ${task.fileFormat}`; | ||
} | ||
return false; | ||
} | ||
})); | ||
}; | ||
|
||
function constructTaskList({ config, committedGitFiles }) { | ||
let filenames = committedGitFiles | ||
.map(file => file.filename) | ||
.map(file => path.resolve(gitDir, file)); | ||
return Object.keys(config).map(fileFormat => { | ||
let fileList = []; | ||
let commandList = config[fileFormat]; | ||
fileList = micromatch( | ||
filenames | ||
.filter(file => pathIsInside(file, cwd)) | ||
.map(file => path.relative(cwd, file)), | ||
[fileFormat], | ||
{ | ||
matchBase: true, | ||
dot: true | ||
} | ||
).map(file => path.resolve(cwd, file)); | ||
return { fileFormat, commandList, fileList }; | ||
}); | ||
} |
Oops, something went wrong.