Skip to content

fix: add types and tests for the documented .exec api #631

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

Merged
merged 2 commits into from
Jun 8, 2021
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
14 changes: 12 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ require('simple-git')()
.push(['-u', 'origin', 'master'], () => console.log('done'));
```

### Piping to the console for long running tasks
### Piping to the console for long-running tasks

```js
require('simple-git')()
Expand All @@ -773,14 +773,24 @@ require('simple-git')()
### Update repo and print messages when there are changes, restart the app

```javascript
// when using a chain
require('simple-git')()
.exec(() => console.log('Starting pull...'))
.pull((err, update) => {
if(update && update.summary.changes) {
require('child_process').exec('npm restart');
}
})
.exec(() => console.log('pull done.'));
.exec(() => console.log('pull done.'));

// when using async and optional chaining
const git = require('simple-git')()
console.log('Starting pull...')
if ((await git.pull())?.summary.changes) {
require('child_process').exec('npm restart');
}
console.log('pull done.');

```

### Get a full commits list, and then only between 0.11.0 and 0.12.0 tags
Expand Down
4 changes: 0 additions & 4 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,6 @@ Git.prototype.clean = function (mode, options, then) {
);
};

/**
* Call a simple function at the next step in the chain.
* @param {Function} [then]
*/
Git.prototype.exec = function (then) {
const task = {
commands: [],
Expand Down
23 changes: 23 additions & 0 deletions test/integration/exec.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createTestContext, newSimpleGit, SimpleGitTestContext } from '../__fixtures__';

describe('exec', () => {
let context: SimpleGitTestContext;

beforeEach(async () => {
context = await createTestContext();
});

it('will exec a function between other chained methods', async () => {
const calls: string[] = [];

await (
newSimpleGit(context.root)
.exec(() => calls.push('a'))
.raw('init', () => calls.push('b'))
.exec(() => calls.push('c'))
.raw('init', () => calls.push('d'))
);

expect(calls).toEqual(['a', 'b', 'c', 'd']);
});
})
6 changes: 6 additions & 0 deletions typings/simple-git.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ export interface SimpleGit extends SimpleGitBase {

env(env: object): this;

/**
* Calls the supplied `handle` function at the next step in the chain, used to run arbitrary functions synchronously
* before the next task in the git api.
*/
exec(handle: () => void): Response<void>;

/**
* Updates the local working copy database with changes from the default remote repo and branch.
*/
Expand Down