From 6656ee3717330fb93c616c0ce8d0d9c154585774 Mon Sep 17 00:00:00 2001 From: Mauricio Saglietto Date: Wed, 4 Jan 2017 19:47:21 -0300 Subject: [PATCH] Add use of reporter lang method (#2269) --- __tests__/util/git.js | 15 +++++++++------ src/cli/commands/check.js | 26 +++++++++++++++----------- src/cli/commands/global.js | 6 +++--- src/package-fetcher.js | 2 +- src/reporters/lang/en.js | 18 ++++++++++++++++++ src/util/git.js | 13 ++++++++----- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/__tests__/util/git.js b/__tests__/util/git.js index 2715727ad4..52ef5b54a5 100644 --- a/__tests__/util/git.js +++ b/__tests__/util/git.js @@ -1,6 +1,7 @@ /* @flow */ import Git from '../../src/util/git.js'; +import {NoopReporter} from '../../src/reporters/index.js'; jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000; @@ -38,39 +39,41 @@ async function toThrow(f): Promise { } xit('secureUrl', async function (): Promise { + const reporter = new NoopReporter(); + expect(await toThrow(() => { - return Git.secureUrl('http://random.repo', ''); + return Git.secureUrl('http://random.repo', '', reporter); }), ).toEqual(true); expect(await toThrow(() => { - return Git.secureUrl('http://random.repo', 'ab_12'); + return Git.secureUrl('http://random.repo', 'ab_12', reporter); }), ).toEqual(true); expect(await toThrow(() => { - return Git.secureUrl('git://random.repo', ''); + return Git.secureUrl('git://random.repo', '', reporter); }), ).toEqual(true); expect(await toThrow(() => { - return Git.secureUrl('https://random.repo', ''); + return Git.secureUrl('https://random.repo', '', reporter); }), ).toEqual(false); expect(await toThrow(() => { - return Git.secureUrl('http://random.repo', 'abc12'); + return Git.secureUrl('http://random.repo', 'abc12', reporter); }), ).toEqual(false); expect(await toThrow(() => { - return Git.secureUrl('git://random.repo', 'abc12'); + return Git.secureUrl('git://random.repo', 'abc12', reporter); }), ).toEqual(false); }, diff --git a/src/cli/commands/check.js b/src/cli/commands/check.js index 9adbec642a..23d37d7369 100644 --- a/src/cli/commands/check.js +++ b/src/cli/commands/check.js @@ -34,8 +34,8 @@ export async function run( let warningCount = 0; let errCount = 0; - function reportError(msg) { - reporter.error(msg); + function reportError(msg, ...vars) { + reporter.error(reporter.lang(msg, ...vars)); errCount++; } @@ -46,7 +46,7 @@ export async function run( // check if patterns exist in lockfile for (const pattern of patterns) { if (!lockfile.getLocked(pattern)) { - reportError(`Lockfile does not contain pattern: ${pattern}`); + reportError('lockfileNotContainPatter', pattern); } } @@ -57,10 +57,10 @@ export async function run( if (integrityLoc && await fs.exists(integrityLoc)) { const match = await install.matchesIntegrityHash(patterns); if (match.matches === false) { - reportError(`Integrity hashes don't match, expected ${match.expected} but got ${match.actual}`); + reportError('integrityHashesDontMatch', match.expected, match.actual); } } else { - reportError("Couldn't find an integrity hash file"); + reportError('noIntegirtyHashFile'); } } else { // check if any of the node_modules are out of sync @@ -99,9 +99,9 @@ export async function run( const pkgLoc = path.join(loc, 'package.json'); if (!(await fs.exists(loc)) || !(await fs.exists(pkgLoc))) { if (pkg._reference.optional) { - reporter.warn(`Optional dependency ${human} not installed`); + reporter.warn(reporter.lang('optionalDepNotInstalled', human)); } else { - reportError(`${human} not installed`); + reportError('packageNotInstalled', human); } continue; } @@ -109,7 +109,7 @@ export async function run( const packageJson = await config.readJson(pkgLoc); if (pkg.version !== packageJson.version) { // node_modules contains wrong version - reportError(`${human} is wrong version: expected ${pkg.version}, got ${packageJson.version}`); + reportError('packageWrongVersion', human, pkg.version, packageJson.version); } const deps = Object.assign({}, packageJson.dependencies, packageJson.peerDependencies); @@ -155,7 +155,7 @@ export async function run( const foundHuman = `${humaniseLocation(path.dirname(depPkgLoc)).join('#')}@${depPkg.version}`; if (!semver.satisfies(depPkg.version, range, config.looseSemver)) { // module isn't correct semver - reportError(`${subHuman} doesn't satisfy found match of ${foundHuman}`); + reportError('packageDontSatisfy', subHuman, foundHuman); continue; } @@ -170,8 +170,12 @@ export async function run( (semver.satisfies(packageJson.version, range, config.looseSemver) && semver.gt(packageJson.version, depPkg.version, config.looseSemver))) { reporter.warn( - `${subHuman} could be deduped from ${packageJson.version} to ` + - `${humaniseLocation(path.dirname(loc)).join('#')}@${packageJson.version}`, + reporter.lang( + 'couldBeDeduped', + subHuman, + packageJson.version, + `${humaniseLocation(path.dirname(loc)).join('#')}@${packageJson.version}`, + ), ); warningCount++; } diff --git a/src/cli/commands/global.js b/src/cli/commands/global.js index ad7f00f49c..a6be839036 100644 --- a/src/cli/commands/global.js +++ b/src/cli/commands/global.js @@ -158,13 +158,13 @@ function ls(manifest: Manifest, reporter: Reporter, saved: boolean) { const human = `${manifest.name}@${manifest.version}`; if (bins.length) { if (saved) { - reporter.success(`Installed ${human} with binaries:`); + reporter.success(reporter.lang('packageInstalledWithBinaries', human)); } else { - reporter.info(`${human} has binaries:`); + reporter.info(reporter.lang('packageHasBinaries', human)); } reporter.list(`bins-${manifest.name}`, bins); } else if (saved) { - reporter.warn(`${human} has no binaries`); + reporter.warn(reporter.lang('packageHasNoBinaries')); } } diff --git a/src/package-fetcher.js b/src/package-fetcher.js index 366202b1da..3c5f19ed57 100644 --- a/src/package-fetcher.js +++ b/src/package-fetcher.js @@ -39,7 +39,7 @@ export default class PackageFetcher { const remote = ref.remote; const Fetcher = fetchers[remote.type]; if (!Fetcher) { - throw new MessageError(`Unknown fetcher for ${remote.type}`); + throw new MessageError(this.reporter.lang('unknownFetcherFor', remote.type)); } const fetcher = new Fetcher(dest, remote, this.config); diff --git a/src/reporters/lang/en.js b/src/reporters/lang/en.js index f820d0ba8c..4b0c7ae80b 100644 --- a/src/reporters/lang/en.js +++ b/src/reporters/lang/en.js @@ -238,6 +238,24 @@ const messages = { errorDecompressingTarball: '$0. Error decompressing $1, it appears to be corrupt.', updateInstalling: 'Installing $0...', hostedGitResolveError: 'Error connecting to repository. Please, check the url.', + + unknownFetcherFor: 'Unknown fetcher for $0', + + refusingDownloadGitWithoutCommit: 'Refusing to download the git repo $0 over plain git without a commit hash', + refusingDownloadHTTPWithoutCommit: 'Refusing to download the git repo $0 over HTTP without a commit hash', + + packageInstalledWithBinaries: 'Installed $0 with binaries:', + packageHasBinaries: '$0 has binaries:', + packageHasNoBinaries: '$0 has no binaries', + + couldBeDeduped: '$0 could be deduped from $1 to $2', + lockfileNotContainPatter: 'Lockfile does not contain pattern: $0', + integrityHashesDontMatch: 'Integrity hashes don\'t match, expected $0 but got $1', + noIntegirtyHashFile: 'Couldn\'t find an integrity hash file', + packageNotInstalled: '$0 not installed', + optionalDepNotInstalled: 'Optional dependency $0 not installed', + packageWrongVersion: '$0 is wrong version: expected $1, got $2', + packageDontSatisfy: '$0 doesn\'t satisfy found match of $1', }; export type LanguageKeys = $Keys; diff --git a/src/util/git.js b/src/util/git.js index da09d21cb7..495206cdf9 100644 --- a/src/util/git.js +++ b/src/util/git.js @@ -1,6 +1,7 @@ /* @flow */ import type Config from '../config.js'; +import type {Reporter} from '../reporters/index.js'; import {MessageError, SecurityError} from '../errors.js'; import {removeSuffix} from './misc.js'; import * as crypto from './crypto.js'; @@ -27,6 +28,7 @@ export default class Git { this.supportsArchive = false; this.fetched = false; this.config = config; + this.reporter = config.reporter; this.hash = hash; this.ref = hash; this.url = Git.cleanUrl(url); @@ -36,6 +38,7 @@ export default class Git { supportsArchive: boolean; fetched: boolean; config: Config; + reporter: Reporter; hash: string; ref: string; cwd: string; @@ -93,7 +96,7 @@ export default class Git { * attempt to upgrade unsecure protocols to securl protocol */ - static async secureUrl(ref: string, hash: string): Promise { + static async secureUrl(ref: string, hash: string, reporter: Reporter): Promise { if (Git.isCommitHash(hash)) { // this is cryptographically secure return ref; @@ -107,7 +110,7 @@ export default class Git { return secureUrl; } else { throw new SecurityError( - `Refusing to download the git repo ${ref} over plain git without a commit hash`, + reporter.lang('refusingDownloadGitWithoutCommit', ref), ); } } @@ -118,7 +121,7 @@ export default class Git { return secureUrl; } else { throw new SecurityError( - `Refusing to download the git repo ${ref} over HTTP without a commit hash`, + reporter.lang('refusingDownloadHTTPWithoutCommit', ref), ); } } @@ -299,7 +302,7 @@ export default class Git { * set the ref to match an input `target`. */ async init(): Promise { - this.url = await Git.secureUrl(this.url, this.hash); + this.url = await Git.secureUrl(this.url, this.hash, this.reporter); // check capabilities if (await Git.hasArchiveCapability(this.url)) { this.supportsArchive = true; @@ -345,7 +348,7 @@ export default class Git { this.ref = ref; return this.hash = commit; } else { - throw new MessageError(this.config.reporter.lang('couldntFindMatch', ref, names.join(','), this.url)); + throw new MessageError(this.reporter.lang('couldntFindMatch', ref, names.join(','), this.url)); } }