Skip to content

Commit

Permalink
Merge pull request #48 from tiktok/tweak-1
Browse files Browse the repository at this point in the history
chore: final tweak before 1.0
  • Loading branch information
chengcyber authored Mar 5, 2024
2 parents 5c929b8 + 53e8e14 commit d09c3e8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
23 changes: 11 additions & 12 deletions apps/sparo-lib/src/cli/commands/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface ICloneCommandOptions {
directory?: string;
skipGitConfig?: boolean;
profile?: string[];
addProfile?: string[];
}

@Command()
Expand Down Expand Up @@ -57,8 +56,6 @@ export class CloneCommand implements ICommand<ICloneCommandOptions> {
})
.array('profile')
.default('profile', [])
.array('add-profile')
.default('add-profile', [])
.check((argv) => {
if (!argv.repository) {
return 'You must specify a repository to clone.';
Expand Down Expand Up @@ -92,16 +89,16 @@ export class CloneCommand implements ICommand<ICloneCommandOptions> {

process.chdir(directory);

const { profiles, addProfiles, isNoProfile } = await this._sparoProfileService.preprocessProfileArgs({
const { profiles, isNoProfile } = await this._sparoProfileService.preprocessProfileArgs({
profilesFromArg: args.profile ?? [],
addProfilesFromArg: args.addProfile ?? []
addProfilesFromArg: []
});

await this._GitSparseCheckoutService.ensureSkeletonExistAndUpdated();

// check whether profile exist in local branch
if (!isNoProfile) {
const targetProfileNames: Set<string> = new Set([...profiles, ...addProfiles]);
const targetProfileNames: Set<string> = new Set(profiles);
const nonExistProfileNames: string[] = [];
for (const targetProfileName of targetProfileNames) {
if (!this._sparoProfileService.hasProfileInFS(targetProfileName)) {
Expand All @@ -118,11 +115,13 @@ export class CloneCommand implements ICommand<ICloneCommandOptions> {
}
}

// sync local sparse checkout state with given profiles.
await this._sparoProfileService.syncProfileState({
profiles: isNoProfile ? undefined : profiles,
addProfiles
});
// Avoid redundant sync if no profile is given
if (!isNoProfile && profiles.size) {
// sync local sparse checkout state with given profiles.
await this._sparoProfileService.syncProfileState({
profiles: isNoProfile ? undefined : profiles
});
}

// set recommended git config
if (!args.skipGitConfig) {
Expand All @@ -140,7 +139,7 @@ export class CloneCommand implements ICommand<ICloneCommandOptions> {
terminal.writeLine(' ' + Colorize.cyan(`cd ${directory}`));
terminal.writeLine();

if (isNoProfile || (profiles.size === 0 && addProfiles.size === 0)) {
if (isNoProfile || profiles.size === 0) {
terminal.writeLine('Your next step is to choose a Sparo profile for checkout.');
terminal.writeLine('To see available profiles in this repo:');
terminal.writeLine(' ' + Colorize.cyan('sparo list-profiles'));
Expand Down
3 changes: 2 additions & 1 deletion apps/sparo-lib/src/cli/commands/init-profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class InitProfileCommand implements ICommand<IInitProjectCommandOptions>
type: 'string',
description: 'The name of the profile to initialize.'
})
.demandOption(['profile']);
.demandOption(['profile'])
.usage('Usage: $0 init-profile --profile <profile>');
}

public handler = async (
Expand Down
15 changes: 11 additions & 4 deletions apps/sparo-lib/src/services/GitSparseCheckoutService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ export class GitSparseCheckoutService {
if ('true' !== this._gitService.getGitConfig('core.sparsecheckout')?.trim()) {
throw new Error('Sparse checkout is not enabled in this repo.');
}
this.initializeAndUpdateSkeleton();
this._initializeAndUpdateSkeleton();
}

public initializeAndUpdateSkeleton(): void {
/**
* Other services should call ensureSkeletonExistAndUpdated
*/
private _initializeAndUpdateSkeleton(): void {
this._terminalService.terminal.writeLine('Checking out and updating core files...');
this._loadRushConfiguration();
this._prepareMonorepoSkeleton();
Expand Down Expand Up @@ -196,12 +199,14 @@ export class GitSparseCheckoutService {
*/
switch (checkoutAction) {
case 'purge':
case 'skeleton':
// re-apply the initial paths for setting up sparse repo state
this._prepareMonorepoSkeleton({
restore: checkoutAction === 'purge'
});
break;
case 'skeleton':
// Skeleton should be always prepared in the beginning of the function
break;
case 'add':
case 'set':
if (targetFolders.length === 0) {
Expand All @@ -211,7 +216,8 @@ export class GitSparseCheckoutService {
if (checkoutAction === 'set') {
targetFolders.push(...this._finalSkeletonPaths);
}
terminal.writeLine(
terminal.writeLine(`Checking out ${targetFolders.length} folders...`);
terminal.writeDebugLine(
`Performing sparse checkout ${checkoutAction} for these folders: \n${targetFolders.join('\n')}`
);

Expand Down Expand Up @@ -266,6 +272,7 @@ export class GitSparseCheckoutService {
private _prepareMonorepoSkeleton(options: { restore?: boolean } = {}): void {
const { restore } = options;
this._finalSkeletonPaths = this._getSkeletonPaths();
this._terminalService.terminal.writeLine('Checking out skeleton...');
this._terminalService.terminal.writeDebugLine(`Skeleton paths: ${this._finalSkeletonPaths.join(', ')}`);
this._sparseCheckoutPaths(this._finalSkeletonPaths, {
action: restore ? 'set' : 'add'
Expand Down
18 changes: 12 additions & 6 deletions apps/sparo-lib/src/services/SparoProfileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,18 @@ ${availableProfiles.join(',')}
addProfiles?: Set<string>;
}): Promise<void> {
this._localState.reset();
this._terminalService.terminal.writeLine(
`Syncing local sparse checkout state with following specified profiles:\n${Array.from([
...(profiles ?? []),
...(addProfiles ?? [])
]).join('\n')}`
);
const allProfiles: string[] = Array.from([...(profiles ?? []), ...(addProfiles ?? [])]);
if (allProfiles.length > 1) {
this._terminalService.terminal.writeLine(
`Syncing checkout with these Sparo profiles:\n${allProfiles.join(', ')}`
);
} else if (allProfiles.length === 1) {
this._terminalService.terminal.writeLine(
`Syncing checkout with the Sparo profile: ${allProfiles[0]}`
);
} else {
this._terminalService.terminal.writeLine('Syncing checkout with the Sparo skeleton (no profile selection)');
}
this._terminalService.terminal.writeLine();
if (!profiles || profiles.size === 0) {
// If no profile was specified, purge local state to skeleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"changes": [
{
"packageName": "sparo",
"comment": "support profile related parameters in pull & clone command",
"comment": "Support --profile parameter in clone command",
"type": "none"
}
],
Expand Down
10 changes: 10 additions & 0 deletions common/changes/sparo/tweak-1_2024-03-05-04-01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "sparo",
"comment": "",
"type": "none"
}
],
"packageName": "sparo"
}

0 comments on commit d09c3e8

Please sign in to comment.