Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add command to validate directly #41

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
"strict": [0, "global"],
"array-bracket-spacing": [2, "always"]
},
"ecmaFeatures": {
"modules": false
},
"env": {
"es6": true,
"node": true
Expand Down
2 changes: 1 addition & 1 deletion .fitcommitjsrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ validators:
emptyLines: 1
tags:
enabled: true
tags: fix, chore, feature, style, docs
tags: fix, chore, feat, test, docs
subjectPeriod:
enabled: false
capitalizedSubject:
Expand Down
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "stable"
sudo: false
Expand Down
15 changes: 15 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ Guidelines when adding new code:
* Ensure the entire test suite still passes by running `npm test`.
* Ensure that the code does not have lintin errors by running: `npm run lint`
* Install the commit validator by running: ./bin/fit-commit-js.js install

## Commands

### Running a validation manually
The command: `npm run fitcommitjs` is available.

To send arguments use: `--`. So, to run the plain validation:

`npm run fitcommitjs -- -c`

To run it verbose:

`npm run fitcommitjs -- -ddc`

Note: If you run this locally, to test the validation against a commit message, the validator will automatically take the last commit message.
2 changes: 1 addition & 1 deletion bin/fit-commit-js.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node --use-strict
#!/usr/bin/env node

const cli = require( '../lib/cli' );

Expand Down
25 changes: 21 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env node --use-strict
#!/usr/bin/env node

const pgk = require( '../package.json' );
const installer = require( './installer' );
const validate = require( './validate' );
const log = require( 'npmlog' );

/*
Expand All @@ -15,8 +16,9 @@ function version() {

function help() {
log.info( 'fit-commit-js', `v-${pgk.version}` );
log.info( 'fit-commit-js', 'Usage: fit-commit-js install || fit-commit-js -i' );
log.info( 'fit-commit-js', 'Usage: fit-commit-js uninstall || fit-commit-js -u' );
log.info( 'fit-commit-js', 'Usage: fit-commit-js install || fit-commit-js -i', 'Installs a precommit hook' );
log.info( 'fit-commit-js', 'Usage: fit-commit-js uninstall || fit-commit-js -u', 'Uninstalls a precommit hook' );
log.info( 'fit-commit-js', 'Usage: fit-commit-js validate || fit-commit-js -c', 'Runs the commit validation' );
process.exit( 0 );
}

Expand All @@ -28,13 +30,22 @@ function uninstall() {
installer.uninstall();
}

function validateFn() {
validate.run();
}

/*
Public
========================================================================== */

function execute() {
const action = process.argv[ 2 ];

if ( action.indexOf( '-dd' ) !== -1 ) {
log.info( 'Running in verbose mode' );
log.level = 'verbose';
}

switch ( action ) {
case '-h':
case 'help':
Expand All @@ -43,17 +54,23 @@ function execute() {
break;

case '-i':
case '-ddi':
case 'install':
install();

break;

case '-u':
case '-ddu':
case 'uninstall':
uninstall();

break;
case '-c':
case '-ddc':
case 'validate':
validateFn();

break;
case '-v':
case 'version':
version();
Expand Down
2 changes: 1 addition & 1 deletion lib/config/fileFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function getSupportedConfigFiles( entries, directory, supportedConfigs ) {
* @return {String} Path to the config file
*/
function findFileInDirectory( directory ) {
let currentDirectory = ( directory !== undefined ) ? directory : fileUtils.getCurrentDirectory();
let currentDirectory = directory || fileUtils.getCurrentDirectory();
let filePath;
let pathToConfigFile;
const filesInDirectory = fileUtils.getDirectoryFiles( currentDirectory );
Expand Down
72 changes: 2 additions & 70 deletions lib/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,16 @@

'use strict';

// Runs the actual validation of the message
// loads the config File
// loads the validator
// gets the commit message and parses it
// Validates the commit message against the validators

const R = require( 'ramda' );
const parser = require( './message/parser' );
const error = require( './message/error' );
const validatorLoader = require( './validatorLoader' );
const fileUtils = require( './config/fileUtils' );
const EXIT_CODE = {
SUCESS: 0,
ERROR: 1,
};
const validate = require( './validate' );

const isNotTestEnv = ( !process.env.NODE_ENV && process.env.NODE_ENV !== 'test' );

// The the process.argv array for a git commit, contains the next in its indexes
// 0: the path to node
// 1: the path to the commit-msg hook
// 2: the path to the actual commit message in the git repo (.git/COMMIT_EDITMSG)
function getCommitMessageFilePath() {
return process.argv[ 2 ];
}

/**
* Extracts the commit message and
* parses it into an array
* @return {String} The commit message in a string
*/
function extractCommitMessage() {
return fileUtils.readFile( getCommitMessageFilePath() );
}

const getParsedMessage = R.compose( parser.getRelevantLines, extractCommitMessage );

function runValidator( validator, messageArray ) {
if ( validator && R.propIs( Function, 'validate', validator ) ) {
validator.validate.call( this, messageArray );
}
}

function runValidators( directory ) {
const validators = validatorLoader.loadValidators( directory );
const parsedMessage = getParsedMessage();
error.clearErrorsMap();

// loadValidators return an array of objects. Iterate over it
for ( let i = 0; i < validators.length; i = i + 1 ) {
const validatorsObject = validators[ i ];
// Now iterate over the validator's properties to find
// the validate function and execute it
R.forEach( ( validator ) => {
runValidator( validatorsObject[ validator ], parsedMessage );
}, R.keys( validatorsObject ) );
}

if ( isNotTestEnv && error.hasErrors() ) {
error.printErrors();
process.exit( EXIT_CODE.ERROR );
}
}

module.exports = {
extractCommitMessage,
getCommitMessageFilePath,
getParsedMessage,
runValidators,
runValidator,
};

/*
This section is to be executed by the git hook
========================================================================== */

function execHook() {
runValidators();
validate.run();
}

if ( isNotTestEnv ) {
Expand Down
8 changes: 5 additions & 3 deletions lib/installer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const exec = require( 'child_process' ).exec;
const { exec } = require( 'child_process' );
const fs = require( 'fs' );
const log = require( 'npmlog' );
const fileUtils = require( './config/fileUtils' );
Expand Down Expand Up @@ -36,14 +36,16 @@ function deleteHook() {
}

function posthook() {
exec( `chmod +x ${HOOK_PATH}`,
exec(
`chmod +x ${HOOK_PATH}`,
( error, stdout, stderr ) => {
callbackError( stdout, stderr );
if ( error !== null ) {
log.info( 'fit-commit-js', 'Removing symlink' );
deleteHook();
}
}
},

);
log.verbose( `Installed hook in ${HOOK_PATH}` );
}
Expand Down
87 changes: 87 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env node

'use strict';

// Runs the actual validation of the message
// loads the config File
// loads the validator
// gets the commit message and parses it
// Validates the commit message against the validators

const R = require( 'ramda' );
const log = require( 'npmlog' );
const parser = require( './message/parser' );
const error = require( './message/error' );
const validatorLoader = require( './validatorLoader' );
const fileUtils = require( './config/fileUtils' );

const EXIT_CODE = {
SUCESS: 0,
ERROR: 1,
};

const isNotTestEnv = ( !process.env.NODE_ENV && process.env.NODE_ENV !== 'test' );

// The the process.argv array for a git commit, contains the next in its indexes
// 0: the path to node
// 1: the path to the commit-msg hook
// 2: the path to the actual commit message in the git repo (.git/COMMIT_EDITMSG)
function getCommitMessageFilePath() {
const terminalArgument = process.argv[ 2 ];
const isFilePath = (
terminalArgument !== '-ddc' &&
terminalArgument !== '-c' &&
terminalArgument !== 'validate' );
return isFilePath ? terminalArgument : '.git/COMMIT_EDITMSG';
}

/**
* Extracts the commit message and
* parses it into an array
* @return {String} The commit message in a string
*/
function extractCommitMessage() {
return fileUtils.readFile( getCommitMessageFilePath() );
}

const getParsedMessage = R.compose( parser.getRelevantLines, extractCommitMessage );

function runValidator( validator, messageArray ) {
if ( validator && R.propIs( Function, 'validate', validator ) ) {
validator.validate.call( this, messageArray );
}
}

function run( directory ) {
const validators = validatorLoader.loadValidators( directory );
const parsedMessage = getParsedMessage();
log.verbose( `Validating: ${parsedMessage}` );
error.clearErrorsMap();

// loadValidators return an array of objects. Iterate over it
for ( let i = 0; i < validators.length; i += 1 ) {
const validatorsObject = validators[ i ];
// Now iterate over the validator's properties to find
// the validate function and execute it
R.forEach( ( validator ) => {
runValidator( validatorsObject[ validator ], parsedMessage );
}, R.keys( validatorsObject ) );
}

if ( isNotTestEnv && error.hasErrors() ) {
error.printErrors();
process.exit( EXIT_CODE.ERROR );
}

if ( !error.hasErrors() ) {
process.exit( EXIT_CODE.SUCESS );
}
}

module.exports = {
extractCommitMessage,
getCommitMessageFilePath,
getParsedMessage,
run,
runValidator,
};
7 changes: 3 additions & 4 deletions lib/validatorLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const validatorsConfig = require( './config/validatorsConfig' );
const fileUtils = require( './config/fileUtils' );
const R = require( 'ramda' );
const path = require( 'path' );

const VALIDATORS_PATH = path.resolve( __dirname, './validators/' );

/*
Expand All @@ -18,9 +19,7 @@ const VALIDATORS_PATH = path.resolve( __dirname, './validators/' );
*/
function requireValidator( fileName ) {
const newValidator = {};
// eslint-disable global-require
newValidator[ fileName ] = require( path.join( VALIDATORS_PATH, fileName ) );
// eslint-enable global-require
newValidator[ fileName ] = require( path.join( VALIDATORS_PATH, fileName ) ); //eslint-disable-line
return newValidator;
}

Expand Down Expand Up @@ -49,7 +48,7 @@ function getValidatorFiles() {
function getEnabledValidatorsFiles( directory ) {
const validatorFiles = getValidatorFiles();
const enabledValidators = validatorsConfig.getEnabledValidatorsArray( directory );
const isEnabled = ( key ) => R.contains( key, enabledValidators );
const isEnabled = key => R.contains( key, enabledValidators );
const enabledValidatorsFiles = R.compose( R.values, R.pickBy( isEnabled ), getValidatorFiles );
return R.map( enabledValidatorsFiles, validatorFiles )[ 0 ];
}
Expand Down
6 changes: 4 additions & 2 deletions lib/validators/emptyLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ const getEmptyLines = utils.getDefinedValues;
*/
function validateLine( lineNo, lineText ) {
let errorMessage = '';
const emptyLines = getEmptyLines( constants.VALIDATOR_NAME,
constants.EMPTY_LINES_PROPERTY || constants.DEFAULT_VALUE );
const emptyLines = getEmptyLines(
constants.VALIDATOR_NAME,
constants.EMPTY_LINES_PROPERTY || constants.DEFAULT_VALUE,
);

for ( let i = 0; i < emptyLines.length; i += 1 ) {
const emptyLineNo = parseInt( emptyLines[ i ], 10 );
Expand Down
12 changes: 8 additions & 4 deletions lib/validators/lineLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ const constants = {
========================================================================== */

function getSubjectMaxLineLength() {
return base.getValidatorProperty( constants.VALIDATOR_NAME,
constants.SUBJECT_PROPERTY ) || constants.DEFAULT_VALUE;
return base.getValidatorProperty(
constants.VALIDATOR_NAME,
constants.SUBJECT_PROPERTY,
) || constants.DEFAULT_VALUE;
}

function getContentMaxLineLength() {
return base.getValidatorProperty( constants.VALIDATOR_NAME,
constants.LINE_PROPERTY ) || constants.DEFAULT_VALUE;
return base.getValidatorProperty(
constants.VALIDATOR_NAME,
constants.LINE_PROPERTY,
) || constants.DEFAULT_VALUE;
}

/**
Expand Down
Loading