Skip to content

Commit

Permalink
chore: integrate Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd committed Sep 10, 2020
1 parent dfdc434 commit c6303f2
Show file tree
Hide file tree
Showing 50 changed files with 2,054 additions and 890 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"build": "npm run clean && tsc",
"watch": "tsc -w",
"test": "jest --maxWorkers=4",
"lint": "true",
"lint": "npm run prettier -- --check",
"prettier": "prettier \"**/*.ts\"",
"publish:ci": "semantic-release",
"publish:testing": "npm version prerelease --preid=testing --no-git-tag-version && npm publish --tag=testing && git stash",
"prepublishOnly": "npm run build"
Expand Down Expand Up @@ -46,6 +47,7 @@
"yauzl": "^2.10.0"
},
"devDependencies": {
"@ionic/prettier-config": "^1.0.0",
"@semantic-release/changelog": "^5.0.0",
"@semantic-release/git": "^9.0.0",
"@types/debug": "4.1.5",
Expand All @@ -60,10 +62,12 @@
"@types/yauzl": "^2.9.1",
"husky": "^4.0.9",
"jest": "^26.4.2",
"prettier": "^2.1.1",
"semantic-release": "^17.1.1",
"ts-jest": "^26.3.0",
"typescript": "~4.0.2"
},
"prettier": "@ionic/prettier-config",
"release": {
"branches": "stable",
"plugins": [
Expand Down
4 changes: 2 additions & 2 deletions src/android/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export async function run(args: readonly string[]): Promise<void> {
}

if (args.includes('--list')) {
cmd = await import ('./list');
cmd = await import('./list');
return cmd.run(args);
}

if (args.includes('--sdk-info')) {
cmd = await import ('./sdk-info');
cmd = await import('./sdk-info');
return cmd.run(args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/android/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function list(args: readonly string[]): Promise<Targets> {
const sdk = await getSDK();

const errors: Exception<string>[] = [];
const [ devices, virtualDevices ] = await Promise.all([
const [devices, virtualDevices] = await Promise.all([
(async () => {
try {
return await getDeviceTargets(sdk);
Expand Down
85 changes: 67 additions & 18 deletions src/android/run.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import * as Debug from 'debug';

import { AVDException, AndroidRunException, CLIException, ERR_BAD_INPUT, ERR_NO_DEVICE, ERR_NO_TARGET, ERR_TARGET_NOT_FOUND, ERR_UNSUITABLE_API_INSTALLATION } from '../errors';
import {
AVDException,
AndroidRunException,
CLIException,
ERR_BAD_INPUT,
ERR_NO_DEVICE,
ERR_NO_TARGET,
ERR_TARGET_NOT_FOUND,
ERR_UNSUITABLE_API_INSTALLATION,
} from '../errors';
import { getOptionValue, getOptionValues } from '../utils/cli';
import { log } from '../utils/log';
import { onBeforeExit } from '../utils/process';

import { Device, Ports, closeApp, forwardPorts, getDevices, startActivity, unforwardPorts, waitForBoot, waitForClose } from './utils/adb';
import {
Device,
Ports,
closeApp,
forwardPorts,
getDevices,
startActivity,
unforwardPorts,
waitForBoot,
waitForClose,
} from './utils/adb';
import { getApkInfo } from './utils/apk';
import { getInstalledAVDs } from './utils/avd';
import { installApkToDevice, selectDeviceByTarget, selectHardwareDevice, selectVirtualDevice } from './utils/run';
import {
installApkToDevice,
selectDeviceByTarget,
selectHardwareDevice,
selectVirtualDevice,
} from './utils/run';
import { SDK, getSDK } from './utils/sdk';

const modulePrefix = 'native-run:android:run';
Expand All @@ -22,10 +46,12 @@ export async function run(args: readonly string[]): Promise<void> {

if (forwardedPorts && forwardedPorts.length > 0) {
forwardedPorts.forEach((port: string) => {
const [ device, host ] = port.split(':');
const [device, host] = port.split(':');

if (!device || !host) {
throw new CLIException(`Invalid --forward value "${port}": expecting <device port:host port>, e.g. 8080:8080`);
throw new CLIException(
`Invalid --forward value "${port}": expecting <device port:host port>, e.g. 8080:8080`,
);
}

ports.push({ device, host });
Expand All @@ -38,16 +64,22 @@ export async function run(args: readonly string[]): Promise<void> {

const device = await selectDevice(sdk, args);

log(`Selected ${device.type === 'hardware' ? 'hardware device' : 'emulator'} ${device.serial}\n`);
log(
`Selected ${device.type === 'hardware' ? 'hardware device' : 'emulator'} ${
device.serial
}\n`,
);

const { appId, activityName } = await getApkInfo(apkPath);
await waitForBoot(sdk, device);

if (ports) {
await Promise.all(ports.map(async (port: Ports) => {
await forwardPorts(sdk, device, port);
log(`Forwarded device port ${port.device} to host port ${port.host}\n`);
}));
await Promise.all(
ports.map(async (port: Ports) => {
await forwardPorts(sdk, device, port);
log(`Forwarded device port ${port.device} to host port ${port.host}\n`);
}),
);
}

await installApkToDevice(sdk, device, apkPath, appId);
Expand All @@ -59,9 +91,11 @@ export async function run(args: readonly string[]): Promise<void> {

onBeforeExit(async () => {
if (ports) {
await Promise.all(ports.map(async (port: Ports) => {
await unforwardPorts(sdk, device, port);
}));
await Promise.all(
ports.map(async (port: Ports) => {
await unforwardPorts(sdk, device, port);
}),
);
}
});

Expand All @@ -75,7 +109,10 @@ export async function run(args: readonly string[]): Promise<void> {
}
}

export async function selectDevice(sdk: SDK, args: readonly string[]): Promise<Device> {
export async function selectDevice(
sdk: SDK,
args: readonly string[],
): Promise<Device> {
const debug = Debug(`${modulePrefix}:${selectDevice.name}`);

const devices = await getDevices(sdk);
Expand All @@ -90,7 +127,10 @@ export async function selectDevice(sdk: SDK, args: readonly string[]): Promise<D
if (targetDevice) {
return targetDevice;
} else {
throw new AndroidRunException(`Target not found: ${target}`, ERR_TARGET_NOT_FOUND);
throw new AndroidRunException(
`Target not found: ${target}`,
ERR_TARGET_NOT_FOUND,
);
}
}

Expand All @@ -100,7 +140,10 @@ export async function selectDevice(sdk: SDK, args: readonly string[]): Promise<D
if (selectedDevice) {
return selectedDevice;
} else if (args.includes('--device')) {
throw new AndroidRunException(`No hardware devices found. Not attempting emulator because --device was specified.`, ERR_NO_DEVICE);
throw new AndroidRunException(
`No hardware devices found. Not attempting emulator because --device was specified.`,
ERR_NO_DEVICE,
);
} else {
log('No hardware devices found, attempting emulator...\n');
}
Expand All @@ -116,9 +159,15 @@ export async function selectDevice(sdk: SDK, args: readonly string[]): Promise<D
debug('Issue with AVDs: %s', e.message);

if (e.code === ERR_UNSUITABLE_API_INSTALLATION) {
throw new AndroidRunException('No targets devices/emulators available. Cannot create AVD because there is no suitable API installation. Use --sdk-info to reveal missing packages and other issues.', ERR_NO_TARGET);
throw new AndroidRunException(
'No targets devices/emulators available. Cannot create AVD because there is no suitable API installation. Use --sdk-info to reveal missing packages and other issues.',
ERR_NO_TARGET,
);
}
}

throw new AndroidRunException('No target devices/emulators available.', ERR_NO_TARGET);
throw new AndroidRunException(
'No target devices/emulators available.',
ERR_NO_TARGET,
);
}
26 changes: 21 additions & 5 deletions src/android/sdk-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export async function run(args: readonly string[]): Promise<void> {
function formatSDKInfo(sdk: SDKInfo): string {
return `
SDK Location: ${sdk.root}
AVD Home${sdk.avdHome ? `: ${sdk.avdHome}` : ` (!): not found`}
AVD Home${
sdk.avdHome ? `: ${sdk.avdHome}` : ` (!): not found`
}
${sdk.platforms.map(platform => `${formatPlatform(platform)}\n\n`).join('\n')}
Tools:
Expand All @@ -51,11 +53,25 @@ ${sdk.tools.map(tool => formatPackage(tool)).join('\n')}
function formatPlatform(platform: Platform): string {
return `
API Level: ${platform.apiLevel}
Packages: ${platform.packages.map(p => formatPackage(p)).join('\n' + ' '.repeat(22))}
${platform.missingPackages.length > 0 ? `(!) Missing Packages: ${platform.missingPackages.map(p => formatPackage(p)).join('\n' + ' '.repeat(22))}` : ''}
Packages: ${platform.packages
.map(p => formatPackage(p))
.join('\n' + ' '.repeat(22))}
${
platform.missingPackages.length > 0
? `(!) Missing Packages: ${platform.missingPackages
.map(p => formatPackage(p))
.join('\n' + ' '.repeat(22))}`
: ''
}
`.trim();
}

function formatPackage(p: { name: string; path: string; version?: string | RegExp; }): string {
return `${p.name} ${p.path} ${typeof p.version === 'string' ? p.version : ''}`;
function formatPackage(p: {
name: string;
path: string;
version?: string | RegExp;
}): string {
return `${p.name} ${p.path} ${
typeof p.version === 'string' ? p.version : ''
}`;
}
6 changes: 0 additions & 6 deletions src/android/utils/__tests__/adb.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as os from 'os';

describe('android/utils/adb', () => {

describe('parseAdbDevices', () => {

let adbUtils: typeof import('../adb');

beforeEach(async () => {
Expand Down Expand Up @@ -143,7 +141,6 @@ List of devices attached
});

describe('windows', () => {

let adbUtils: typeof import('../adb');

beforeEach(async () => {
Expand Down Expand Up @@ -173,9 +170,6 @@ List of devices attached
},
]);
});

});

});

});
34 changes: 24 additions & 10 deletions src/android/utils/__tests__/avd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as iniUtils from '../../../utils/ini';
import * as avdUtils from '../avd';

describe('android/utils/avd', () => {

describe('getAVDFromINI', () => {

it('should properly parse Pixel_2_API_28', async () => {
const inipath = path.resolve(__dirname, './fixtures/avd/Pixel_2_API_28.ini');
const inipath = path.resolve(
__dirname,
'./fixtures/avd/Pixel_2_API_28.ini',
);
const ini: any = await iniUtils.readINI(inipath);
ini.path = path.resolve(__dirname, './fixtures/avd/Pixel_2_API_28.avd'); // patch path

Expand All @@ -27,9 +28,15 @@ describe('android/utils/avd', () => {
});

it('should properly parse Pixel_2_XL_API_28', async () => {
const inipath = path.resolve(__dirname, './fixtures/avd/Pixel_2_XL_API_28.ini');
const inipath = path.resolve(
__dirname,
'./fixtures/avd/Pixel_2_XL_API_28.ini',
);
const ini: any = await iniUtils.readINI(inipath);
ini.path = path.resolve(__dirname, './fixtures/avd/Pixel_2_XL_API_28.avd'); // patch path
ini.path = path.resolve(
__dirname,
'./fixtures/avd/Pixel_2_XL_API_28.avd',
); // patch path

const expected = {
id: 'Pixel_2_XL_API_28',
Expand All @@ -46,7 +53,10 @@ describe('android/utils/avd', () => {
});

it('should properly parse Pixel_API_25', async () => {
const inipath = path.resolve(__dirname, './fixtures/avd/Pixel_API_25.ini');
const inipath = path.resolve(
__dirname,
'./fixtures/avd/Pixel_API_25.ini',
);
const ini: any = await iniUtils.readINI(inipath);
ini.path = path.resolve(__dirname, './fixtures/avd/Pixel_API_25.avd'); // patch path

Expand All @@ -65,7 +75,10 @@ describe('android/utils/avd', () => {
});

it('should properly parse Nexus_5X_API_24', async () => {
const inipath = path.resolve(__dirname, './fixtures/avd/Nexus_5X_API_24.ini');
const inipath = path.resolve(
__dirname,
'./fixtures/avd/Nexus_5X_API_24.ini',
);
const ini: any = await iniUtils.readINI(inipath);
ini.path = path.resolve(__dirname, './fixtures/avd/Nexus_5X_API_24.avd'); // patch path

Expand All @@ -84,7 +97,10 @@ describe('android/utils/avd', () => {
});

it('should properly parse avdmanager_1', async () => {
const inipath = path.resolve(__dirname, './fixtures/avd/avdmanager_1.ini');
const inipath = path.resolve(
__dirname,
'./fixtures/avd/avdmanager_1.ini',
);
const ini: any = await iniUtils.readINI(inipath);
ini.path = path.resolve(__dirname, './fixtures/avd/avdmanager_1.avd'); // patch path

Expand All @@ -101,7 +117,5 @@ describe('android/utils/avd', () => {
const avd = await avdUtils.getAVDFromINI(inipath, ini);
expect(avd).toEqual(expected);
});

});

});
7 changes: 2 additions & 5 deletions src/android/utils/__tests__/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ OK
`;

describe('android/utils/emulator', () => {

describe('parseAndroidConsoleResponse', () => {

it('should not parse an event from whitespace', () => {
for (const output of ['', '\n', ' \n']) {
const event = parseAndroidConsoleResponse(output);
Expand All @@ -29,11 +27,10 @@ describe('android/utils/emulator', () => {
});

it('should parse response from output', () => {
const expected = authRequiredOutput.split('\n').slice(0, -2).join('\n') + '\n';
const expected =
authRequiredOutput.split('\n').slice(0, -2).join('\n') + '\n';
const event = parseAndroidConsoleResponse(authRequiredOutput);
expect(event).toBe(expected);
});

});

});
Loading

0 comments on commit c6303f2

Please sign in to comment.