Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for presets #153

Merged
merged 6 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ CLI options:

--scope Restricts the command to packages which names match the given glob pattern.
Default: null

--preset Uses an alternative set of dependencies defined in the config file.
```

All these options can also be specified in `mrgit.json` (options passed through CLI takes precedence):
Expand All @@ -137,34 +139,54 @@ The dependency keys can be any strings, but it's recommended to use package name

Examples:

```js
```json
// Clone 'git@github.com:cksource/foo.git' and check out to 'master'.
{
"foo": "git@github.com:cksource/foo.git"
}
```

```js
```json
// Short format. Clone 'git@github.com:cksource/foo.git' and check out to branch 'dev'.
{
"@cksource/foo": "cksource/foo#dev"
}
```

```js
```json
// Clone 'https://github.com/cksource/foo.git' (via HTTPS) and check out to tag 'v1.2.3'.
{
"foo": "https://github.com/cksource/foo.git@v1.2.3"
}
```

```js
```json
// Clone 'cksource/foo' and check out to the latest available tag.
{
"foo": "cksource/foo@latest"
}
```

### The `presets` option

This option allows the user to switch between different states of dependencies easily. When using any command with the `--preset` option, it will behave as if the `dependencies` option was using values from the given preset. Dependencies not specified in the preset but in the `dependencies` object will use a version from the latter as a fallback.

Example:

```json
{
"presets": {
"dev": {
"@cksource/foo": "cksource/foo#dev"
},
"example-feature": {
"@cksource/foo": "cksource/foo#i/1-example-feature",
"@cksource/bar": "cksource/foo#i/1-example-feature"
}
}
}
```

### Recursive cloning

When the `--recursive` option is used `mrgit` will clone repositories recursively. First, it will clone the `dependencies` specified in `mrgit.json` and, then, their `dependencies` and `devDependencies` specified in `package.json` files located in cloned repositories.
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ function handleCli() {
${ y( '--scope' ) } Restricts the command to packages which names match the given glob pattern.
${ g( 'Default: null' ) }

${ y( '--preset' ) } Uses an alternative set of dependencies defined in the config file.

${ u( 'Git Options:' ) }
Git options are supported by the following commands: commit, diff, fetch, push.
Type "mrgit [command] -h" in order to see which options are supported.
Expand Down
34 changes: 31 additions & 3 deletions lib/commands/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ module.exports = {
*
* @param {Set} processedPackages Collection of processed packages.
* @param {Set} commandResponses Results of executed command for each package.
* @param {Options} toolOptions Options resolved by mrgit.
*/
afterExecute( processedPackages, commandResponses ) {
afterExecute( processedPackages, commandResponses, toolOptions ) {
const cwd = require( '../utils/getcwd' )();
const mrgitJsonPath = path.join( cwd, 'mrgit.json' );

Expand All @@ -112,15 +113,42 @@ module.exports = {
.replace( tagPattern, '' )
.split( '#' )[ 0 ];

const objectToUpdate = getObjectToUpdate( json, toolOptions, response.packageName );

// If returned branch is equal to 'master', save only the repository path.
if ( response.branch && response.data === 'master' ) {
json.dependencies[ response.packageName ] = repository;
objectToUpdate[ response.packageName ] = repository;
} else {
json.dependencies[ response.packageName ] = `${ repository }#${ response.data }`;
objectToUpdate[ response.packageName ] = `${ repository }#${ response.data }`;
}
}

return json;
} );

/**
* If preset is being used it should update the value defined in the preset,
* rather than the one in the base "dependencies" object.
*
* @param {Object} json
* @param {Options} toolOptions
* @param {String} packageName
* @returns
*/
function getObjectToUpdate( json, toolOptions ) {
if ( !toolOptions.preset ) {
return json.dependencies;
}

if ( !json.presets ) {
return json.dependencies;
}

if ( !json.presets[ toolOptions.preset ] ) {
return json.dependencies;
}

return json.presets[ toolOptions.preset ];
}
}
};
11 changes: 8 additions & 3 deletions lib/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,15 @@ module.exports = {
shouldDisplaySyncHint = true;
}

if ( !shouldUseTag && status.branch !== mrgitBranch ) {
if ( !shouldUseTag && status.branch !== mrgitBranch && commit !== mrgitBranch ) {
branchOrTag = `${ chalk.cyan( '!' ) } ${ branchOrTag }`;
shouldDisplaySyncHint = true;
}

if ( !shouldUseTag && mrgitBranch === commit ) {
branchOrTag = 'Using saved commit →';
}

if ( status.ahead ) {
branchOrTag += chalk.yellow( ` ↑${ status.ahead }` );
}
Expand Down Expand Up @@ -199,8 +203,9 @@ module.exports = {
hints.push( [
chalk.green( 'L' ),
'This is the latest local tag. To ensure having latest remote tag, execute',
chalk.blue( 'mrgit sync' )
].join( ' ' ) + '.' );
chalk.blue( 'mrgit fetch' ),
'before checking status.'
].join( ' ' ) );
}

if ( shouldDisplaySyncHint ) {
Expand Down
12 changes: 11 additions & 1 deletion lib/utils/getoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const shell = require( 'shelljs' );
* @param {String} cwd An absolute path to the directory where `mrgit.json` is available.
* @returns {Options} The options object.
*/
module.exports = function cwdResolver( callOptions, cwd ) {
module.exports = function getOptions( callOptions, cwd ) {
const mrgitJsonPath = path.resolve( cwd, 'mrgit.json' );

// Default options.
Expand Down Expand Up @@ -52,6 +52,16 @@ module.exports = function cwdResolver( callOptions, cwd ) {
options.cwdPackageBranch = response.stdout.trim();
}

if ( !options.preset ) {
return options;
}

if ( !options.presets || !options.presets[ options.preset ] ) {
throw new Error( `Preset "${ options.preset }" is not defined in "mrgit.json" file.` );
}

options.dependencies = Object.assign( options.dependencies, options.presets[ options.preset ] );

return options;
};

Expand Down
Loading