Skip to content

Commit

Permalink
Merge pull request #15 from theenadayalank/feature_skip_rerunning_tasks
Browse files Browse the repository at this point in the history
[FEATURE] Cache successful linted commit hash
  • Loading branch information
theenadayalank authored Mar 1, 2019
2 parents a7e1e5d + f5f6713 commit 9490f4d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 65 deletions.
81 changes: 50 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,81 @@ if (process.stdout.isTTY) {
const Listr = require("listr");
const chalk = require("chalk");
const debug = require("debug")("lint-prepush:index");
const os = require('os');
const Cache = require("file-system-cache").default;

const cache = Cache({
basePath: `${os.homedir()}/.lint-prepush`, //Path where cache files are stored.
ns: process.cwd() //A grouping namespace for items.
});

const success = chalk.keyword("green");
const error = chalk.keyword("red");
const warning = chalk.keyword("yellow");

const { log } = console;

const { loadConfig, spawnChildProcess } = require("./utils/common");
const { loadConfig, execChildProcess } = require("./utils/common");
const resolveMainTask = require("./utils/resolveMainTask");
const fetchGitDiff = require("./utils/fetchGitDiff");

loadConfig()
.then(({ config = {} } = {}) => {

let { base : baseBranch = 'master', tasks = {} } = config;
debug('Base Branch:' + baseBranch);

// Skip linter for base branch
let getCurrentBranchCommand = 'git rev-parse --abbrev-ref HEAD';
spawnChildProcess({ command : getCurrentBranchCommand }, ({ hasErrors = false, output = '' }) => {
let currentBranch = (output.split('\n') || [])[0];
if (hasErrors) {
log(error('\nCould not get the current Branch.\n'));
process.exitCode = 1;
return;
}
execChildProcess({ command: getCurrentBranchCommand })
.then(currentBranch => {

if(currentBranch === baseBranch) {
log(warning("\nNOTE: Skipping the Lintners since you are in the base branch\n"));
return;
}
if(currentBranch === baseBranch) {
log(warning("\nNOTE: Skipping the Lintners since you are in the base branch\n"));
return;
}

// Fetching committed git files
fetchGitDiff( baseBranch ).then((committedGitFiles = []) => {
debug(committedGitFiles);
new Listr(resolveMainTask({ tasks, committedGitFiles }), {
exitOnError: false,
concurrent: true
})
.run()
.then(() => {
log(success("\nVoila! 🎉 Code is ready to be Shipped.\n"));
})
.catch(({ errors }) => {
process.exitCode = 1;
errors.forEach(err => {
console.error(err.customErrorMessage);
execChildProcess({ command: 'git rev-parse HEAD' })
.then((commitHash = '') => {
let cachedCommitHash = cache.getSync("linted-hash") || "";

if(commitHash === cachedCommitHash) {
log(warning("\nNOTE: Skipping the Lintners since the commits are linted already.\n"));
return;
}

// Fetching committed git files
fetchGitDiff( baseBranch ).then((committedGitFiles = []) => {
debug(committedGitFiles);
new Listr(resolveMainTask({ tasks, committedGitFiles }), {
exitOnError: false,
concurrent: true
})
.run()
.then(() => {
cache.setSync("linted-hash", commitHash);
log(success("\nVoila! 🎉 Code is ready to be Shipped.\n"));
})
.catch(({ errors }) => {
process.exitCode = 1;
errors.forEach(err => {
console.error(err.customErrorMessage);
});
});
})
.catch((message = '') => {
process.exitCode = 1;
log(warning(message));
});
});
})
.catch((message = '') => {
.catch(() => {
log(error('\nCould not get the current Branch.\n'));
process.exitCode = 1;
log(warning(message));
return;
});

});
})

.catch(() => {
process.exitCode = 1;
log(error("Loading Configuration⚙️ Failed!😑"));
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lint-prepush",
"version": "0.2.1",
"version": "0.3.0",
"description": "Run linters on committed files in a Branch🔬",
"author": "“Theenadayalan” <“puduvai.theena@gmail.com”>",
"license": "MIT",
Expand Down Expand Up @@ -31,6 +31,7 @@
"debug": "^3.1.0",
"dedent": "^0.7.0",
"execa": "^0.10.0",
"file-system-cache": "^1.0.5",
"listr": "^0.14.1",
"log-symbols": "^2.2.0",
"micromatch": "^3.1.10",
Expand Down
35 changes: 12 additions & 23 deletions utils/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const cosmiconfig = require("cosmiconfig");
const { spawn } = require("child_process");
const { exec } = require("child_process");

function loadConfig() {
const explorer = cosmiconfig("lint-prepush", {
Expand All @@ -16,32 +16,21 @@ function loadConfig() {
return explorer.search();
}

function spawnChildProcess({ command = "", params = [] }, callback) {
let [bin, ...args] = command.split(" ");
args = args.concat(params);
let executor = spawn(bin, args);

let output = "",
error = "";

executor.stdout.on("data", data => {
output += data.toString();
});

executor.stderr.on("data", data => {
error += data.toString();
});

executor.on("close", code => {
let hasErrors = code !== 0;
callback({
hasErrors,
output: (hasErrors ? error : output)
function execChildProcess({ command = "" } = {}) {
return new Promise( (resolve, reject) => {
exec(command , (err, stdout, stderr) => {
if (err) {
reject(stderr);
}
let result = (stdout.toString() || "").split('\n');
result = result.slice(0,-1);
result = result.join('\n');
resolve(result);
});
});
}

module.exports = {
loadConfig,
spawnChildProcess
execChildProcess
};
18 changes: 8 additions & 10 deletions utils/fetchGitDiff.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
const { spawnChildProcess } = require("./common");
const { execChildProcess } = require("./common");

module.exports = function fetchGitDiff( baseBranch = "master" ) {

// git command to pull out the changed file names between current branch and base branch (Excluded delelted files which cannot be fetched now)
let command = `git diff --relative --name-only --diff-filter=d ${baseBranch}...HEAD`;

return new Promise( (resolve, reject) => {
spawnChildProcess({ command }, ({ hasErrors = false, output = '' }) => {
let fileList = [];
if (hasErrors) {
reject(`\n Fetching committed file list process has been stopped with the following error: \n ${output}`);
}
output.split("\n").forEach(filename => {
filename ? fileList.push(filename) : null;
return execChildProcess({ command })
.then((result = '') => {
let fileList = result.split('\n');
resolve(fileList);
})
.catch((err) => {
reject(`\n Fetching committed file list process has been stopped with the following error: \n ${err}`);
});
resolve(fileList);
});
});
};
49 changes: 49 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ base@^0.11.1:
mixin-deep "^1.2.0"
pascalcase "^0.1.1"

bluebird@^3.3.5:
version "3.5.3"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.3.tgz#7d01c6f9616c9a51ab0f8c549a79dfe6ec33efa7"
integrity sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -603,6 +608,15 @@ file-entry-cache@^2.0.0:
flat-cache "^1.2.1"
object-assign "^4.0.1"

file-system-cache@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/file-system-cache/-/file-system-cache-1.0.5.tgz#84259b36a2bbb8d3d6eb1021d3132ffe64cfff4f"
integrity sha1-hCWbNqK7uNPW6xAh0xMv/mTP/08=
dependencies:
bluebird "^3.3.5"
fs-extra "^0.30.0"
ramda "^0.21.0"

fill-range@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-4.0.0.tgz#d544811d428f98eb06a63dc402d2403c328c38f7"
Expand Down Expand Up @@ -635,6 +649,17 @@ fragment-cache@^0.2.1:
dependencies:
map-cache "^0.2.2"

fs-extra@^0.30.0:
version "0.30.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-0.30.0.tgz#f233ffcc08d4da7d432daa449776989db1df93f0"
integrity sha1-8jP/zAjU2n1DLapEl3aYnbHfk/A=
dependencies:
graceful-fs "^4.1.2"
jsonfile "^2.1.0"
klaw "^1.0.0"
path-is-absolute "^1.0.0"
rimraf "^2.2.8"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -685,6 +710,11 @@ graceful-fs@^4.1.2:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"

graceful-fs@^4.1.6, graceful-fs@^4.1.9:
version "4.1.15"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==

has-ansi@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91"
Expand Down Expand Up @@ -988,6 +1018,13 @@ json-stable-stringify-without-jsonify@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"

jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
integrity sha1-NzaitCi4e72gzIO1P6PWM6NcKug=
optionalDependencies:
graceful-fs "^4.1.6"

kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
Expand All @@ -1008,6 +1045,13 @@ kind-of@^6.0.0, kind-of@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.2.tgz#01146b36a6218e64e58f3a8d66de5d7fc6f6d051"

klaw@^1.0.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/klaw/-/klaw-1.3.1.tgz#4088433b46b3b1ba259d78785d8e96f73ba02439"
integrity sha1-QIhDO0azsbolnXh4XY6W9zugJDk=
optionalDependencies:
graceful-fs "^4.1.9"

levn@^0.3.0, levn@~0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/levn/-/levn-0.3.0.tgz#3b09924edf9f083c0490fdd4c0bc4421e04764ee"
Expand Down Expand Up @@ -1338,6 +1382,11 @@ punycode@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"

ramda@^0.21.0:
version "0.21.0"
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.21.0.tgz#a001abedb3ff61077d4ff1d577d44de77e8d0a35"
integrity sha1-oAGr7bP/YQd9T/HVd9RN536NCjU=

regex-not@^1.0.0, regex-not@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
Expand Down

0 comments on commit 9490f4d

Please sign in to comment.