Skip to content

Commit

Permalink
Add use of reporter lang method (#2269)
Browse files Browse the repository at this point in the history
  • Loading branch information
msaglietto authored and bestander committed Jan 4, 2017
1 parent a9eb571 commit 6656ee3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
15 changes: 9 additions & 6 deletions __tests__/util/git.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import Git from '../../src/util/git.js';
import {NoopReporter} from '../../src/reporters/index.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;

Expand Down Expand Up @@ -38,39 +39,41 @@ async function toThrow(f): Promise <boolean> {
}

xit('secureUrl', async function (): Promise<void> {
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);
},
Expand Down
26 changes: 15 additions & 11 deletions src/cli/commands/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

Expand All @@ -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);
}
}

Expand All @@ -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
Expand Down Expand Up @@ -99,17 +99,17 @@ 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;
}

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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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++;
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/commands/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/package-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof messages>;
Expand Down
13 changes: 8 additions & 5 deletions src/util/git.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -36,6 +38,7 @@ export default class Git {
supportsArchive: boolean;
fetched: boolean;
config: Config;
reporter: Reporter;
hash: string;
ref: string;
cwd: string;
Expand Down Expand Up @@ -93,7 +96,7 @@ export default class Git {
* attempt to upgrade unsecure protocols to securl protocol
*/

static async secureUrl(ref: string, hash: string): Promise<string> {
static async secureUrl(ref: string, hash: string, reporter: Reporter): Promise<string> {
if (Git.isCommitHash(hash)) {
// this is cryptographically secure
return ref;
Expand All @@ -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),
);
}
}
Expand All @@ -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),
);
}
}
Expand Down Expand Up @@ -299,7 +302,7 @@ export default class Git {
* set the ref to match an input `target`.
*/
async init(): Promise<string> {
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;
Expand Down Expand Up @@ -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));
}
}

Expand Down

0 comments on commit 6656ee3

Please sign in to comment.