-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from cksource/t/27
Feature: Introduced "save-hashes" command for saving hashes of the packages. Resolves: #27.
- Loading branch information
Showing
9 changed files
with
166 additions
and
10 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
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
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
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,72 @@ | ||
/** | ||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const path = require( 'path' ); | ||
const updateJsonFile = require( '../utils/updatejsonfile' ); | ||
|
||
module.exports = { | ||
/** | ||
* @param {Object} data | ||
* @param {String} data.packageName Name of current package to process. | ||
* @returns {Promise} | ||
*/ | ||
execute( data ) { | ||
const log = require( '../utils/log' )(); | ||
const execCommand = require( './exec' ); | ||
|
||
return new Promise( ( resolve, reject ) => { | ||
execCommand.execute( getExecData( 'git rev-parse HEAD' ) ) | ||
.then( ( execResponse ) => { | ||
const commitHash = execResponse.logs.info[ 0 ]; | ||
|
||
const commandResponse = { | ||
packageName: data.packageName, | ||
commit: commitHash | ||
}; | ||
|
||
log.info( `Commit: ${ commitHash }.` ); | ||
|
||
resolve( { | ||
response: commandResponse, | ||
logs: log.all() | ||
} ); | ||
} ) | ||
.catch( ( error ) => { | ||
log.error( error ); | ||
|
||
reject( { logs: log.all() } ); | ||
} ); | ||
} ); | ||
|
||
function getExecData( command ) { | ||
return Object.assign( {}, data, { | ||
parameters: [ command ] | ||
} ); | ||
} | ||
}, | ||
|
||
/** | ||
* Saves collected hashes to configuration file. | ||
* | ||
* @param {Set} processedPackages Collection of processed packages. | ||
* @param {Set} commandResponses Results of executed command for each package. | ||
*/ | ||
afterExecute( processedPackages, commandResponses ) { | ||
const cwd = require( '../utils/getcwd.js' )(); | ||
const mgitJsonPath = path.join( cwd, 'mgit.json' ); | ||
|
||
updateJsonFile( mgitJsonPath, ( json ) => { | ||
for ( const response of commandResponses.values() ) { | ||
const repository = json.dependencies[ response.packageName ].split( '#' )[ 0 ]; | ||
|
||
json.dependencies[ response.packageName ] = `${ repository }#${ response.commit }`; | ||
} | ||
|
||
return json; | ||
} ); | ||
} | ||
}; |
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
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 |
---|---|---|
|
@@ -5,8 +5,6 @@ | |
|
||
'use strict'; | ||
|
||
/** | ||
*/ | ||
module.exports = function log() { | ||
const logs = new Map( [ | ||
[ 'info', [] ], | ||
|
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,22 @@ | ||
/** | ||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const fs = require( 'fs' ); | ||
|
||
/** | ||
* Updates JSON file under specified path. | ||
* @param {String} path Path to file on disk. | ||
* @param {Function} updateFunction Function that will be called with parsed JSON object. It should return | ||
* modified JSON object to save. | ||
*/ | ||
module.exports = function updateJsonFile( path, updateFunction ) { | ||
const contents = fs.readFileSync( path, 'utf-8' ); | ||
let json = JSON.parse( contents ); | ||
json = updateFunction( json ); | ||
|
||
fs.writeFileSync( path, JSON.stringify( json, null, 2 ) + '\n', 'utf-8' ); | ||
}; |
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
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,43 @@ | ||
/** | ||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* jshint mocha:true */ | ||
|
||
'use strict'; | ||
|
||
const updateJsonFile = require( '../../lib/utils/updatejsonfile' ); | ||
const expect = require( 'chai' ).expect; | ||
const sinon = require( 'sinon' ); | ||
|
||
describe( 'utils', () => { | ||
let sandbox; | ||
|
||
beforeEach( () => { | ||
sandbox = sinon.sandbox.create(); | ||
} ); | ||
|
||
afterEach( () => { | ||
sandbox.restore(); | ||
} ); | ||
describe( 'updateJsonFile()', () => { | ||
it( 'should read, update and save JSON file', () => { | ||
const path = 'path/to/file.json'; | ||
const fs = require( 'fs' ); | ||
const readFileStub = sandbox.stub( fs, 'readFileSync', () => '{}' ); | ||
const modifiedJSON = { modified: true }; | ||
const writeFileStub = sandbox.stub( fs, 'writeFileSync' ); | ||
|
||
updateJsonFile( path, () => { | ||
return modifiedJSON; | ||
} ); | ||
|
||
expect( readFileStub.calledOnce ).to.equal( true ); | ||
expect( readFileStub.firstCall.args[ 0 ] ).to.equal( path ); | ||
expect( writeFileStub.calledOnce ).to.equal( true ); | ||
expect( writeFileStub.firstCall.args[ 0 ] ).to.equal( path ); | ||
expect( writeFileStub.firstCall.args[ 1 ] ).to.equal( JSON.stringify( modifiedJSON, null, 2 ) + '\n' ); | ||
} ); | ||
} ); | ||
} ); |