Skip to content

Commit 4ca2da7

Browse files
committed
refactor(log): prefer util.format vs string interpolation
1 parent 499d066 commit 4ca2da7

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

src/cli.ts

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export function parseAdaptiveIconBackgroundOptions(resourcesDirectory: string, a
119119

120120
export function parseSimpleResourceOptions(platform: Platform, type: ResourceType.ICON | ResourceType.SPLASH, resourcesDirectory: string, args: readonly string[]): SimpleResourceOptions {
121121
const source = parseSourceFromArgs(type, args);
122+
122123
return { sources: source ? [source] : getDefaultSources(platform, type, resourcesDirectory) };
123124
}
124125

src/cordova/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ensureDir, readFile, writeFile } from '@ionic/utils-fs';
22
import Debug from 'debug';
33
import et from 'elementtree';
44
import pathlib from 'path';
5+
import util from 'util';
56

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

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

8485
const platforms = groupImages(resources);

src/image.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ export function debugSourceImage(src: string, error: NodeJS.ErrnoException, errs
5353
debug('Source file missing: %s', src);
5454
} else {
5555
if (errstream) {
56-
const message = util.format('WARN: Error with source file %s: %s', src, error);
57-
errstream.write(`${message}\n`);
56+
errstream.write(util.format('WARN: Error with source file %s: %s', src, error) + '\n');
5857
} else {
5958
debug('Error with source file %s: %O', src, error);
6059
}
@@ -78,7 +77,7 @@ export async function generateImage(image: ImageSchema, src: Sharp, metadata: Me
7877

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

src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { pathWritable } from '@ionic/utils-fs';
22
import Debug from 'debug';
33
import et from 'elementtree';
44
import path from 'path';
5+
import util from 'util';
56

67
import { getDirectory, parseOptions, resolveOptions } from './cli';
78
import { getConfigPath, read as readConfig, run as runConfig, write as writeConfig } from './cordova/config';
89
import { BaseError } from './error';
910
import { NativeProjectConfig, copyToNativeProject } from './native';
10-
import { GeneratedResource, PLATFORMS, Platform, RunPlatformOptions, run as runPlatform } from './platform';
11+
import { GeneratedResource, PLATFORMS, Platform, RunPlatformOptions, prettyPlatform, run as runPlatform } from './platform';
1112
import { Density, Orientation, ResolvedSource, SourceType } from './resources';
1213
import { tryFn } from './utils/fn';
1314

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

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

7980
resources.push(...platformResult.resources);
8081
sources.push(...platformResult.sources);

src/native.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { copy } from '@ionic/utils-fs';
22
import Debug from 'debug';
33
import path from 'path';
4+
import util from 'util';
45

5-
import { Platform } from './platform';
6+
import { Platform, prettyPlatform } from './platform';
67

78
export interface NativeProjectConfig {
89
directory: string;
@@ -184,15 +185,15 @@ export async function copyToNativeProject(platform: Platform, nativeProject: Nat
184185
const iosProjectDirectory = nativeProject.directory || 'ios';
185186
await copyImages(SOURCE_IOS_ICON, path.join(iosProjectDirectory, TARGET_IOS_ICON), IOS_ICONS);
186187
await copyImages(SOURCE_IOS_SPLASH, path.join(iosProjectDirectory, TARGET_IOS_SPLASH), IOS_SPLASHES);
187-
logstream.write(`Copied ${IOS_ICONS.length + IOS_SPLASHES.length} resource items to ${platform}\n`);
188+
logstream.write(util.format(`Copied %s resource items to %s`, IOS_ICONS.length + IOS_SPLASHES.length, prettyPlatform(platform)) + '\n');
188189
} else if (platform === Platform.ANDROID) {
189190
const androidProjectDirectory = nativeProject.directory || 'android';
190191
await copyImages(SOURCE_ANDROID_ICON, path.join(androidProjectDirectory, TARGET_ANDROID_ICON), ANDROID_ICONS);
191192
await copyImages(SOURCE_ANDROID_SPLASH, path.join(androidProjectDirectory, TARGET_ANDROID_SPLASH), ANDROID_SPLASHES);
192-
logstream.write(`Copied ${ANDROID_ICONS.length + ANDROID_SPLASHES.length} resource items to ${platform}\n`);
193+
logstream.write(util.format(`Copied %s resource items to %s`, ANDROID_ICONS.length + ANDROID_SPLASHES.length, prettyPlatform(platform)) + '\n');
193194
} else {
194195
if (errstream) {
195-
errstream.write(`WARN: Copying to native projects is not supported for ${platform}\n`);
196+
errstream.write(util.format('WARN: Copying to native projects is not supported for %s', prettyPlatform(platform)) + '\n');
196197
}
197198
}
198199
}

src/platform.ts

+11
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,14 @@ export function filterSupportedPlatforms(platforms: readonly string[]): Platform
352352
export function isSupportedPlatform(platform: any): platform is Platform {
353353
return PLATFORMS.includes(platform);
354354
}
355+
356+
export function prettyPlatform(platform: Platform): string {
357+
switch (platform) {
358+
case Platform.IOS:
359+
return 'iOS';
360+
case Platform.ANDROID:
361+
return 'Android';
362+
case Platform.WINDOWS:
363+
return 'Windows';
364+
}
365+
}

0 commit comments

Comments
 (0)