Skip to content

Commit

Permalink
Merge pull request #44 from cksource/t/43
Browse files Browse the repository at this point in the history
Fix: Fixed various minor issues with the commands. Introduced missing tests. Closes #31. Closes #41. Closes #3. Closes #43.
  • Loading branch information
Kamil Piechaczek committed Feb 13, 2017
2 parents e3e862e + f271fb0 commit 5751eb7
Show file tree
Hide file tree
Showing 13 changed files with 784 additions and 35 deletions.
30 changes: 17 additions & 13 deletions lib/commands/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ const exec = require( '../utils/exec' );
module.exports = {
/**
* @param {Object} data
* @param {Object} data.parameters Additional arguments provided by the user.
* @param {String} data.packageName Name of current package to process.
* @param {Options} data.options The options object.
* @param {Repository|null} data.repository
* @param {Repository} data.repository
* @returns {Promise}
*/
execute( data ) {
Expand All @@ -25,19 +24,24 @@ module.exports = {
return new Promise( ( resolve, reject ) => {
const destinationPath = path.join( data.options.packages, data.repository.directory );

let promise;

// Package is already cloned.
if ( fs.existsSync( destinationPath ) ) {
log.info( `Package "${ data.packageName }" is already cloned. Skipping.` );
log.info( `Package "${ data.packageName }" is already cloned.` );

return resolve( { logs: log.all() } );
}
promise = Promise.resolve();
} else {
const command = [
`git clone --progress ${ data.repository.url } ${ destinationPath }`,
`cd ${ destinationPath }`,
`git checkout --quiet ${ data.repository.branch }`
].join( ' && ' );

const command =
`git clone --progress ${ data.repository.url } ${ destinationPath } && ` +
`cd ${ destinationPath } && ` +
`git checkout --quiet ${ data.repository.branch }`;
promise = exec( command );
}

exec( command )
promise
.then( ( output ) => {
log.info( output );

Expand All @@ -47,14 +51,14 @@ module.exports = {

if ( data.options.recursive ) {
const packageJson = require( path.join( destinationPath, 'package.json' ) );
let packages = [];
const packages = [];

if ( packageJson.dependencies ) {
packages = packages.concat( Object.keys( packageJson.dependencies ) );
packages.push( ...Object.keys( packageJson.dependencies ) );
}

if ( packageJson.devDependencies ) {
packages = packages.concat( Object.keys( packageJson.devDependencies ) );
packages.push( ...Object.keys( packageJson.devDependencies ) );
}

commandOutput.packages = packages;
Expand Down
12 changes: 6 additions & 6 deletions lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ const exec = require( '../utils/exec' );

module.exports = {
/**
* @param {Array.<String>} parameters Arguments that user provided calling the mgit.
* @param {Array.<String>} args Arguments that user provided calling the mgit.
*/
beforeExecute( parameters ) {
if ( parameters.length === 1 ) {
beforeExecute( args ) {
if ( args.length === 1 ) {
throw new Error( 'Missing command to execute. Use: mgit exec [command-to-execute].' );
}
},

/**
* @param {Object} data
* @param {Object} data.parameters Additional arguments provided by the user.
* @param {Object} data.arguments Arguments that user provided calling the mgit.
* @param {String} data.packageName Name of current package to process.
* @param {Options} data.options The options object.
* @param {Repository|null} data.repository
* @param {Repository} data.repository
* @returns {Promise}
*/
execute( data ) {
Expand All @@ -42,7 +42,7 @@ module.exports = {

process.chdir( newCwd );

exec( data.parameters[ 0 ] )
exec( data.arguments[ 0 ] )
.then( ( stdout ) => {
process.chdir( data.options.cwd );

Expand Down
6 changes: 3 additions & 3 deletions lib/commands/savehashes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
commit: commitHash
};

log.info( `Commit: ${ commitHash }.` );
log.info( `Commit: "${ commitHash }".` );

resolve( {
response: commandResponse,
Expand All @@ -44,7 +44,7 @@ module.exports = {

function getExecData( command ) {
return Object.assign( {}, data, {
parameters: [ command ]
arguments: [ command ]
} );
}
},
Expand All @@ -56,7 +56,7 @@ module.exports = {
* @param {Set} commandResponses Results of executed command for each package.
*/
afterExecute( processedPackages, commandResponses ) {
const cwd = require( '../utils/getcwd.js' )();
const cwd = require( '../utils/getcwd' )();
const mgitJsonPath = path.join( cwd, 'mgit.json' );

updateJsonFile( mgitJsonPath, ( json ) => {
Expand Down
18 changes: 11 additions & 7 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ const chalk = require( 'chalk' );
module.exports = {
/**
* @param {Object} data
* @param {Object} data.parameters Additional arguments provided by the user.
* @param {String} data.packageName Name of current package to process.
* @param {Options} data.options The options object.
* @param {Repository|null} data.repository
* @param {Repository} data.repository
* @returns {Promise}
*/
execute( data ) {
Expand All @@ -33,7 +32,6 @@ module.exports = {
const bootstrapOptions = {
options: data.options,
packageName: data.packageName,
mgit: data.mgit,
repository: data.repository
};

Expand Down Expand Up @@ -62,7 +60,10 @@ module.exports = {
log.concat( response.logs );
} )
.then( () => {
return execCommand.execute( getExecData( `git checkout ${ data.repository.branch }` ) );
return execCommand.execute( getExecData( `git checkout ${ data.repository.branch }` ) )
.catch( ( response ) => {
throw new Error( response.logs.error[ 0 ].replace( /^error\: /, '' ) );
} );
} )
.then( ( response ) => {
log.concat( response.logs );
Expand All @@ -72,7 +73,7 @@ module.exports = {
} )
.then( ( response ) => {
const stdout = response.logs.info.join( '\n' ).trim();
const isOnBranchRegexp = /HEAD detached at [\w\d]+/;
const isOnBranchRegexp = /HEAD detached at+/;

// If on a detached commit, mgit must not pull the changes.
if ( isOnBranchRegexp.test( stdout ) ) {
Expand All @@ -81,7 +82,10 @@ module.exports = {
return resolve( { logs: log.all() } );
}

return execCommand.execute( getExecData( `git pull origin ${ data.repository.branch }` ) );
return execCommand.execute( getExecData( `git pull origin ${ data.repository.branch }` ) )
.catch( ( response ) => {
throw new Error( response.logs.error[ 0 ] );
} );
} )
.then( ( response ) => {
log.concat( response.logs );
Expand All @@ -97,7 +101,7 @@ module.exports = {

function getExecData( command ) {
return Object.assign( {}, data, {
parameters: [ command ]
arguments: [ command ]
} );
}
},
Expand Down
12 changes: 6 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const logDisplay = require( './utils/displaylog' );
const getOptions = require( './utils/getoptions' );

/**
* @param {Array.<String>} parameters Arguments that the user provided.
* @param {Array.<String>} args Arguments that the user provided.
* @param {Options} options The options object. It will be extended with the default options.
*/
module.exports = function( parameters, options ) {
module.exports = function( args, options ) {
const startTime = process.hrtime();
const forkPool = createForkPool( path.join( __dirname, 'utils', 'child-process.js' ) );

Expand All @@ -23,13 +23,13 @@ module.exports = function( parameters, options ) {
const resolver = require( options.resolverPath );

// Remove all dashes from command name.
parameters[ 0 ] = parameters[ 0 ].replace( /-/g, '' );
args[ 0 ] = args[ 0 ].replace( /-/g, '' );

const commandPath = path.join( __dirname, 'commands', parameters[ 0 ] );
const commandPath = path.join( __dirname, 'commands', args[ 0 ] );
const command = require( commandPath );

if ( typeof command.beforeExecute == 'function' ) {
command.beforeExecute( parameters );
command.beforeExecute( args );
}

const processedPackages = new Set();
Expand All @@ -52,7 +52,7 @@ module.exports = function( parameters, options ) {

const data = {
command: commandPath,
parameters: parameters.slice( 1 ),
arguments: args.slice( 1 ),
options,
packageName: packageName,
repository: resolver( packageName, options )
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ module.exports = function log() {
},

log( type, msg ) {
if ( !msg ) {
return;
}

msg = msg.trim();

if ( !msg ) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"guppy-pre-commit": "^0.4.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"mockery": "^2.0.0",
"sinon": "^1.17.7"
},
"repository": {
Expand Down
Loading

0 comments on commit 5751eb7

Please sign in to comment.