Skip to content

Commit 2f6451e

Browse files
committed
formatted code
1 parent 1336b7b commit 2f6451e

11 files changed

+156
-127
lines changed

.eslintrc

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"root": true,
33
"parser": "@typescript-eslint/parser",
44
"plugins": ["@typescript-eslint"],
5-
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier/@typescript-eslint"],
5+
"extends": [
6+
"eslint:recommended",
7+
"plugin:@typescript-eslint/recommended",
8+
"prettier/@typescript-eslint"
9+
],
610
"rules": {
711
"@typescript-eslint/no-explicit-any": 0,
812
"@typescript-eslint/explicit-module-boundary-types": 0

.prettierrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"printWidth": 160
2+
"quoteProps": "consistent",
3+
"singleQuote": true,
4+
"trailingComma": "all"
35
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Forked from [git-rev-sync-js](https://github.com/kurttheviking/git-rev-sync-js).
1010
## usage
1111

1212
```typescript
13-
import git from "@nice-labs/git-rev";
13+
import git from '@nice-labs/git-rev';
1414

1515
// short commit-hash
1616
console.log(git.commitHash(true)); // 75bf4ee

src/exec.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import { execFileSync } from "child_process";
1+
import { execFileSync } from 'child_process';
22

3-
export type ExecValue = string | number | ExecValue[] | undefined | null | false | Record<string, boolean | undefined | null>;
3+
export type ExecValue =
4+
| string
5+
| number
6+
| ExecValue[]
7+
| undefined
8+
| null
9+
| false
10+
| Record<string, boolean | undefined | null>;
411

512
export const git = (values: ExecValue[], cwd?: string) =>
6-
execFileSync("git", pattern(values), {
13+
execFileSync('git', pattern(values), {
714
cwd,
815
shell: false,
9-
stdio: ["inherit", "pipe", "pipe"],
10-
encoding: "utf-8",
16+
stdio: ['inherit', 'pipe', 'pipe'],
17+
encoding: 'utf-8',
1118
maxBuffer: 0xffff, // 65535 bytes
1219
timeout: 60000, // one minute
1320
}).trim();
@@ -19,9 +26,9 @@ function pattern(...rest: ExecValue[]) {
1926
continue;
2027
} else if (Array.isArray(value)) {
2128
values = values.concat(pattern(...value));
22-
} else if (typeof value === "string" || typeof value === "number") {
29+
} else if (typeof value === 'string' || typeof value === 'number') {
2330
values = values.concat([String(value)]);
24-
} else if (typeof value === "object") {
31+
} else if (typeof value === 'object') {
2532
values = values.concat(Object.keys(value).filter((key) => value[key]));
2633
}
2734
}

src/git-agent.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { Git } from "./git";
1+
import { Git } from './git';
22

33
export class GitAgent extends Git {
44
public commit(message: string) {
5-
this.git("commit", "-am", message);
5+
this.git('commit', '-am', message);
66
}
77

88
public createTag(tag: string) {
9-
this.git("tag", tag);
9+
this.git('tag', tag);
1010
}
1111

1212
public createBranch(name: string, remoteName?: string) {
13-
this.git("branch", name);
13+
this.git('branch', name);
1414
if (remoteName) {
1515
const branch = this.branchName();
1616
this.checkoutBranch(name);
@@ -25,31 +25,35 @@ export class GitAgent extends Git {
2525

2626
public deleteBranch(name: string, remoteName?: string) {
2727
if (remoteName) {
28-
return this.git("push", remoteName, "--delete", name);
28+
return this.git('push', remoteName, '--delete', name);
2929
}
30-
return this.git("branch", "--delete", name);
30+
return this.git('branch', '--delete', name);
3131
}
3232

3333
public checkoutBranch(name: string) {
34-
this.git("checkout", name);
34+
this.git('checkout', name);
3535
}
3636

3737
public push(options?: PushOptions) {
3838
const opts = {
39-
"--force": options?.force ?? false,
40-
"--tags": options?.includeTags ?? false,
41-
"--no-verify": !options?.verify,
39+
'--force': options?.force ?? false,
40+
'--tags': options?.includeTags ?? false,
41+
'--no-verify': !options?.verify,
4242
};
43-
const track = options?.track && ["--set-upstream", options?.remoteName ?? "origin", this.branchName()];
44-
this.git("push", opts, track);
43+
const track = options?.track && [
44+
'--set-upstream',
45+
options?.remoteName ?? 'origin',
46+
this.branchName(),
47+
];
48+
this.git('push', opts, track);
4549
}
4650

4751
public getField(name: string) {
48-
return this.git("config", "--get", name);
52+
return this.git('config', '--get', name);
4953
}
5054

5155
public setField(name: string, value: string) {
52-
return this.git("config", name, value);
56+
return this.git('config', name, value);
5357
}
5458
}
5559

src/git-base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ExecValue, git } from "./exec";
1+
import { ExecValue, git } from './exec';
22

33
export { ExecValue };
44

src/git.ts

+41-34
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { basename } from "path";
2-
import { ExecValue, GitBase } from "./git-base";
1+
import { basename } from 'path';
2+
import { ExecValue, GitBase } from './git-base';
33

44
export class Git extends GitBase {
55
public constructor(cwd?: string) {
66
super(cwd);
77
}
88

99
public getTopLevel() {
10-
return this.git("rev-parse", "--show-toplevel");
10+
return this.git('rev-parse', '--show-toplevel');
1111
}
1212

1313
public isRepository() {
@@ -19,91 +19,98 @@ export class Git extends GitBase {
1919
return true;
2020
}
2121

22-
public branchName(rev = "HEAD") {
23-
return this.git("rev-parse", "--abbrev-ref", rev);
22+
public branchName(rev = 'HEAD') {
23+
return this.git('rev-parse', '--abbrev-ref', rev);
2424
}
2525

26-
public commitHash(short: boolean | number = false, rev = "HEAD") {
27-
const makeShort = () => (typeof short === "number" && `--short=${short}`) || (short && "--short");
28-
return this.git("rev-parse", makeShort(), rev);
26+
public commitHash(short: boolean | number = false, rev = 'HEAD') {
27+
const makeShort = () =>
28+
(typeof short === 'number' && `--short=${short}`) || (short && '--short');
29+
return this.git('rev-parse', makeShort(), rev);
2930
}
3031

3132
public commitDate(rev?: string) {
32-
return new Date(this.logN1("%aI", rev));
33+
return new Date(this.logN1('%aI', rev));
3334
}
3435

3536
public commitCount() {
36-
return Number.parseInt(this.git("rev-list", "--all", "--count"), 10);
37+
return Number.parseInt(this.git('rev-list', '--all', '--count'), 10);
3738
}
3839

3940
public message(rev?: string) {
40-
return this.logN1("%B", rev);
41+
return this.logN1('%B', rev);
4142
}
4243

4344
public describe(...values: ExecValue[]) {
44-
return this.git("describe", ...values);
45+
return this.git('describe', ...values);
4546
}
4647

4748
public tag(options?: TagOptions) {
4849
return this.describe(
49-
"--always",
50-
"--tag",
51-
"--abbrev=0",
52-
{ "--dirty": options?.markDirty, "--first-parent": options?.firstParent },
53-
options?.match && ["--match", options?.match]
50+
'--always',
51+
'--tag',
52+
'--abbrev=0',
53+
{ '--dirty': options?.markDirty, '--first-parent': options?.firstParent },
54+
options?.match && ['--match', options?.match],
5455
);
5556
}
5657

57-
public log<T extends keyof any>(fields?: Record<T, string>, n?: number): Record<T, string>[];
58+
public log<T extends keyof any>(
59+
fields?: Record<T, string>,
60+
n?: number,
61+
): Record<T, string>[];
5862
public log(fields?: string[], n?: number): string[][];
5963
public log(fields?: string, n?: number): string[];
6064
public log(fields?: any, n?: number) {
6165
if (fields === undefined) {
62-
fields = { hash: "%H", date: "%s", subject: "%cI", name: "%an" };
66+
fields = { hash: '%H', date: '%s', subject: '%cI', name: '%an' };
6367
}
6468
const pretty = `--pretty=format:${JSON.stringify(fields)},`;
6569
const count = n && `--max-count=${n}`;
66-
const result = this.git("log", "--abbrev-commit", count, pretty);
70+
const result = this.git('log', '--abbrev-commit', count, pretty);
6771
return JSON.parse(`[${result.slice(0, -1)}]`);
6872
}
6973

70-
public logN1(format: string, rev = "HEAD"): string {
74+
public logN1(format: string, rev = 'HEAD'): string {
7175
const pretty = `--pretty=format:${format}`;
72-
return this.git("log", "-1", pretty, rev);
76+
return this.git('log', '-1', pretty, rev);
7377
}
7478

75-
public remoteURL(name = "origin") {
76-
return this.git("remote", "get-url", name);
79+
public remoteURL(name = 'origin') {
80+
return this.git('remote', 'get-url', name);
7781
}
7882

7983
public hasUnstagedChanges() {
80-
return this.isDirty(this.git("write-tree"));
84+
return this.isDirty(this.git('write-tree'));
8185
}
8286

83-
public isDirty(rev = "HEAD") {
84-
return this.git("diff-index", rev, "--").length > 0;
87+
public isDirty(rev = 'HEAD') {
88+
return this.git('diff-index', rev, '--').length > 0;
8589
}
8690

8791
public isTagDirty() {
8892
try {
89-
this.describe("--exact-match", "--tags");
93+
this.describe('--exact-match', '--tags');
9094
} catch (err) {
91-
if (err.message.includes("no tag exactly matches")) {
95+
if (err.message.includes('no tag exactly matches')) {
9296
return true;
9397
}
9498
throw err;
9599
}
96100
return false;
97101
}
98102

99-
public isUpdateToDate(branchName = this.branchName(), remoteName = "origin") {
100-
this.git("fetch", remoteName, branchName);
101-
return this.commitHash(false, "HEAD") === this.commitHash(false, `${remoteName}/${branchName}`);
103+
public isUpdateToDate(branchName = this.branchName(), remoteName = 'origin') {
104+
this.git('fetch', remoteName, branchName);
105+
return (
106+
this.commitHash(false, 'HEAD') ===
107+
this.commitHash(false, `${remoteName}/${branchName}`)
108+
);
102109
}
103110

104-
public repositoryName(remoteName = "origin") {
111+
public repositoryName(remoteName = 'origin') {
105112
const url = this.remoteURL(remoteName);
106-
return basename(url, ".git");
113+
return basename(url, '.git');
107114
}
108115
}
109116

src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Git } from "./git";
1+
import { Git } from './git';
22

3-
export { Git, TagOptions } from "./git";
4-
export { GitAgent, PushOptions } from "./git-agent";
5-
export { GitBase } from "./git-base";
3+
export { Git, TagOptions } from './git';
4+
export { GitAgent, PushOptions } from './git-agent';
5+
export { GitBase } from './git-base';
66

77
export default new Git();

test/git-agent.spec.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { assert } from "chai";
2-
import "mocha";
3-
import rimraf from "rimraf";
1+
import { assert } from 'chai';
2+
import 'mocha';
3+
import rimraf from 'rimraf';
44

5-
import { GitAgent } from "../src";
6-
import { cloneBundle, mkdtemp, writeFile } from "./utils";
5+
import { GitAgent } from '../src';
6+
import { cloneBundle, mkdtemp, writeFile } from './utils';
77

8-
describe("git-agent", () => {
8+
describe('git-agent', () => {
99
let git: GitAgent;
1010

1111
before(() => {
1212
const cwd = mkdtemp();
1313
cloneBundle(cwd);
14-
writeFile(cwd, "updated");
14+
writeFile(cwd, 'updated');
1515
git = new GitAgent(cwd);
1616
});
1717

@@ -20,30 +20,30 @@ describe("git-agent", () => {
2020
});
2121

2222
it(".checkoutBranch('master')", () => {
23-
git.checkoutBranch("master");
23+
git.checkoutBranch('master');
2424
});
2525

2626
it(".createBranch('sample')", () => {
27-
git.createBranch("sample");
27+
git.createBranch('sample');
2828
});
2929

3030
it(".checkoutBranch('sample')", () => {
31-
git.checkoutBranch("sample");
31+
git.checkoutBranch('sample');
3232
});
3333

3434
it(".commit('Commit made by test')", () => {
35-
git.commit("Commit made by test");
35+
git.commit('Commit made by test');
3636
});
3737

3838
it(".createTag('sample')", () => {
39-
const tagName = "sample";
39+
const tagName = 'sample';
4040
git.createTag(tagName);
4141
assert.equal(git.tag(), tagName);
4242
});
4343

44-
it(".setField and .getField", () => {
45-
const name = "foo.bar";
46-
const value = "example";
44+
it('.setField and .getField', () => {
45+
const name = 'foo.bar';
46+
const value = 'example';
4747
git.setField(name, value);
4848
assert.equal(git.getField(name), value);
4949
});

0 commit comments

Comments
 (0)