-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- It was necessary to move the logic to other file. When require was execute in the test file, the command was triggered.
- Loading branch information
1 parent
b9c776b
commit a8c5164
Showing
3 changed files
with
109 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env node | ||
/** | ||
* How porcelain works | ||
* When a new file is created , it will add a ?? before the file | ||
* When a file is modified, it shows ' M'. Yes, one space and M (this has a reason) | ||
* When a file was modified and added to the staging area, and modified once again, it shows MM | ||
* (that's why there is an space for the modified files, to show the output aligned) | ||
*/ | ||
(function () { | ||
'use strict'; | ||
|
||
var childProcess = require('child_process'), | ||
inquirer = require('inquirer'), | ||
utils = require('../../bin/utils')(); | ||
|
||
function Add(){ | ||
|
||
this.init = function(){ | ||
|
||
var gitStatusPorcelain = childProcess.exec('git status --porcelain'); | ||
|
||
gitStatusPorcelain.stdout.on('data', function (data) { | ||
var files = this.getNoStagedFiles(data); | ||
|
||
if (!files) { | ||
utils.showError('No files to add'); | ||
return; | ||
} | ||
this.promptFileSelection(files); | ||
}.bind(this)); | ||
}; | ||
|
||
/** | ||
* returns string 'file1 file2 file2' | ||
* @param data | ||
*/ | ||
this.getNoStagedFiles = function(data) { | ||
var newFiles = data.match(this.newFilesRegex(data)) || [], //with ?? and M string | ||
formattedArray = []; | ||
|
||
newFiles.forEach(function (item, index) { | ||
item = item.replace(/^ M/, ''); | ||
item = item.replace(/^MM/, ''); | ||
item = item.replace(/\?\?./, ''); | ||
formattedArray.push(item); | ||
}); | ||
return formattedArray; | ||
}; | ||
|
||
this.newFilesRegex = function () { | ||
return new RegExp('(\\?\\?.*)|(^( M|MM).*)', 'mg'); | ||
}; | ||
|
||
this.promptFileSelection = function (files) { | ||
inquirer.prompt([ | ||
{ | ||
type: 'checkbox', | ||
message: 'Select files to add', | ||
name: 'files', | ||
choices: files | ||
} | ||
]).then(function (selection) { | ||
var command = 'git add ' + selection.files.join(' '), | ||
gitAdd = childProcess.exec(command); | ||
|
||
gitAdd.stdout.on('data', function (data) { | ||
console.log(data); | ||
}); | ||
|
||
gitAdd.stderr.on('err', function (err) { | ||
console.log(err); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
module.exports = Add; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,9 @@ | ||
#!/usr/bin/env node | ||
/** | ||
* How porcelain works | ||
* When a new file is created , it will add a ?? before the file | ||
* When a file is modified, it shows ' M'. Yes, one space and M (this has a reason) | ||
* When a file was modified and added to the staging area, and modified once again, it shows MM | ||
* (that's why there is an space for the modified files, to show the output aligned) | ||
*/ | ||
(function () { | ||
(function(){ | ||
'use strict'; | ||
|
||
var childProcess = require('child_process'), | ||
inquirer = require('inquirer'), | ||
utils = require('../bin/utils')(); | ||
|
||
|
||
init(); | ||
|
||
function init() { | ||
var gitStatusPorcelain = childProcess.exec('git status --porcelain'); | ||
|
||
gitStatusPorcelain.stdout.on('data', function (data) { | ||
var files = getNoStagedFiles(data); | ||
|
||
if (!files) { | ||
utils.showError('No files to add'); | ||
return; | ||
} | ||
promptFileSelection(files); | ||
}); | ||
} | ||
|
||
/** | ||
* returns string 'file1 file2 file2' | ||
* @param data | ||
*/ | ||
function getNoStagedFiles(data) { | ||
var newFiles = data.match(newFilesRegex(data)) || [], //with ?? and M string | ||
formattedArray = []; | ||
|
||
newFiles.forEach(function (item, index) { | ||
item = item.replace(/^ M/, ''); | ||
item = item.replace(/^MM/, ''); | ||
item = item.replace(/\?\?./, ''); | ||
formattedArray.push(item); | ||
}); | ||
return formattedArray; | ||
} | ||
|
||
function newFilesRegex() { | ||
return new RegExp('(\\?\\?.*)|(^( M|MM).*)', 'mg'); | ||
} | ||
|
||
function promptFileSelection(files) { | ||
inquirer.prompt([ | ||
{ | ||
type: 'checkbox', | ||
message: 'Select files to add', | ||
name: 'files', | ||
choices: files | ||
} | ||
]).then(function (selection) { | ||
var command = 'git add ' + selection.files.join(' '), | ||
gitAdd = childProcess.exec(command); | ||
|
||
gitAdd.stdout.on('data', function (data) { | ||
console.log(data); | ||
}); | ||
|
||
gitAdd.stderr.on('err', function (err) { | ||
console.log(err); | ||
}); | ||
}); | ||
} | ||
|
||
var Add = require('./commands/add'); | ||
var addCommand = new Add(); | ||
addCommand.init(); | ||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env node | ||
var consoleMock = require('console-mock'), | ||
consoleTest = consoleMock.create(), | ||
turboAdd = require('../lib/turbo-add'), | ||
childProcess = require('child_process'); | ||
|
||
describe('turbo-add', function(){ | ||
'use strict'; | ||
|
||
it('init', function(){ | ||
var gitPorcelainOutput = '?? file1.js \n?? file2.js'; | ||
var getNoStagedFilesValue = ['file1.js','file2.js']; | ||
|
||
spyOn(childProcess, "exec").andReturn(gitPorcelainOutput); | ||
|
||
spyOn("getNoStagedFiles").andReturn(getNoStagedFilesValue); | ||
|
||
turboAdd.init(); | ||
|
||
spyOn('promptFileSelection').toHaveBeenCalledWith(getNoStagedFilesValue); | ||
}); | ||
|
||
}); | ||
|