Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify code and clear TODO #1649

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions src/packager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { createPlatformArchPairs, osModules, validateListFromOptions } from './targets';
import { extractElectronZip } from './unzip';
import { packageUniversalMac } from './universal';
import { ComboOptions, DownloadOptions, OfficialPlatform, Options, SupportedArch, SupportedPlatform } from './types';
import { ApplicationBundlePath, ComboOptions, DownloadOptions, OfficialPlatform, Options, SupportedArch, SupportedPlatform } from './types';
import { App } from './platform';

function debugHostInfo() {
Expand All @@ -35,7 +35,10 @@
}
}

async testSymlink(comboOpts: ComboOptions, zipPath: string) {
/**
* Returns `true` if symlink creation is supported, `false` otherwise.
*/
async testSymlink(comboOpts: ComboOptions): Promise<boolean> {
await fs.mkdirp(this.tempBase);
const testPath = await fs.mkdtemp(path.join(this.tempBase, `symlink-test-${comboOpts.platform}-${comboOpts.arch}-`));
const testFile = path.join(testPath, 'test');
Expand All @@ -52,24 +55,13 @@
await fs.remove(testPath);
}

if (this.canCreateSymlinks) {
return this.checkOverwrite(comboOpts, zipPath);
}

/* istanbul ignore next */
return this.skipHostPlatformSansSymlinkSupport(comboOpts);
return this.canCreateSymlinks;
}

/* istanbul ignore next */
skipHostPlatformSansSymlinkSupport(comboOpts: ComboOptions) {
skipHostPlatformSansSymlinkSupport(comboOpts: ComboOptions): ApplicationBundlePath {
info(`Cannot create symlinks (on Windows hosts, it requires admin privileges); skipping ${comboOpts.platform} platform`, this.opts.quiet);
return Promise.resolve();
}

async overwriteAndCreateApp(outDir: string, comboOpts: ComboOptions, zipPath: string) {
debug(`Removing ${outDir} due to setting overwrite: true`);
await fs.remove(outDir);
return this.createApp(comboOpts, zipPath);
return '';
}

async extractElectronZip(comboOpts: ComboOptions, zipPath: string, buildDir: string) {
Expand Down Expand Up @@ -106,14 +98,16 @@
return app.create();
}

async checkOverwrite(comboOpts: ComboOptions, zipPath: string) {
async checkOverwrite(comboOpts: ComboOptions, zipPath: string): Promise<ApplicationBundlePath> {
const finalPath = generateFinalPath(comboOpts);
if (await fs.pathExists(finalPath)) {
if (this.opts.overwrite) {
return this.overwriteAndCreateApp(finalPath, comboOpts, zipPath);
debug(`Removing ${finalPath} due to setting overwrite: true`);
await fs.remove(finalPath);
return this.createApp(comboOpts, zipPath);
} else {
info(`Skipping ${comboOpts.platform} ${comboOpts.arch} (output dir already exists, use --overwrite to force)`, this.opts.quiet);
return true;
return '';
}
} else {
return this.createApp(comboOpts, zipPath);
Expand All @@ -140,26 +134,32 @@
}
}

async packageForPlatformAndArchWithOpts(comboOpts: ComboOptions, downloadOpts: DownloadOptions) {
async packageForPlatformAndArchWithOpts(comboOpts: ComboOptions, downloadOpts: DownloadOptions): Promise<ApplicationBundlePath> {
const zipPath = await this.getElectronZipPath(downloadOpts);

if (!this.useTempDir) {
return this.createApp(comboOpts, zipPath);
}

let skipHostPlatform = false;

if (isPlatformMac(comboOpts.platform)) {
/* istanbul ignore else */
if (this.canCreateSymlinks === undefined) {
return this.testSymlink(comboOpts, zipPath);
if (this.canCreateSymlinks === undefined && !(await this.testSymlink(comboOpts))) {
skipHostPlatform = true;

Check warning on line 149 in src/packager.ts

View check run for this annotation

Codecov / codecov/patch

src/packager.ts#L149

Added line #L149 was not covered by tests
} else if (!this.canCreateSymlinks) {
return this.skipHostPlatformSansSymlinkSupport(comboOpts);
skipHostPlatform = true;
}
}

if (skipHostPlatform) {
return this.skipHostPlatformSansSymlinkSupport(comboOpts);

Check warning on line 156 in src/packager.ts

View check run for this annotation

Codecov / codecov/patch

src/packager.ts#L156

Added line #L156 was not covered by tests
}

return this.checkOverwrite(comboOpts, zipPath);
}

async packageForPlatformAndArch(downloadOpts: DownloadOptions) {
async packageForPlatformAndArch(downloadOpts: DownloadOptions): Promise<ApplicationBundlePath> {
// Create delegated options object with specific platform and arch, for output directory naming
const comboOpts: ComboOptions = {
...this.opts,
Expand All @@ -176,7 +176,7 @@
}
}

async function packageAllSpecifiedCombos(opts: Options, archs: SupportedArch[], platforms: SupportedPlatform[]) {
async function packageAllSpecifiedCombos(opts: Options, archs: SupportedArch[], platforms: SupportedPlatform[]): Promise<ApplicationBundlePath[]> {
const packager = new Packager(opts);
await packager.ensureTempDir();
return Promise.all(createDownloadCombos(opts, platforms, archs).map(
Expand Down Expand Up @@ -245,5 +245,5 @@
]);
const appPaths = await packageAllSpecifiedCombos(opts, archs, platforms);
// Remove falsy entries (e.g. skipped platforms)
return appPaths.filter(appPath => appPath && typeof appPath === 'string') as string[];
return appPaths.filter(Boolean);
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,8 @@ export interface ComboOptions extends Options {
arch: OptionsWithRequiredArchAndPlatform['arch']
platform: OptionsWithRequiredArchAndPlatform['platform']
}

/**
* Path to an application bundle, or an empty string in case the bundling was skipped for some reason.
*/
export type ApplicationBundlePath = string
9 changes: 4 additions & 5 deletions src/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import fs from 'fs-extra';
import path from 'path';
import { App } from './mac';
import { ComboOptions, DownloadOptions, SupportedArch } from './types';
import { ApplicationBundlePath, ComboOptions, DownloadOptions, SupportedArch } from './types';
import { Packager } from './packager';

export async function packageUniversalMac(packageForPlatformAndArchWithOpts: Packager['packageForPlatformAndArchWithOpts'],
buildDir: string, comboOpts: ComboOptions,
downloadOpts: DownloadOptions, tempBase: string) {
downloadOpts: DownloadOptions, tempBase: string): Promise<ApplicationBundlePath> {
// In order to generate a universal macOS build we actually need to build the x64 and the arm64 app
// and then glue them together
info(`Packaging app for platform ${comboOpts.platform} universal using electron v${comboOpts.electronVersion} - Building x64 and arm64 slices now`, comboOpts.quiet);
Expand All @@ -24,7 +24,7 @@
await fs.remove(finalUniversalPath);
} else {
info(`Skipping ${comboOpts.platform} ${comboOpts.arch} (output dir already exists, use --overwrite to force)`, comboOpts.quiet);
return true;
return '';

Check warning on line 27 in src/universal.ts

View check run for this annotation

Codecov / codecov/patch

src/universal.ts#L27

Added line #L27 was not covered by tests
}
}

Expand All @@ -44,8 +44,7 @@
delete tempOpts.osxSign;
delete tempOpts.osxNotarize;

// @TODO(erikian): I don't like this type cast, the return type for `packageForPlatformAndArchWithOpts` is probably wrong
tempPackages[tempArch] = (await packageForPlatformAndArchWithOpts(tempOpts, tempDownloadOpts)) as string;
tempPackages[tempArch] = await packageForPlatformAndArchWithOpts(tempOpts, tempDownloadOpts);

Check warning on line 47 in src/universal.ts

View check run for this annotation

Codecov / codecov/patch

src/universal.ts#L47

Added line #L47 was not covered by tests
}));

const x64AppPath = tempPackages.x64;
Expand Down