Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

refactor: simplify macOS install flow

Tw9372be61061520
diff --git a/bin/builders/BaseBuilder.ts b/bin/builders/BaseBuilder.ts
index e780b210..ff3cfcd9 100644
--- a/bin/builders/BaseBuilder.ts
+++ b/bin/builders/BaseBuilder.ts
@@ -1,4 +1,3 @@
-import os from 'os';
import path from 'path';
import fsExtra from 'fs-extra';
import chalk from 'chalk';
@@ -302,71 +301,35 @@ export default abstract class BaseBuilder {
logger.success('✔ Raw binary located in', path.resolve(binaryPath));
}
- // Auto-install to /Applications on macOS
- if (IS_MAC && fileType === 'dmg' && this.options.install) {
- await this.installDmgToApplications(distPath, name);
+ if (IS_MAC && fileType === 'app' && this.options.install) {
+ await this.installAppToApplications(distPath, name);
}
}
- private async installDmgToApplications(
- dmgPath: string,
+ private async installAppToApplications(
+ appBundlePath: string,
appName: string,
): Promise<void> {
- const { execa } = await import('execa');
- const mountRoot = path.join(os.tmpdir(), 'pake-install-');
- const mountPoint = await fsExtra.mkdtemp(mountRoot);
- let attached = false;
-
try {
logger.info(`- Installing ${appName} to /Applications...`);
- await execa('hdiutil', [
- 'attach',
- dmgPath,
- '-nobrowse',
- '-readonly',
- '-mountpoint',
- mountPoint,
- ]);
- attached = true;
-
- const entries = await fsExtra.readdir(mountPoint);
- const appBundleName = entries.find((entry) => entry.endsWith('.app'));
-
- if (!appBundleName) {
- throw new Error('No .app bundle found inside mounted DMG');
- }
-
- const appSource = path.join(mountPoint, appBundleName);
+ const appBundleName = path.basename(appBundlePath);
const appDest = path.join('/Applications', appBundleName);
- // Remove existing installation if present
if (await fsExtra.pathExists(appDest)) {
await fsExtra.remove(appDest);
}
- await fsExtra.copy(appSource, appDest);
-
- await execa('hdiutil', ['detach', mountPoint]);
- attached = false;
- await fsExtra.remove(dmgPath);
+ await fsExtra.copy(appBundlePath, appDest);
+ await fsExtra.remove(appBundlePath);
logger.success(
`✔ ${appBundleName.replace(/\.app$/, '')} installed to /Applications`,
);
- logger.success(`✔ Installer DMG removed`);
+ logger.success('✔ Local app bundle removed');
} catch (error) {
logger.error(`✕ Failed to install ${appName}: ${error}`);
- logger.info(` The DMG is still available at: ${dmgPath}`);
- } finally {
- if (attached) {
- await execa('hdiutil', ['detach', mountPoint]).catch(async () => {
- await execa('hdiutil', ['detach', mountPoint, '-force']).catch(
- () => undefined,
- );
- });
- }
- await fsExtra.remove(mountPoint).catch(() => undefined);
+ logger.info(` The app bundle is still available at: ${appBundlePath}`);
}
}
diff --git a/bin/builders/MacBuilder.ts b/bin/builders/MacBuilder.ts
index 29fa8cd7..1fb2611f 100644
--- a/bin/builders/MacBuilder.ts
+++ b/bin/builders/MacBuilder.ts
@@ -15,7 +15,11 @@ export default class MacBuilder extends BaseBuilder {
? options.targets
: 'auto';
- if (options.iterativeBuild || process.env.PAKE_CREATE_APP === '1') {
+ if (
+ options.iterativeBuild ||
+ options.install ||
+ process.env.PAKE_CREATE_APP === '1'
+ ) {
this.buildFormat = 'app';
} else {
this.buildFormat = 'dmg';
diff --git a/bin/helpers/cli-program.ts b/bin/helpers/cli-program.ts
index b07df71a..ac1a1a63 100644
--- a/bin/helpers/cli-program.ts
+++ b/bin/helpers/cli-program.ts
@@ -244,7 +244,7 @@ ${green('|_| \\__,_|_|\\_\\___| can turn any webpage into a desktop app with
)
.option(
'--install',
- 'Auto-install app to /Applications (macOS) after build and remove installer',
+ 'Auto-install app to /Applications (macOS) after build and remove local bundle',
DEFAULT.install,
)
.version(packageJson.version, '-v, --version')
diff --git a/dist/cli.js b/dist/cli.js
index 05777df3..97361d34 100644
--- a/dist/cli.js
+++ b/dist/cli.js
@@ -4,9 +4,9 @@ import updateNotifier from 'update-notifier';
import path from 'path';
import fsExtra from 'fs-extra';
import { fileURLToPath } from 'url';
-import os from 'os';
import chalk from 'chalk';
import prompts from 'prompts';
+import os from 'os';
import { execa, execaSync } from 'execa';
import crypto from 'crypto';
import ora from 'ora';
@@ -962,56 +962,26 @@ class BaseBuilder {
const binaryPath = this.getRawBinaryPath(name);
logger.success('✔ Raw binary located in', path.resolve(binaryPath));
}
- // Auto-install to /Applications on macOS
- if (IS_MAC && fileType === 'dmg' && this.options.install) {
- await this.installDmgToApplications(distPath, name);
+ if (IS_MAC && fileType === 'app' && this.options.install) {
+ await this.installAppToApplications(distPath, name);
}
}
- async installDmgToApplications(dmgPath, appName) {
- const { execa } = await import('execa');
- const mountRoot = path.join(os.tmpdir(), 'pake-install-');
- const mountPoint = await fsExtra.mkdtemp(mountRoot);
- let attached = false;
+ async installAppToApplications(appBundlePath, appName) {
try {
logger.info(`- Installing ${appName} to /Applications...`);
- await execa('hdiutil', [
- 'attach',
- dmgPath,
- '-nobrowse',
- '-readonly',
- '-mountpoint',
- mountPoint,
- ]);
- attached = true;
- const entries = await fsExtra.readdir(mountPoint);
- const appBundleName = entries.find((entry) => entry.endsWith('.app'));
- if (!appBundleName) {
- throw new Error('No .app bundle found inside mounted DMG');
- }
- const appSource = path.join(mountPoint, appBundleName);
+ const appBundleName = path.basename(appBundlePath);
const appDest = path.join('/Applications', appBundleName);
- // Remove existing installation if present
if (await fsExtra.pathExists(appDest)) {
await fsExtra.remove(appDest);
}
- await fsExtra.copy(appSource, appDest);
- await execa('hdiutil', ['detach', mountPoint]);
- attached = false;
- await fsExtra.remove(dmgPath);
+ await fsExtra.copy(appBundlePath, appDest);
+ await fsExtra.remove(appBundlePath);
logger.success(`✔ ${appBundleName.replace(/\.app$/, '')} installed to /Applications`);
- logger.success(`✔ Installer DMG removed`);
+ logger.success('✔ Local app bundle removed');
}
catch (error) {
logger.error(`✕ Failed to install ${appName}: ${error}`);
- logger.info(` The DMG is still available at: ${dmgPath}`);
- }
- finally {
- if (attached) {
- await execa('hdiutil', ['detach', mountPoint]).catch(async () => {
- await execa('hdiutil', ['detach', mountPoint, '-force']).catch(() => undefined);
- });
- }
- await fsExtra.remove(mountPoint).catch(() => undefined);
+ logger.info(` The app bundle is still available at: ${appBundlePath}`);
}
}
getFileType(target) {
@@ -1213,7 +1183,9 @@ class MacBuilder extends BaseBuilder {
this.buildArch = validArchs.includes(options.targets || '')
? options.targets
: 'auto';
- if (options.iterativeBuild || process.env.PAKE_CREATE_APP === '1') {
+ if (options.iterativeBuild ||
+ options.install ||
+ process.env.PAKE_CREATE_APP === '1') {
this.buildFormat = 'app';
}
else {
@@ -2262,7 +2234,7 @@ ${green('|_| \\__,_|_|\\_\\___| can turn any webpage into a desktop app with
.addOption(new Option('--new-window', 'Allow new window for third-party login')
.default(DEFAULT_PAKE_OPTIONS.newWindow)
.hideHelp())
- .option('--install', 'Auto-install app to /Applications (macOS) after build and remove installer', DEFAULT_PAKE_OPTIONS.install)
+ .option('--install', 'Auto-install app to /Applications (macOS) after build and remove local bundle', DEFAULT_PAKE_OPTIONS.install)
.version(packageJson.version, '-v, --version')
.configureHelp({
sortSubcommands: true,
diff --git a/docs/cli-usage.md b/docs/cli-usage.md
index aee4e287..1e48f0a9 100644
--- a/docs/cli-usage.md
+++ b/docs/cli-usage.md
@@ -60,7 +60,7 @@ pake [url] [options]
The packaged application will be located in the current working directory by default. The first packaging might take some time due to environment configuration. Please be patient.
-> **macOS Output**: On macOS, Pake creates DMG installers by default. To create `.app` bundles for testing (to avoid user interaction), set the environment variable `PAKE_CREATE_APP=1`.
+> **macOS Output**: On macOS, Pake creates DMG installers by default. To create `.app` bundles for testing (to avoid user interaction), set the environment variable `PAKE_CREATE_APP=1`. If you want Pake to install the app directly into `/Applications`, use `--install`, which builds an `.app`, copies it into `/Applications`, and removes the local bundle after a successful install.
>
> **Note**: Packaging requires the Rust environment. If Rust is not installed, you will be prompted for installation confirmation. In case of installation failure or timeout, you can [install it manually](https://www.rust-lang.org/tools/install).
@@ -423,6 +423,19 @@ Turn on rapid build mode (app only, no dmg/deb/msi), good for debugging. Default
--iterative-build
```
+#### [install]
+
+Install the built macOS app directly into `/Applications`. Default is `false`.
+
+This option is macOS-only and is intended for local development or quick testing. When enabled, Pake builds an `.app` bundle, copies it into `/Applications`, replaces any existing app with the same name, and removes the local bundle after a successful install. If the install fails, the local `.app` is kept in the current working directory.
+
+```shell
+--install
+
+# Example: Build and install directly to /Applications
+pake https://github.com --name GitHub --install
+```
+
#### [multi-instance]
Allow the packaged app to run more than one instance at the same time. Default is `false`, which means launching a second instance simply focuses the existing window. Enable this when you need to open several windows of the same app simultaneously.
diff --git a/docs/cli-usage_CN.md b/docs/cli-usage_CN.md
index 093816a4..356a7792 100644
--- a/docs/cli-usage_CN.md
+++ b/docs/cli-usage_CN.md
@@ -60,7 +60,7 @@ pake [url] [options]
应用程序的打包结果将默认保存在当前工作目录。由于首次打包需要配置环境,这可能需要一些时间,请耐心等待。
-> **macOS 输出**:在 macOS 上,Pake 默认创建 DMG 安装程序。如需创建 `.app` 包进行测试(避免用户交互),请设置环境变量 `PAKE_CREATE_APP=1`。
+> **macOS 输出**:在 macOS 上,Pake 默认创建 DMG 安装程序。如需创建 `.app` 包进行测试(避免用户交互),请设置环境变量 `PAKE_CREATE_APP=1`。如果希望 Pake 直接将应用安装到 `/Applications`,可以使用 `--install`;该选项会构建 `.app`、复制到 `/Applications`,并在安装成功后删除当前目录中的本地 `.app`。
>
> **注意**:打包过程需要使用 `Rust` 环境。如果您没有安装 `Rust`,系统会提示您是否要安装。如果遇到安装失败或超时的问题,您可以 [手动安装](https://www.rust-lang.org/tools/install)。
@@ -421,6 +421,19 @@ pake https://github.com --name GitHub --keep-binary
--iterative-build
```
+#### [install]
+
+将构建出的 macOS 应用直接安装到 `/Applications`。默认为 `false`。
+
+该选项仅适用于 macOS,适合本地开发和快速验证。启用后,Pake 会构建 `.app` 包,将其复制到 `/Applications`,如果已存在同名应用则先替换,并在安装成功后删除当前工作目录中的本地 `.app`。如果安装失败,当前目录中的 `.app` 会被保留。
+
+```shell
+--install
+
+# 示例:构建后直接安装到 /Applications
+pake https://github.com --name GitHub --install
+```
+
#### [multi-instance]
允许打包后的应用同时运行多个实例。默认为 `false`,此时再次启动只会聚焦已有窗口。启用该选项后,可以同时打开同一个应用的多个窗口。
diff --git a/tests/unit/cli-options.test.ts b/tests/unit/cli-options.test.ts
index 949955c9..756d2a4a 100644
--- a/tests/unit/cli-options.test.ts
+++ b/tests/unit/cli-options.test.ts
@@ -22,10 +22,11 @@ describe('CLI options', () => {
expect(option?.defaultValue).toBe('');
});
- it('registers --install option', () => {
+ it('registers visible --install option', () => {
const option = program.options.find((item) => item.long === '--install');
expect(option).toBeDefined();
expect(option?.defaultValue).toBe(false);
+ expect(option?.hidden).toBe(false);
});
});