-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
git.cwd
can now be configured to affect just the chain rather…
… than root instance. Adds a new interface for `git.cwd` to supply the `directory` as an object containing `{ path: string, root?: boolean }` where omitting the `root` property or setting it to `false` will change the working directory only for the chain, whereas setting `root` to `true` (or supplying `directory` as a string - for backward compatibility purposes) will change the working directory of the main instance.
- Loading branch information
Showing
11 changed files
with
154 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
## Changing Working Directory | ||
|
||
To change the directory the `git` commands are run in you can either configure the `simple-git` instance | ||
when it is created by using the `baseDir` property: | ||
|
||
```typescript | ||
import { join } from 'path'; | ||
import simpleGit from 'simple-git'; | ||
|
||
const git = simpleGit({ baseDir: join(__dirname, 'repos') }); | ||
``` | ||
|
||
Or explicitly set the working directory at some later time, for example after cloning a repo: | ||
|
||
```typescript | ||
import { join } from 'path'; | ||
import simpleGit, { SimpleGit } from 'simple-git'; | ||
|
||
const remote = `https://github.com/steveukx/git-js.git`; | ||
const target = join(__dirname, 'repos', 'git-js'); | ||
|
||
// repo is now a `SimpleGit` instance operating on the `target` directory | ||
// having cloned the remote repo then switched into the cloned directory | ||
const repo: SimpleGit = await simpleGit().clone(remote, target).cwd({ path: target }); | ||
``` | ||
|
||
In the example above we're using the command chaining feature of `simple-git` where many commands | ||
are treated as an atomic operation. To rewrite this using separate `async/await` steps would be: | ||
|
||
```typescript | ||
import { join } from 'path'; | ||
import simpleGit, { SimpleGit } from 'simple-git'; | ||
|
||
const remote = `https://github.com/steveukx/git-js.git`; | ||
const target = join(__dirname, 'repos', 'git-js'); | ||
|
||
// create a `SimpleGit` instance | ||
const git: SimpleGit = simpleGit(); | ||
|
||
// use that instance to do the clone | ||
await git.clone(remote, target); | ||
|
||
// then set the working directory of the root instance - you want all future | ||
// tasks run through `git` to be from the new directory, rather than just tasks | ||
// chained off this task | ||
await git.cwd({ path: target, root: true }); | ||
``` |
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
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,13 @@ | ||
import { folderExists } from '../utils'; | ||
import { SimpleGitExecutor } from '../types'; | ||
import { adhocExecTask } from './task'; | ||
|
||
export function changeWorkingDirectoryTask (directory: string, root?: SimpleGitExecutor) { | ||
return adhocExecTask((instance: SimpleGitExecutor) => { | ||
if (!folderExists(directory)) { | ||
throw new Error(`Git.cwd: cannot change to non-directory "${ directory }"`); | ||
} | ||
|
||
return ((root || instance).cwd = directory); | ||
}); | ||
} |
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
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