Skip to content

Commit

Permalink
Merge pull request #2 from theenadayalank/feature_branch_config
Browse files Browse the repository at this point in the history
[Feature] Configure Base Branch
  • Loading branch information
theenadayalank authored Oct 6, 2018
2 parents f459a0e + 7117dec commit bb45690
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ Configure the following scripts in package.json to lint your committed files
+ "prepush": "lint-prepush"
},
+ "lint-prepush": {
+ "*.js": [
+ "eslint"
+ ]
+ "base": "master",
+ "tasks": {
+ "*.js": [
+ "eslint"
+ ]
+ }
+ }
}
```
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (process.stdout.isTTY) {
(function() {
const Listr = require("listr");
const chalk = require("chalk");
const debug = require("debug")("index");
const debug = require("debug")("lint-prepush:index");

const success = chalk.keyword("green");
const error = chalk.keyword("red");
Expand All @@ -20,11 +20,13 @@ if (process.stdout.isTTY) {
const fetchGitDiff = require("./utils/fetchGitDiff");

loadConfig()
.then(({ config = [] }) => {
.then(({ config = {} } = {}) => {
let { base : baseBranch = 'master', tasks = {} } = config;
debug('Base Branch:' + baseBranch);
// Fetching committed git files
fetchGitDiff().then((committedGitFiles = []) => {
fetchGitDiff( baseBranch ).then((committedGitFiles = []) => {
debug(committedGitFiles);
new Listr(resolveMainTask({ config, committedGitFiles }), {
new Listr(resolveMainTask({ tasks, committedGitFiles }), {
exitOnError: false,
concurrent: true
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lint-prepush",
"version": "0.0.2",
"version": "0.1.0",
"description": "Run linters on committed files in a Branch🔬",
"author": "“Theenadayalan” <“puduvai.theena@gmail.com”>",
"license": "MIT",
Expand Down
17 changes: 7 additions & 10 deletions utils/fetchGitDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@ const warning = chalk.keyword("orange");
const { log } = console;
const { spawnChildProcess } = require("./common");

module.exports = function fetchGitDiff(origin = "master") {
// git command to push out the changed file names between current branch and master (Exclude delelted files which cannot be fetched now)
let command = `git diff --name-only --diff-filter=d ${origin} HEAD`;
module.exports = function fetchGitDiff( baseBranch = "master" ) {
// git command to pull out the changed file names between current branch and master (Exclude delelted files which cannot be fetched now)
let command = `git diff --name-only --diff-filter=d ${baseBranch}...HEAD`;

return new Promise(resolve => {
spawnChildProcess({ command }, ({ hasErrors, output }) => {
spawnChildProcess({ command }, ({ hasErrors = false, output = {} }) => {
let fileList = [];
if (hasErrors) {
log(
warning(
`Fetching GIT diff process closed with errors: \n ${output.stderr}`
`Fetching commited files process closed with errors: \n ${output.stderr}`
)
);
resolve(fileList);
} else {
output.stdout.split("\n").forEach(filename => {
if (filename) {
fileList.push(filename);
}
filename ? fileList.push(filename) : null;
});
resolve(fileList);
}
resolve(fileList);
});
});
};
8 changes: 4 additions & 4 deletions utils/resolveMainTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const micromatch = require("micromatch");
const cwd = process.cwd();
const resolveLintTask = require("./resolveLintTask");

module.exports = function resolveMainTask(options) {
module.exports = function resolveMainTask( options = {} ) {
return constructTaskList(options).map(task => ({
title: `Linting ${task.fileFormat} files`,
task: () =>
Expand All @@ -22,10 +22,10 @@ module.exports = function resolveMainTask(options) {
}));
};

function constructTaskList({ config, committedGitFiles }) {
return Object.keys(config).map(fileFormat => {
function constructTaskList({ tasks = {}, committedGitFiles = [] } = {}) {
return Object.keys(tasks).map(fileFormat => {
let fileList = [];
let commandList = config[fileFormat];
let commandList = tasks[fileFormat];
fileList = micromatch(committedGitFiles, [fileFormat], {
matchBase: true,
dot: true
Expand Down

0 comments on commit bb45690

Please sign in to comment.