All files / lib/setup utils.js

100% Statements 25/25
100% Branches 10/10
100% Functions 3/3
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57    3x 3x   3x   3x 11x 11x 44x 8x     36x     3x     3x 11x   11x                     8x     3x       3x 10x 10x 1x 1x     9x 9x 3x 3x     6x    
'use strict';
 
const fs = require('fs');
const path = require('path');
 
const debug = require('debug')('gint:setup');
 
const findParent = (directory, name) => {
  let dir = directory;
  while (dir !== '/') {
    if (fs.existsSync(path.join(dir, name))) {
      return path.resolve(dir, name);
    }
 
    dir = path.dirname(dir);
  }
 
  return null;
};
 
exports.findHooksDir = (dirname) => {
  const dir = findParent(dirname, '.git');
 
  if (dir) {
    // const gitDir = path.join(dir, '.git');
    // const stats = fs.lstatSync(gitDir);
 
    // if (stats.isFile()) {
    //   // Expect following format
    //   // git: pathToGit
    //   // On Windows pathToGit can contain ':' (example "gitdir: C:/Some/Path")
    //   const gitFileData = fs.readFileSync(gitDir, 'utf-8');
    //   gitDir = gitFileData.split(':').slice(1).join(':').trim();
    // }
    return path.resolve(dir, 'hooks');
  }
 
  return null;
};
 
 
exports.isSetupRequired = (gintDir) => {
  const isInSubNodeModule = (gintDir.match(/node_modules/g) || []).length > 1;
  if (isInSubNodeModule) {
    debug('Trying to install from sub \'node_module\' directory, skipping');
    return false;
  }
 
  const isSelf = !/node_modules/.test(gintDir);
  if (isSelf) {
    debug('Trying to install for itself, skipping');
    return false;
  }
 
  return true;
};