Skip to content

Commit 08d92f6

Browse files
committed
docs: add jsdoc and new error type
1 parent c8afb45 commit 08d92f6

6 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/clone.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getRemoteName } from './inspect';
88
import { GitStep, IGitUserInfos, ILogger } from './interface';
99

1010
export async function clone(options: {
11+
/** Optional fallback of userInfo. If some info is missing in userInfo, will use defaultGitInfo instead. */
1112
defaultGitInfo?: typeof defaultDefaultGitInfo;
1213
/** wiki folder path, can be relative, should exist before function call */
1314
dir: string;

src/commitAndSync.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface ICommitAndSyncOptions {
1010
/** the commit message */
1111
commitMessage?: string;
1212
commitOnly?: boolean;
13+
/** Optional fallback of userInfo. If some info is missing in userInfo, will use defaultGitInfo instead. */
1314
defaultGitInfo?: typeof defaultDefaultGitInfo;
1415
/** wiki folder path, can be relative */
1516
dir: string;

src/errors.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ export class CantSyncInSpecialGitStateAutoFixFailed extends Error {
9090
`E-6 Unable to Sync, this folder is in special condition, thus can't Sync directly. An auto-fix has been tried, but error still remains. Please resolve all the conflict manually (For example, use VSCode to open the wiki folder), if this still don't work out, please use professional Git tools (Source Tree, GitKraken) to solve this. This is caused by procedural bug in the git-sync-js.\n${stateMessage}`;
9191
}
9292
}
93+
94+
export class CantForcePullError extends Error {
95+
stateMessage: string;
96+
constructor(stateMessage: string) {
97+
super(stateMessage);
98+
Object.setPrototypeOf(this, CantForcePullError.prototype);
99+
this.stateMessage = stateMessage;
100+
this.name = 'CantForcePullError';
101+
this.message = `E-7 Unable to force pull remote. This is caused by procedural bug in the git-sync-js.\n${stateMessage}`;
102+
}
103+
}

src/forcePull.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { GitProcess } from 'dugite';
22
import { credentialOff, credentialOn } from './credential';
33
import { defaultGitInfo as defaultDefaultGitInfo } from './defaultGitInfo';
4-
import { SyncParameterMissingError } from './errors';
4+
import { CantForcePullError, SyncParameterMissingError } from './errors';
55
import { getDefaultBranchName, getRemoteName } from './inspect';
66
import { GitStep, IGitUserInfos, ILogger } from './interface';
77
import { fetchRemote } from './sync';
88

99
export interface IForcePullOptions {
10+
/** Optional fallback of userInfo. If some info is missing in userInfo, will use defaultGitInfo instead. */
1011
defaultGitInfo?: typeof defaultDefaultGitInfo;
1112
/** wiki folder path, can be relative */
1213
dir: string;
@@ -23,6 +24,7 @@ export interface IForcePullOptions {
2324

2425
/**
2526
* Ignore all local changes, force reset local to remote.
27+
* This is usually used in readonly blog, that will fetch content from a remote repo. And you can push content to the remote repo, let the blog update.
2628
*/
2729
export async function forcePull(options: IForcePullOptions) {
2830
const { dir, logger, defaultGitInfo = defaultDefaultGitInfo, userInfo, remoteUrl } = options;
@@ -65,14 +67,23 @@ export async function forcePull(options: IForcePullOptions) {
6567
logProgress(GitStep.StartResettingLocalToRemote);
6668
await hardResetLocalToRemote(dir, branch, remoteName);
6769
logProgress(GitStep.FinishForcePull);
70+
} catch (error) {
71+
if (error instanceof CantForcePullError) {
72+
throw error;
73+
} else {
74+
throw new CantForcePullError(`${(error as Error).message} ${(error as Error).stack ?? ''}`);
75+
}
6876
} finally {
6977
await credentialOff(dir, remoteName, remoteUrl);
7078
}
7179
}
7280

81+
/**
82+
* Internal method used by forcePull, does the `reset --hard`.
83+
*/
7384
export async function hardResetLocalToRemote(dir: string, branch: string, remoteName: string) {
7485
const { exitCode, stderr } = await GitProcess.exec(['reset', '--hard', `${remoteName}/${branch}`], dir);
7586
if (exitCode !== 0) {
76-
throw new Error(`Failed to reset local to remote: ${stderr}`);
87+
throw new CantForcePullError(`${remoteName}/${branch} ${stderr}`);
7788
}
7889
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/** primary functions */
22
export * from './clone';
33
export * from './commitAndSync';
4+
export * from './forcePull';
45
export * from './initGit';
56
/** utils */
67
export * from './credential';
78
export * from './defaultGitInfo';
89
export * from './errors';
9-
export * from './forcePull';
1010
export * from './inspect';
1111
export * from './interface';
1212
export * from './sync';

src/initGit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { commitFiles } from './sync';
88

99
export type IInitGitOptions = IInitGitOptionsSyncImmediately | IInitGitOptionsNotSync;
1010
export interface IInitGitOptionsSyncImmediately {
11+
/** Optional fallback of userInfo. If some info is missing in userInfo, will use defaultGitInfo instead. */
1112
defaultGitInfo?: typeof defaultDefaultGitInfo;
1213
/** wiki folder path, can be relative */
1314
dir: string;

0 commit comments

Comments
 (0)