Navigation Menu

Skip to content

Commit

Permalink
Skeleton Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Theenadayalan K authored and theenadayalank committed Aug 8, 2018
1 parent 2239c13 commit 5fb9763
Show file tree
Hide file tree
Showing 11 changed files with 2,009 additions and 51 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
/node_modules
41 changes: 41 additions & 0 deletions .eslintrc.js
@@ -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"
}
};
55 changes: 4 additions & 51 deletions .gitignore
Expand Up @@ -4,58 +4,11 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
package-lock.json

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
bower_components

# next.js build output
.next
# Editor Configurations
.vscode
44 changes: 44 additions & 0 deletions index.js
@@ -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!😑");
});
})();
49 changes: 49 additions & 0 deletions package.json
@@ -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"
}
}
35 changes: 35 additions & 0 deletions utils/execTask.js
@@ -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;
}
26 changes: 26 additions & 0 deletions utils/loadConfig.js
@@ -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
};
11 changes: 11 additions & 0 deletions utils/printErrors.js
@@ -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));
}
};
12 changes: 12 additions & 0 deletions utils/resolveLintTask.js
@@ -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
})
}));
};
47 changes: 47 additions & 0 deletions utils/resolveMainTask.js
@@ -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 };
});
}

0 comments on commit 5fb9763

Please sign in to comment.