0.0.2 ā€¢ Published 1 year ago

nestjs-git v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Getting started

Node.js build and publish package

TypeScript Nestjs Free. Built on open source. Runs everywhere. GitHub Actions

Git module for Nest framework (node.js)

Installation

Install with yarn or npm: yarn or npm:

# yarn
yarn add nestjs-git
# npm
npm i nestjs-git --save
# pnpm
pnpm add nestjs-git --save

Usage example


// cat.service.ts
import fs from 'fs';
import { GitService } from 'nestjs-git/dist/git';

export class CatService {
  constructor(private readonly gitService: GitService) {}

  public async getConfig() {
    return this.gitService.getConfig({
      fs,
      dir: '/tutorial',
      path: 'remote.origin.url',
    });
  }
}

Now that we've got an empty directory, let's clone a git repository. I'm cloning nestjs-git itself (how meta!). I'm only cloning a single branch and only to a depth of 10 commits to save time, bandwidth, and browser storage space. Since GitHub hasn't added CORS headers to the git clone endpoint yet, we have to use a proxy server. (They never suspected that a browser would want to run "git clone"!)

// cat.service.ts
import fs from 'fs';
import { GitService } from 'nestjs-git';

export class CatService {
  constructor(private readonly gitService: GitService) {}

  public async clone(http: HttpClient, dir: string) {
    return this.gitService.clone({
      fs,
      http,
      dir,
      corsProxy: 'https://cors.isomorphic-git.org',
      url: 'https://github.com/hebertcisco/nestjs-gitt',
      ref: 'main',
      singleBranch: true,
      depth: 10,
    });
  }
}

Great! We've got files. We've also got commits. Let's see what the recent history of this branch looks like. Hint: be sure to expand the objects so you can see all the properties.

await this.gitService.log({ fs, dir });

Git is used to track files. Let's see what kind of file things we can do!

this.gitService.status is a major one. That let's us compare the working directory file to the current branch.

await this.gitService.status({ fs, dir, filepath: 'README.md' });

OK so the status is "unmodified" because we haven't modified it. What if we change the file by writing over it?

await pfs.writeFile(`${dir}/README.md`, 'Very short README', 'utf8');
await this.gitService.status({ fs, dir, filepath: 'README.md' });

The status is "*modified" with a star. Text editors sometimes use a "*" in the title bar to indicate a file has unsaved changes. That's what is going on here - we've made changes to the file but we haven't added those changes to the git "staging area".

await this.gitService.add({ fs, dir, filepath: 'README.md' });
await this.gitService.status({ fs, dir, filepath: 'README.md' });

Now that we've done "git add" that little star has gone away and the status is just "modified".

What if we write a new file?

await this.gitService.status({ fs, dir, filepath: 'newfile.txt' });

"*added" means the file has been added, but not staged. Simple to fix:

await this.gitService.add({ fs, dir, filepath: 'newfile.txt' });
await this.gitService.status({ fs, dir, filepath: 'newfile.txt' });

This last bit has always been unintuitive to me... but you need to tell git you deleted the file.

await this.gitService.remove({ fs, dir, filepath: 'package.json' });
await this.gitService.status({ fs, dir, filepath: 'package.json' });

What happens if you tell git you deleted a file but you really didn't?

await this.gitService.remove({ fs, dir, filepath: 'package-lock.json' });
await this.gitService.status({ fs, dir, filepath: 'package-lock.json' });

Does that make sense? No? Sorry, naming things is hard. (Git doesn't do a great job of it either. It reports the file as "untracked" and "deleted" at the same time.) OK, enough messing around.

await this.gitService.add({ fs, dir, filepath: 'package-lock.json' });
await this.gitService.status({ fs, dir, filepath: 'package-lock.json' });

Cool. So we've deleted package.json and replaced the README with the text "Very short README". A solid day's work - let's commit those changes.

let sha = await this.gitService.commit({
  fs,
  dir,
  message: 'Delete package.json and overwrite README.',
  author: {
    name: 'Mr. Test',
    email: 'mrtest@example.com',
  },
});

console.log(sha);

git.commit returns the shasum of our new commit. Let's examine our handiwork:

let commits = await this.gitService.log({ fs, dir, depth: 1 });
console.log(commits[0]);

šŸ¤ Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ā­ļø if this project helped you!

Or buy me a coffee šŸ™ŒšŸ¾

šŸ“ License

Copyright Ā© 2022 Hebert F Barros. This project is MIT licensed.