steveukx/git-js

Unable to clone a repository

Closed this issue · 1 comments

I am trying to use simple-git to clone a repository. If I execute the command directly from the terminal, I am able to clone the repository without problems.

git clone https://github.com/steveukx/git-js.git C:\repos\example1

When I try to replicate this code through a typescript class, nothing happens. Even if I set a fake git repository, 0 logs are generated.

import { simpleGit, CleanOptions } from "simple-git";

export class Git {
static clone(repository: string, outputDirectory: string) {
   const git = simpleGit({ binary: 'git'});
  console.log("cloning the repo");
  git.clone("https://github.com/steveukx/git-js.git", "C:\repos\example1");
  console.log("repo cloned");
}}

I can see the "cloning the repo" and the "repo cloned" lines... but nothing else. No git execution logs, nothing in the output folder.
Is this an issue with my code or the settings of my typescript project?

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs", 
    "rootDir": "src/",  
    "moduleResolution": "node", 
    "target": "es2020",
    "types": [ "node" ], 
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true, 
    "strict": true,   
    "skipLibCheck": true
}}

Hi, the clone operation is asynchronous, to allow for the fact it may take some time to run the underlying git command. You would therefore need to either use an async function to await the git.clone, or use promises to chain .then handlers. For example:

import { simpleGit, CleanOptions } from "simple-git";

export class Git {
  async static clone(repository: string, outputDirectory: string) {
    const git = simpleGit({ binary: 'git'});
    console.log("cloning the repo");

    await git.clone("https://github.com/steveukx/git-js.git", "C:/repos/example1");
    console.log("repo cloned");
  }
}

For more info, see the https://github.com/steveukx/git-js#using-task-promises section of the docs.

In your example, the Windows file path format should use escaped slashes \\ or forward slashes / to avoid them being interpreted as special characters (for example \r is a carriage return character).