Skip to content

Commit

Permalink
feat: create postinstall script to set symlink
Browse files Browse the repository at this point in the history
this will create symlink 2 levels above the node location.
it will create cz-config file in your porject root
and the symlink inside this node package

closes #1
  • Loading branch information
leonardoanalista committed Nov 14, 2015
1 parent 54480ca commit f5b8b14
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@

var wrap = require('./node_modules/word-wrap/index');

var SYMLINK_CONFIG_NAME = 'cz-config';


function logger(arguments) {
console.info(arguments);
}

/* istanbul ignore next */
function readConfigFile(){
// this function is replaced in test.
var config;
try {
// Yry to find a customized version for the project
// Try to find a customized version for the project
// This file is a symlink to the real one usually placed in the root of your project.
config = require('./.cz-config');
config = require('./' + SYMLINK_CONFIG_NAME);
} catch (err) {
logger('You don\'t have a file "' + SYMLINK_CONFIG_NAME + '" in your porject root directory. We will use the default configuration file inside this directory: ' + __dirname);
logger('\n\nYou should run "npm run postinstall" to fix it up.');
config = require('./cz-config-EXAMPLE');
}
return config;
}
var config = readConfigFile();


function buildCommit(answers) {
var maxLineWidth = 100;
Expand Down Expand Up @@ -61,15 +71,12 @@ var isNotWip = function(answers) {
return answers.type.toLowerCase() !== 'wip';
}

var logger = function(arguments) {
console.info(arguments);
}

module.exports = {

prompter: function(cz, commit) {
var config = readConfigFile();

// console.info('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');
logger('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');

cz.prompt([
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"test:check-coverage": "istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
"test:watch": "node_modules/jasmine-node/bin/jasmine-node --color --autotest spec/ --watch .",
"report-coverage": "cat ./coverage/lcov.info | codecov",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"postinstall": "node ./postinstall.js"
},
"homepage": "https://github.com/leonardoanalista/cz-customizable",
"repository": {
Expand Down
27 changes: 27 additions & 0 deletions postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

var path = require('path');
var fs = require('fs');

var SYM_LINK_LOCATION = './cz-config';
var CZ_CONFIG_NAME = '.cz-config';
var CZ_CONFIG_EXAMPLE_LOCATION = './cz-config-EXAMPLE.js';



fs.stat(path.resolve('./../../' + CZ_CONFIG_NAME), function(err, stats){
if (err) {
console.info('>>> config file doesn\'t exist. I will create one for you.');
fs.writeFileSync(path.resolve('./../../' + CZ_CONFIG_NAME), fs.readFileSync(path.resolve(CZ_CONFIG_EXAMPLE_LOCATION)));
} else {
console.info('>>> file ' + CZ_CONFIG_NAME + ' already exist in your project root. We will NOT override it.');
}

});

//first delete any existing symbolic link.
fs.unlink(SYM_LINK_LOCATION, function(err){

// create or re-create symlink to file located 2 dirs up.
console.info('>>> cz-customizable is about to create this symlink "' + __dirname + '/.cz-config" to point to your project root directory, 2 levels up.');
fs.symlinkSync(path.resolve('./../../' + CZ_CONFIG_NAME), path.resolve(SYM_LINK_LOCATION), 'file')
});
31 changes: 23 additions & 8 deletions spec/czCustomizableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@
describe('cz-customizable', function(){

var module, cz, commit;

var rewire = require('rewire');

beforeEach(function(){
module = rewire('../index.js');

module.__set__({
console: {
info: function(){}
},
'config': {
types: [{value: 'feat', name: 'feat: my feat'}],
scopes: [{name: 'myScope'}],
scopeOverrides: {
fix: [{name: 'fixOverride'}]

// logger: function(){console.info('>>> zzzzzzzzzzzzzzzzzzzzzz')},
readConfigFile: function() {
return {
types: [{value: 'feat', name: 'feat: my feat'}],
scopes: [{name: 'myScope'}],
scopeOverrides: {
fix: [{name: 'fixOverride'}]
}
}
}
},

// 'require': function (a){
// console.info('>>> RRRRRRRRRRRR: ', a);
// },

// 'config': {
// types: [{value: 'feat', name: 'feat: my feat'}],
// scopes: [{name: 'myScope'}],
// scopeOverrides: {
// fix: [{name: 'fixOverride'}]
// }
// }

});

Expand Down Expand Up @@ -124,7 +140,6 @@ describe('cz-customizable', function(){
var answers = {
confirmCommit: true,
type: 'WIP',
// scope: 'myScope',
subject: 'this is my worl-in-progress'
};

Expand Down

0 comments on commit f5b8b14

Please sign in to comment.