diff --git a/index.js b/index.js index 274ccea..2959cb9 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ var gutil = require('gulp-util'); var through = require('through2'); var execSync = require('child_process').execSync; +var path = require('path'); function diffBranches(options) { var filesChanged = []; @@ -11,13 +12,19 @@ function diffBranches(options) { } var cmd = 'git diff --name-only $(git merge-base ' + options.baseBranch + ' HEAD)..HEAD'; - filesChanged = execSync(cmd, {encoding: 'utf8'}); - filesChanged = filesChanged.split("\n"); + if (process.platform === 'win32') { + cmd = 'powershell -NoProfile ' + cmd; + } + const executeResult = execSync(cmd, {encoding: 'utf8'}); + if (executeResult) { + filesChanged = executeResult.split("\n").map(filePath => path.resolve(filePath)); + } + // last entry is just empty string filesChanged.pop(); return through.obj( - function (file, enc, cb) { + function (file, _enc, cb) { if (isFileChanged(file, filesChanged)) { this.push(file); } @@ -27,8 +34,7 @@ function diffBranches(options) { }; function isFileChanged(file, filesChanged) { - var currentFile = file.path.substr(process.cwd().length + 1); - if (filesChanged.indexOf(currentFile) != -1) { + if (filesChanged.indexOf(file.path) != -1) { return true; } else { return false; diff --git a/package.json b/package.json index 1e441cb..f3be3a6 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "test": "./node_modules/mocha/bin/mocha" }, "author": { - "name" : "Alexander Myshov", - "url": "https://github.com/myshov" + "name": "Alexander Myshov", + "url": "https://github.com/myshov" }, "repository": { "type": "git", @@ -27,6 +27,8 @@ "node": ">=0.12.0" }, "keywords": [ - "gulpplugin", "git", "filtering" + "gulpplugin", + "git", + "filtering" ] } diff --git a/test/isFileChanged.js b/test/isFileChanged.js index 2e81c63..902c6f3 100644 --- a/test/isFileChanged.js +++ b/test/isFileChanged.js @@ -1,28 +1,19 @@ var assert = require('assert'); var rewire = require('rewire'); +var path = require('path'); var gulpGitFlowDiff = rewire('../'); -gulpGitFlowDiff.__set__({ - process: { - cwd: function() { - return 'https://github.com/home/user/dev' - } - } -}); describe('gulp-giflow-diff isFileChanged', function() { - process.cwd = function() { - return 'https://github.com/home/user/dev' - }; - + const basePath = process.cwd(); it('should return true if there is file in list of changed files', function(done) { var fileMocked = {}; - fileMocked.path = 'https://github.com/home/user/dev/src/file33.js'; + fileMocked.path = path.join(basePath, 'src/file33.js'); var filesChanged = [ 'src/file11.js', 'src/file22.js', 'src/file33.js' - ]; + ].map(filePath => path.join(basePath, filePath)); var result = gulpGitFlowDiff.isFileChanged(fileMocked, filesChanged); assert.equal(result, true); done(); @@ -30,12 +21,12 @@ describe('gulp-giflow-diff isFileChanged', function() { it('should return false if there is no file in list of changed files', function(done) { var fileMocked = {}; - fileMocked.path = 'https://github.com/usr/home/user/dev/src/file44.js'; + fileMocked.path = path.join(basePath, 'src/file44.js'); var filesChanged = [ 'src/file11.js', 'src/file22.js', 'src/file33.js' - ]; + ].map(filePath => path.join(basePath, filePath)); var result = gulpGitFlowDiff.isFileChanged(fileMocked, filesChanged); assert.equal(result, false); done();