Skip to content

Commit

Permalink
Merge pull request #17 from tiktok/fix-checkout-uncommitted-sparo-pro…
Browse files Browse the repository at this point in the history
…file

Fix checkout uncommitted sparo profile
  • Loading branch information
chengcyber authored Feb 23, 2024
2 parents 4f38fcb + 2fb5432 commit cf445cb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
14 changes: 12 additions & 2 deletions apps/sparo-lib/src/cli/commands/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,18 @@ export class CheckoutCommand implements ICommand<ICheckoutCommandOptions> {

const nonExistProfileNames: string[] = [];
for (const targetProfileName of targetProfileNames) {
if (!this._sparoProfileService.hasProfile(targetProfileName, operationBranch)) {
nonExistProfileNames.push(targetProfileName);
/**
* If the operation branch is current branch, check the existence from file system.
* Otherwise, check the existence from git.
*/
if (operationBranch === currentBranch) {
if (!this._sparoProfileService.hasProfileInFS(targetProfileName)) {
nonExistProfileNames.push(targetProfileName);
}
} else {
if (!this._sparoProfileService.hasProfileInGit(targetProfileName, operationBranch)) {
nonExistProfileNames.push(targetProfileName);
}
}
}

Expand Down
18 changes: 14 additions & 4 deletions apps/sparo-lib/src/services/SparoProfileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export class SparoProfileService {
public async loadProfilesAsync(): Promise<void> {
if (!this._loadPromise) {
this._loadPromise = (async () => {
const repoRoot: string = this._gitService.getRepoInfo().root;
const sparoProfileFolder: string = path.resolve(repoRoot, defaultSparoProfileFolder);
const sparoProfileFolder: string = this._sparoProfileFolder;
this._terminalService.terminal.writeDebugLine(
'loading sparse profiles from folder:',
sparoProfileFolder
Expand All @@ -39,7 +38,7 @@ export class SparoProfileService {
sparoProfile = await SparoProfile.loadFromFileAsync(this._terminalService, sparoProfilePath);
} catch (e) {
// TODO: more error handling
this._terminalService.terminal.writeLine((e as Error).message);
this._terminalService.terminal.writeErrorLine((e as Error).message);
}

if (sparoProfile) {
Expand All @@ -65,10 +64,21 @@ export class SparoProfileService {
return this._profiles;
}

public hasProfile(name: string, branch: string): boolean {
public hasProfileInGit(name: string, branch: string): boolean {
return this._gitService.hasFile(`${defaultSparoProfileFolder}/${name}.json`, branch);
}

public hasProfileInFS(name: string): boolean {
const sparoProfileFilepath: string = path.resolve(this._sparoProfileFolder, `${name}.json`);
return FileSystem.exists(sparoProfileFilepath);
}

private get _sparoProfileFolder(): string {
const repoRoot: string = this._gitService.getRepoInfo().root;
const sparoProfileFolder: string = path.resolve(repoRoot, defaultSparoProfileFolder);
return sparoProfileFolder;
}

private static _getProfileName(profilePath: string): string {
const pathArr: string[] = profilePath.split('/');
const last: string = pathArr[pathArr.length - 1];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "sparo",
"comment": "Support checkout uncommitted sparo profiles",
"type": "none"
}
],
"packageName": "sparo"
}

0 comments on commit cf445cb

Please sign in to comment.