Skip to content

Commit

Permalink
Introduced the "overrideDirectoryNames" option in a config (#98)
Browse files Browse the repository at this point in the history
Feature: Introduced an option (`overrideDirectoryNames`) that allows modifying a directory where a package would be cloned. Closes #72.
  • Loading branch information
ma2ciek authored Jun 6, 2019
2 parents 4b3439c + 4a06976 commit 55026b4
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ module.exports = {
.then( ( [ hashResponse, statusResponse ] ) => {
let packageName = data.packageName;

if ( data.mgitOptions.packagesPrefix ) {
packageName = packageName.replace( data.mgitOptions.packagesPrefix, '' );
for ( const packagePrefix of data.mgitOptions.packagesPrefix ) {
packageName = packageName.replace( new RegExp( '^' + packagePrefix ), '' );
}

const commandResponse = {
Expand Down
4 changes: 4 additions & 0 deletions lib/default-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ module.exports = function resolver( packageName, options ) {
defaultBranch: options.resolverDefaultBranch
} );

if ( options.overrideDirectoryNames[ packageName ] ) {
repository.directory = options.overrideDirectoryNames[ packageName ];
}

if ( options.resolverTargetDirectory == 'npm' ) {
repository.directory = packageName;
}
Expand Down
13 changes: 12 additions & 1 deletion lib/utils/getoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ module.exports = function cwdResolver( callOptions, cwd ) {
resolverTargetDirectory: 'git',
resolverDefaultBranch: 'master',
ignore: null,
scope: null
scope: null,
packagesPrefix: [],
overrideDirectoryNames: {}
};

if ( fs.existsSync( mgitJsonPath ) ) {
Expand All @@ -36,6 +38,10 @@ module.exports = function cwdResolver( callOptions, cwd ) {

options.packages = path.resolve( cwd, options.packages );

if ( !Array.isArray( options.packagesPrefix ) ) {
options.packagesPrefix = [ options.packagesPrefix ];
}

return options;
};

Expand Down Expand Up @@ -73,4 +79,9 @@ module.exports = function cwdResolver( callOptions, cwd ) {
* If a string: name of branch that should be created.
*
* @property {Boolean|undefined} [hash=undefined] Whether to use current commit hashes as an input data.
*
* @property {Object} [overrideDirectoryNames={}] A map that allows renaming directories where packages will be cloned.
*
* @property {String|Array.<String>} [packagesPrefix=[]] Prefix or prefixes which will be removed from packages' names during
* printing the summary of the "status" command.
*/
50 changes: 49 additions & 1 deletion tests/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,56 @@ describe( 'commands/status', () => {
}
} );

it( 'modifies the package name if "packagesPrefix" is an array', () => {
commandData.mgitOptions.packagesPrefix = [
'@ckeditor/ckeditor-',
'@ckeditor/ckeditor5-',
];

stubs.execCommand.execute.onFirstCall().resolves( {
logs: {
info: [ '6bfd379a56a32c9f8b6e58bf08e39c124cdbae10' ]
}
} );
stubs.execCommand.execute.onSecondCall().resolves( {
logs: {
info: [ 'Response returned by "git status" command.' ]
}
} );

stubs.gitStatusParser.returns( { response: 'Parsed response.' } );

return statusCommand.execute( commandData )
.then( statusResponse => {
expect( stubs.execCommand.execute.calledTwice ).to.equal( true );
expect( stubs.execCommand.execute.firstCall.args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git rev-parse HEAD' )
);
expect( stubs.execCommand.execute.secondCall.args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git status --branch --porcelain' )
);

expect( stubs.gitStatusParser.calledOnce ).to.equal( true );
expect( stubs.gitStatusParser.firstCall.args[ 0 ] ).to.equal( 'Response returned by "git status" command.' );

expect( statusResponse.response ).to.deep.equal( {
packageName: 'test-package',
commit: '6bfd379',
status: { response: 'Parsed response.' },
mgitBranch: 'master'
} );
} );

function getCommandArguments( command ) {
return Object.assign( {}, commandData, {
arguments: [ command ]
} );
}
} );

it( 'does not modify the package name if "packagesPrefix" option is not specified', () => {
delete commandData.mgitOptions.packagesPrefix;
// Mgit resolves this option to be an empty array if it isn't specified.
commandData.mgitOptions.packagesPrefix = [];

stubs.execCommand.execute.onFirstCall().resolves( {
logs: {
Expand Down
24 changes: 24 additions & 0 deletions tests/default-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,28 @@ describe( 'default resolver()', () => {
} );
} );
} );

describe( 'with options.overrideDirectoryNames', () => {
it( 'returns package with modified directory', () => {
const options = getOptions( {}, cwd );

expect( resolver( 'override-directory', options ) ).to.deep.equal( {
url: 'git@github.com:foo/bar.git',
branch: 'master',
directory: 'custom-directory'
} );
} );

it( 'ignores modified directory if "resolverTargetDirectory" is set to "npm"', () => {
const options = getOptions( {
resolverTargetDirectory: 'npm'
}, cwd );

expect( resolver( 'override-directory', options ) ).to.deep.equal( {
url: 'git@github.com:foo/bar.git',
branch: 'master',
directory: 'override-directory'
} );
} );
} );
} );
6 changes: 5 additions & 1 deletion tests/fixtures/project-a/mgit.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"@scoped/package": "c/d",
"full-url-git": "git@github.com:cksource/mgit2.git",
"full-url-git-with-branch": "git@github.com:cksource/mgit2.git#xyz",
"full-url-https": "https://github.com/cksource/mgit2.git"
"full-url-https": "https://github.com/cksource/mgit2.git",
"override-directory": "foo/bar"
},
"overrideDirectoryNames": {
"override-directory": "custom-directory"
}
}
18 changes: 14 additions & 4 deletions tests/utils/getoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ describe( 'utils', () => {
resolverTargetDirectory: 'git',
resolverDefaultBranch: 'master',
scope: null,
ignore: null
ignore: null,
packagesPrefix: [],
overrideDirectoryNames: {
'override-directory': 'custom-directory'
}
} );
} );

Expand All @@ -52,7 +56,9 @@ describe( 'utils', () => {
resolverTargetDirectory: 'git',
resolverDefaultBranch: 'master',
scope: null,
ignore: null
ignore: null,
packagesPrefix: [],
overrideDirectoryNames: {}
} );
} );

Expand All @@ -71,7 +77,9 @@ describe( 'utils', () => {
resolverTargetDirectory: 'git',
resolverDefaultBranch: 'master',
scope: null,
ignore: null
ignore: null,
packagesPrefix: [],
overrideDirectoryNames: {}
} );
} );

Expand All @@ -93,7 +101,9 @@ describe( 'utils', () => {
resolverTargetDirectory: 'git',
resolverDefaultBranch: 'master',
scope: null,
ignore: null
ignore: null,
packagesPrefix: [],
overrideDirectoryNames: {}
} );
} );
} );
Expand Down

0 comments on commit 55026b4

Please sign in to comment.