2024-02-21 00:42:28 -05:00
|
|
|
import { FileSystem, Async } from '@rushstack/node-core-library';
|
|
|
|
import { inject } from 'inversify';
|
|
|
|
import { Service } from '../decorator';
|
|
|
|
import { SparseProfile } from '../logic/SparseProfile';
|
2024-02-21 15:28:50 -05:00
|
|
|
import { TerminalService } from './TerminalService';
|
2024-02-20 20:25:44 -05:00
|
|
|
import { GitService } from './GitService';
|
2024-02-21 00:42:28 -05:00
|
|
|
|
|
|
|
export interface ISparseProfileServiceParams {
|
2024-02-21 15:28:50 -05:00
|
|
|
terminalService: TerminalService;
|
2024-02-21 00:42:28 -05:00
|
|
|
sparseProfileFolder: string;
|
|
|
|
}
|
|
|
|
const defaultSparseProfileFolder: string = 'common/sparse-profiles';
|
|
|
|
|
|
|
|
@Service()
|
|
|
|
export class SparseProfileService {
|
|
|
|
public _profiles: Map<string, SparseProfile> = new Map<string, SparseProfile>();
|
|
|
|
private _loadPromise: Promise<void> | undefined;
|
2024-02-20 20:25:44 -05:00
|
|
|
|
|
|
|
@inject(GitService) private _gitService!: GitService;
|
2024-02-21 15:28:50 -05:00
|
|
|
@inject(TerminalService) private _terminalService!: TerminalService;
|
2024-02-21 00:42:28 -05:00
|
|
|
|
|
|
|
public async loadProfilesAsync(): Promise<void> {
|
|
|
|
if (!this._loadPromise) {
|
|
|
|
this._loadPromise = (async () => {
|
|
|
|
const sparseProfileFolder: string = defaultSparseProfileFolder;
|
|
|
|
const sparseProfilePaths: string[] = await FileSystem.readFolderItemNamesAsync(sparseProfileFolder, {
|
|
|
|
absolutePaths: true
|
|
|
|
});
|
|
|
|
|
|
|
|
await Async.forEachAsync(sparseProfilePaths, async (sparseProfilePath: string) => {
|
|
|
|
let sparseProfile: SparseProfile | undefined;
|
|
|
|
try {
|
2024-02-21 15:28:50 -05:00
|
|
|
sparseProfile = await SparseProfile.loadFromFileAsync(this._terminalService, sparseProfilePath);
|
2024-02-21 00:42:28 -05:00
|
|
|
} catch (e) {
|
|
|
|
// TODO: more error handling
|
2024-02-21 15:28:50 -05:00
|
|
|
this._terminalService.terminal.writeLine((e as Error).message);
|
2024-02-21 00:42:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sparseProfile) {
|
|
|
|
const profileName: string = SparseProfileService._getProfileName(sparseProfilePath);
|
2024-02-21 15:28:50 -05:00
|
|
|
this._terminalService.terminal.writeDebugLine(
|
|
|
|
`load sparse profile ${profileName} from ${sparseProfilePath}`
|
|
|
|
);
|
2024-02-21 00:42:28 -05:00
|
|
|
this._profiles.set(profileName, sparseProfile);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
return this._loadPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getProfileAsync(name: string): Promise<SparseProfile | undefined> {
|
|
|
|
await this.loadProfilesAsync();
|
|
|
|
return this._profiles.get(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getProfilesAsync(): Promise<Map<string, SparseProfile>> {
|
|
|
|
await this.loadProfilesAsync();
|
|
|
|
return this._profiles;
|
|
|
|
}
|
|
|
|
|
2024-02-20 20:25:44 -05:00
|
|
|
public hasProfile(name: string, branch: string): boolean {
|
|
|
|
return this._gitService.hasFile(`${defaultSparseProfileFolder}/${name}.json`, branch);
|
|
|
|
}
|
|
|
|
|
2024-02-21 00:42:28 -05:00
|
|
|
private static _getProfileName(profilePath: string): string {
|
|
|
|
const pathArr: string[] = profilePath.split('/');
|
|
|
|
const last: string = pathArr[pathArr.length - 1];
|
|
|
|
if (last.endsWith('.json')) {
|
|
|
|
return last.slice(0, -5);
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
}
|