Merge pull request #48 from tiktok/tweak-1

chore: final tweak before 1.0
This commit is contained in:
Cheng Liu 2024-03-04 20:31:36 -08:00 committed by GitHub
commit d09c3e87fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 47 additions and 24 deletions

View file

@ -17,7 +17,6 @@ export interface ICloneCommandOptions {
directory?: string;
skipGitConfig?: boolean;
profile?: string[];
addProfile?: string[];
}
@Command()
@ -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.';
@ -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)) {
@ -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) {
@ -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'));

View file

@ -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 (

View file

@ -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();
@ -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) {
@ -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')}`
);
@ -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'

View file

@ -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

View file

@ -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"
}
],

View file

@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "sparo",
"comment": "",
"type": "none"
}
],
"packageName": "sparo"
}