diff --git a/src/modules/package-downloader.ts b/src/modules/package-downloader.ts index 31f40f0..aec003c 100644 --- a/src/modules/package-downloader.ts +++ b/src/modules/package-downloader.ts @@ -26,8 +26,11 @@ export class PackageDownloader { this.packageConfig = packageConfig; } - private async _cloneRepository(packageDir: string, cloneOpts: string[]): Promise { - await this.git.clone(this.packageConfig.getPackageRepo(), packageDir, cloneOpts); + private async _cloneRepository(packageDir: string, cloneOpts: string[], verbose?: boolean): Promise { + const response = await this.git.clone(this.packageConfig.getPackageRepo(), packageDir, cloneOpts); + if (verbose) { + console.log(`Clone response: ${response}`); + } } private async _updateRepository(checkRepoAction: CheckRepoActions, verbose: boolean): Promise { @@ -49,7 +52,12 @@ export class PackageDownloader { await this.git.checkout(branchOrTag); } - private async _checkForValidRepo(packageDir: string, cloneOpts: string[], checkRepoAction: CheckRepoActions): Promise { + private async _checkForValidRepo( + packageDir: string, + cloneOpts: string[], + checkRepoAction: CheckRepoActions, + verbose?: boolean, + ): Promise { let directoryExists = CliFileSystem.existsSync(packageDir); if (directoryExists && !(await this.isValidRepo(packageDir, checkRepoAction))) { CliFileSystem.removeSync(packageDir); @@ -57,7 +65,22 @@ export class PackageDownloader { } if (!directoryExists) { - await this._cloneRepository(packageDir, cloneOpts); + try { + // simple-git typically throws an error if clone fails + // but not all errors reult in an exception, for instance blocked clone due to rate limitation + // does not result in an exception + await this._cloneRepository(packageDir, cloneOpts, verbose); + } catch (error) { + if (verbose) { + console.error(error); + } + throw new Error(`Cloning of ${this.packageConfig.getPackageRepo()} failed!`); + } + + // Do a second check to verify if clone seems to have succeeded + if (!(await this.isValidRepo(packageDir, checkRepoAction))) { + throw new Error(`Problem detected when cloning ${this.packageConfig.getPackageRepo()}!`); + } } } @@ -80,7 +103,7 @@ export class PackageDownloader { checkRepoAction = CheckRepoActions.IS_REPO_ROOT; } - await this._checkForValidRepo(packageDir, cloneOpts, checkRepoAction); + await this._checkForValidRepo(packageDir, cloneOpts, checkRepoAction, verbose); this.git = simpleGit(packageDir); await this._updateRepository(checkRepoAction, verbose); diff --git a/src/modules/package.ts b/src/modules/package.ts index f829a89..8f077f2 100644 --- a/src/modules/package.ts +++ b/src/modules/package.ts @@ -93,29 +93,33 @@ export class PackageConfig { } async getPackageVersions(verbose?: boolean): Promise { - try { - const packageInformation = await packageDownloader(this).downloadPackage({ checkVersionOnly: true, verbose: verbose }); - const packageVersionTags = await packageInformation.tags(); - return packageVersionTags; + const packageInformation = await packageDownloader(this).downloadPackage({ checkVersionOnly: true, verbose: verbose }); + const packageVersionTags = await packageInformation.tags(); + return packageVersionTags; + /* } catch (error) { + console.log(`vvv`); console.log(error); + console.log(`www`); } return { all: [], latest: '', - }; + };*/ } async downloadPackageVersion(verbose?: boolean): Promise { try { await packageDownloader(this).downloadPackage({ verbose: verbose }); } catch (error) { - console.error(error); + if (verbose) { + console.error(error); + } // If repo exist but not version we will end up with default-version // of that repo, and on subsequent runs tooling will say that the missing version // actually exists! To prevent this we remove the directory of the missing version! CliFileSystem.removeSync(this.getPackageDirectoryWithVersion()); - throw new Error(`Cannot find package ${this.getPackageName()}:${this.version}`); + throw new Error(`Downloading package ${this.getPackageName()}:${this.version} failed!`); } return; } diff --git a/src/modules/projectConfig/projectConfigFileReader.ts b/src/modules/projectConfig/projectConfigFileReader.ts index e750bd0..aa05a23 100644 --- a/src/modules/projectConfig/projectConfigFileReader.ts +++ b/src/modules/projectConfig/projectConfigFileReader.ts @@ -46,18 +46,21 @@ export class MultiFormatConfigReader implements IProjectConfigReader { let config: ProjectConfig | null = null; for (const reader of projectConfigReaders) { + let first: boolean = true; try { config = reader.read(cliVersion, path, ignoreLock); if (config !== null) { break; } } catch (error: any) { - console.warn(`Warning: ${path} not in expected format: ${error.message}, falling back to legacy format reading.`); + // This could be format error, but it could also be that repo/tag-settings are faulty, repo cannot be reached and + // similar, for example that "velocitas init" did not succeed! + console.warn(`Error reported by config reader ${reader.constructor.name}: ${error.message}`); } } if (config === null) { - throw new Error(`Unable to read ${path}: unknown format!`); + throw new Error(`Unable to successfully read and interpret ${path}!`); } return config; diff --git a/src/modules/projectConfig/projectConfigLock.ts b/src/modules/projectConfig/projectConfigLock.ts index a70854d..42ec144 100644 --- a/src/modules/projectConfig/projectConfigLock.ts +++ b/src/modules/projectConfig/projectConfigLock.ts @@ -32,7 +32,7 @@ export class ProjectConfigLock implements ProjectConfigLockAttributes { public findVersion(packageName: string): string { const packageVersion = this.packages.get(packageName); if (!packageVersion) { - throw new Error(`Package '${packageName}' not found in lock file.`); + throw new Error(`Package '${packageName}' not found in lock file. Have you performed "velocitas init"?`); } return packageVersion; } diff --git a/test/commands/init/init.test.ts b/test/commands/init/init.test.ts index 83878a4..9518465 100644 --- a/test/commands/init/init.test.ts +++ b/test/commands/init/init.test.ts @@ -100,13 +100,17 @@ describe('init', () => { .command(['init', '-v']) .it('downloads corrupted packages again', (ctx) => { expect(ctx.stdout).to.contain('Initializing Velocitas packages ...'); + expect(ctx.stdout).to.contain( - `... Corrupted .git directory found for: '${corePackageInfoMock.repo}:${corePackageInfoMock.resolvedVersion}'`, + `... Corrupted .git directory found for: '${runtimePackageInfoMock.repo}:${runtimePackageInfoMock.resolvedVersion}'`, + `Some error {ctx.stdout}`, + ); + expect(ctx.stdout).to.contain( + `... Downloading package: '${runtimePackageInfoMock.repo}:${runtimePackageInfoMock.resolvedVersion}'`, ); - expect(ctx.stdout).to.contain(`... Downloading package: '${corePackageInfoMock.repo}:${corePackageInfoMock.resolvedVersion}'`); expect( CliFileSystem.existsSync( - `${userHomeDir}/.velocitas/packages/${corePackageInfoMock.repo}/${corePackageInfoMock.resolvedVersion}`, + `${userHomeDir}/.velocitas/packages/${runtimePackageInfoMock.repo}/${runtimePackageInfoMock.resolvedVersion}`, ), ).to.be.true; }); diff --git a/test/helpers/simpleGit.ts b/test/helpers/simpleGit.ts index 64aaf97..81dbb73 100644 --- a/test/helpers/simpleGit.ts +++ b/test/helpers/simpleGit.ts @@ -30,6 +30,9 @@ export const simpleGitInstanceMock = (mockedNewVersionTag?: string, checkRepo: b } }, checkIsRepo: () => { + let result = checkRepo; + // By default let second attempt succeed + checkRepo = true; return checkRepo; }, fetch: () => {}, diff --git a/test/unit/projectConfigIO.test.ts b/test/unit/projectConfigIO.test.ts index 840990e..ed7e5c0 100644 --- a/test/unit/projectConfigIO.test.ts +++ b/test/unit/projectConfigIO.test.ts @@ -221,7 +221,7 @@ describe('projectConfigIO - module', () => { it('should handle errors when parsing .velocitas.json file', () => { const readFileStub = sinon.stub(CliFileSystem, 'readFileSync').throws(); const projectConfigFileReader = () => ProjectConfigIO.read('', configFilePath, true); - expect(projectConfigFileReader).to.throw(`Unable to read ${configFilePath}: unknown format!`); + expect(projectConfigFileReader).to.throw(`Unable to successfully read and interpret ${configFilePath}`); readFileStub.restore(); });