Skip to content

Commit

Permalink
refactor(log): prefer util.format vs string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Apr 15, 2020
1 parent 499d066 commit 4ca2da7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function parseAdaptiveIconBackgroundOptions(resourcesDirectory: string, a

export function parseSimpleResourceOptions(platform: Platform, type: ResourceType.ICON | ResourceType.SPLASH, resourcesDirectory: string, args: readonly string[]): SimpleResourceOptions {
const source = parseSourceFromArgs(type, args);

return { sources: source ? [source] : getDefaultSources(platform, type, resourcesDirectory) };
}

Expand Down
3 changes: 2 additions & 1 deletion src/cordova/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ensureDir, readFile, writeFile } from '@ionic/utils-fs';
import Debug from 'debug';
import et from 'elementtree';
import pathlib from 'path';
import util from 'util';

import { BadInputError } from '../error';
import { GeneratedResource, Platform } from '../platform';
Expand Down Expand Up @@ -78,7 +79,7 @@ export function runConfig(configPath: string, doc: et.ElementTree, resources: re
const orientation = orientationPreference || 'default';

if (orientation !== 'default' && errstream) {
errstream.write(`WARN: Orientation preference set to '${orientation}'. Only configuring ${orientation} resources.\n`);
errstream.write(util.format(`WARN: Orientation preference set to '%s'. Only configuring %s resources.`, orientation, orientation) + '\n');
}

const platforms = groupImages(resources);
Expand Down
5 changes: 2 additions & 3 deletions src/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export function debugSourceImage(src: string, error: NodeJS.ErrnoException, errs
debug('Source file missing: %s', src);
} else {
if (errstream) {
const message = util.format('WARN: Error with source file %s: %s', src, error);
errstream.write(`${message}\n`);
errstream.write(util.format('WARN: Error with source file %s: %s', src, error) + '\n');
} else {
debug('Error with source file %s: %O', src, error);
}
Expand All @@ -78,7 +77,7 @@ export async function generateImage(image: ImageSchema, src: Sharp, metadata: Me

if (errstream) {
if (metadata.format !== image.format) {
errstream.write(`WARN: Must perform conversion from ${metadata.format} to png.\n`);
errstream.write(util.format(`WARN: Must perform conversion from %s to png.`, metadata.format) + '\n');
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { pathWritable } from '@ionic/utils-fs';
import Debug from 'debug';
import et from 'elementtree';
import path from 'path';
import util from 'util';

import { getDirectory, parseOptions, resolveOptions } from './cli';
import { getConfigPath, read as readConfig, run as runConfig, write as writeConfig } from './cordova/config';
import { BaseError } from './error';
import { NativeProjectConfig, copyToNativeProject } from './native';
import { GeneratedResource, PLATFORMS, Platform, RunPlatformOptions, run as runPlatform } from './platform';
import { GeneratedResource, PLATFORMS, Platform, RunPlatformOptions, prettyPlatform, run as runPlatform } from './platform';
import { Density, Orientation, ResolvedSource, SourceType } from './resources';
import { tryFn } from './utils/fn';

Expand Down Expand Up @@ -74,7 +75,7 @@ async function CordovaRes(options: CordovaRes.Options = {}): Promise<Result> {
if (platformOptions) {
const platformResult = await runPlatform(platform, resourcesDirectory, platformOptions, errstream);

logstream.write(`Generated ${platformResult.resources.length} resources for ${platform}\n`);
logstream.write(util.format(`Generated %s resources for %s`, platformResult.resources.length, prettyPlatform(platform)) + '\n');

resources.push(...platformResult.resources);
sources.push(...platformResult.sources);
Expand Down
9 changes: 5 additions & 4 deletions src/native.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { copy } from '@ionic/utils-fs';
import Debug from 'debug';
import path from 'path';
import util from 'util';

import { Platform } from './platform';
import { Platform, prettyPlatform } from './platform';

export interface NativeProjectConfig {
directory: string;
Expand Down Expand Up @@ -184,15 +185,15 @@ export async function copyToNativeProject(platform: Platform, nativeProject: Nat
const iosProjectDirectory = nativeProject.directory || 'ios';
await copyImages(SOURCE_IOS_ICON, path.join(iosProjectDirectory, TARGET_IOS_ICON), IOS_ICONS);
await copyImages(SOURCE_IOS_SPLASH, path.join(iosProjectDirectory, TARGET_IOS_SPLASH), IOS_SPLASHES);
logstream.write(`Copied ${IOS_ICONS.length + IOS_SPLASHES.length} resource items to ${platform}\n`);
logstream.write(util.format(`Copied %s resource items to %s`, IOS_ICONS.length + IOS_SPLASHES.length, prettyPlatform(platform)) + '\n');
} else if (platform === Platform.ANDROID) {
const androidProjectDirectory = nativeProject.directory || 'android';
await copyImages(SOURCE_ANDROID_ICON, path.join(androidProjectDirectory, TARGET_ANDROID_ICON), ANDROID_ICONS);
await copyImages(SOURCE_ANDROID_SPLASH, path.join(androidProjectDirectory, TARGET_ANDROID_SPLASH), ANDROID_SPLASHES);
logstream.write(`Copied ${ANDROID_ICONS.length + ANDROID_SPLASHES.length} resource items to ${platform}\n`);
logstream.write(util.format(`Copied %s resource items to %s`, ANDROID_ICONS.length + ANDROID_SPLASHES.length, prettyPlatform(platform)) + '\n');
} else {
if (errstream) {
errstream.write(`WARN: Copying to native projects is not supported for ${platform}\n`);
errstream.write(util.format('WARN: Copying to native projects is not supported for %s', prettyPlatform(platform)) + '\n');
}
}
}
11 changes: 11 additions & 0 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,14 @@ export function filterSupportedPlatforms(platforms: readonly string[]): Platform
export function isSupportedPlatform(platform: any): platform is Platform {
return PLATFORMS.includes(platform);
}

export function prettyPlatform(platform: Platform): string {
switch (platform) {
case Platform.IOS:
return 'iOS';
case Platform.ANDROID:
return 'Android';
case Platform.WINDOWS:
return 'Windows';
}
}

0 comments on commit 4ca2da7

Please sign in to comment.