-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support profile parameters in pull command
- Loading branch information
Showing
5 changed files
with
265 additions
and
117 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
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,96 @@ | ||
import { inject } from 'inversify'; | ||
import { Command } from '../../decorator'; | ||
import { GitService } from '../../services/GitService'; | ||
import { SparoProfileService } from '../../services/SparoProfileService'; | ||
|
||
import type { Argv, ArgumentsCamelCase } from 'yargs'; | ||
import type { GitRepoInfo } from 'git-repo-info'; | ||
import type { ICommand } from './base'; | ||
import type { TerminalService } from '../../services/TerminalService'; | ||
|
||
export interface IPullCommandOptions { | ||
branch?: string; | ||
remote?: string; | ||
profile?: string[]; | ||
addProfile?: string[]; | ||
} | ||
|
||
@Command() | ||
export class PullCommand implements ICommand<IPullCommandOptions> { | ||
public cmd: string = 'pull [remote] [branch]'; | ||
public description: string = 'pull changes from remote branch to local'; | ||
|
||
@inject(GitService) private _gitService!: GitService; | ||
@inject(SparoProfileService) private _sparoProfileService!: SparoProfileService; | ||
|
||
public builder(yargs: Argv<{}>): void { | ||
/** | ||
* sparo pull [remote] [branch] --profile <profile_name> --add-profile <profile_name> --no-profile | ||
*/ | ||
yargs | ||
.positional('remote', { type: 'string' }) | ||
.positional('branch', { type: 'string' }) | ||
.string('remote') | ||
.string('branch') | ||
.boolean('full') | ||
.array('profile') | ||
.default('profile', []) | ||
.array('add-profile') | ||
.default('add-profile', []); | ||
} | ||
|
||
public handler = async ( | ||
args: ArgumentsCamelCase<IPullCommandOptions>, | ||
terminalService: TerminalService | ||
): Promise<void> => { | ||
const { _gitService: gitService, _sparoProfileService: sparoProfileService } = this; | ||
const { terminal } = terminalService; | ||
const repoInfo: GitRepoInfo = gitService.getRepoInfo(); | ||
|
||
terminal.writeDebugLine(`got args in pull command: ${JSON.stringify(args)}`); | ||
const pullArgs: string[] = ['pull']; | ||
|
||
const { branch, remote } = args; | ||
|
||
if (branch && remote) { | ||
pullArgs.push(remote, branch); | ||
} | ||
|
||
const { isNoProfile, profiles, addProfiles } = await sparoProfileService.preprocessProfileArgs({ | ||
profilesFromArg: args.profile ?? [], | ||
addProfilesFromArg: args.addProfile ?? [] | ||
}); | ||
|
||
// invoke native git pull command | ||
gitService.executeGitCommand({ args: pullArgs }); | ||
|
||
// check whether profile exist in local branch | ||
if (!isNoProfile) { | ||
const targetProfileNames: Set<string> = new Set([...profiles, ...addProfiles]); | ||
const nonExistProfileNames: string[] = []; | ||
for (const targetProfileName of targetProfileNames) { | ||
if (!this._sparoProfileService.hasProfileInFS(targetProfileName)) { | ||
nonExistProfileNames.push(targetProfileName); | ||
} | ||
} | ||
|
||
if (nonExistProfileNames.length) { | ||
throw new Error( | ||
`Pull failed. The following profile(s) are missing in local branch "${branch}": ${Array.from( | ||
targetProfileNames | ||
).join(', ')}` | ||
); | ||
} | ||
} | ||
|
||
// sync local sparse checkout state with given profiles. | ||
this._sparoProfileService.syncProfileState({ | ||
profiles: isNoProfile ? null : profiles, | ||
addProfiles | ||
}); | ||
}; | ||
|
||
public getHelp(): string { | ||
return `pull help`; | ||
} | ||
} |
Oops, something went wrong.