feat: custom maker dmg (#3501)

This commit is contained in:
Alex Yang
2023-08-01 12:20:29 -07:00
committed by GitHub
parent 03f12f6aa4
commit 1b17743ed3
15 changed files with 287 additions and 298 deletions

View File

@@ -0,0 +1,7 @@
# @affine/maker-dmg
## Pre-requisites
```shell
brew install create-dmg
```

View File

@@ -0,0 +1,18 @@
{
"name": "@affine/maker-dmg",
"version": "1.0.0",
"main": "dist/maker.js",
"scripts": {
"build": "vite build",
"dev": "vite build --watch"
},
"dependencies": {
"@electron-forge/maker-base": "^6.2.1",
"@electron-forge/shared-types": "^6.2.1",
"fs-extra": "^11.1.1"
},
"devDependencies": {
"typescript": "^5.1.6",
"vite": "^4.4.7"
}
}

View File

@@ -0,0 +1,21 @@
{
"name": "@affine/maker-dmg",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"sourceRoot": "packages/maker-dmg/src",
"targets": {
"build": {
"executor": "@nx/vite:build",
"options": {
"outputPath": "packages/maker-dmg/dist"
}
},
"serve": {
"executor": "@nx/vite:build",
"options": {
"outputPath": "packages/maker-dmg/dist",
"watch": true
}
}
}
}

View File

@@ -0,0 +1,74 @@
export interface CodeSignOptions {
'signing-identity': string;
identifier?: string;
}
export interface DMGContents {
x: number;
y: number;
type: 'link' | 'file' | 'position';
path: string;
name: string;
}
export interface WindowPositionOptions {
x: number;
y: number;
}
export interface WindowSizeOptions {
width: number;
height: number;
}
export interface WindowOptions {
position?: WindowPositionOptions;
size?: WindowSizeOptions;
}
export interface AdditionalDMGOptions {
'background-color'?: string;
'icon-size'?: number;
window?: WindowOptions;
'code-sign'?: CodeSignOptions;
}
export interface MakerDMGConfig {
/**
* The application name
*/
name?: string;
/**
* Path to the background for the DMG window
*/
background: string;
/**
* Path to the icon to use for the app in the DMG window
*/
icon: string;
/**
* Overwrite an existing DMG file if if already exists
*/
overwrite?: boolean;
/**
* Enable debug message output
*/
debug?: boolean;
/**
* How big to make the icon for the app in the DMG
*/
iconSize?: number;
/**
* Disk image format
*
* Default: UDZO
*/
format?: 'UDRW' | 'UDRO' | 'UDCO' | 'UDZO' | 'UDBZ' | 'ULFO';
file: string;
/**
* Additional options to pass through to node-appdmng
*
* All available options are available in the [`appdmg` docs](https://github.com/LinusU/node-appdmg)
*/
additionalDMGOptions?: AdditionalDMGOptions;
}

View File

@@ -0,0 +1,77 @@
import { execFileSync } from 'node:child_process';
import { cp, mkdtemp } from 'node:fs/promises';
import os from 'node:os';
import { join, resolve } from 'node:path';
import type { MakerOptions } from '@electron-forge/maker-base';
import { MakerBase } from '@electron-forge/maker-base';
import type { ForgePlatform } from '@electron-forge/shared-types';
import type { MakerDMGConfig } from './config';
export default class MakerDMG extends MakerBase<MakerDMGConfig> {
name = 'dmg';
defaultPlatforms: ForgePlatform[] = ['darwin', 'mas'];
override isSupportedOnCurrentPlatform(): boolean {
return process.platform === 'darwin';
}
override async make({
dir,
makeDir,
appName,
packageJSON,
targetArch,
}: MakerOptions): Promise<string[]> {
const outPath = resolve(makeDir, `${this.config.name || appName}.dmg`);
const forgeDefaultOutPath = resolve(
makeDir,
`${appName}-${packageJSON.version}-${targetArch}.dmg`
);
await this.ensureFile(outPath);
const args = [
'--volname',
appName,
'--window-size',
'610',
'365',
'--background',
this.config.background,
'--icon-size',
'128',
'--icon',
`${appName}.app`,
'176',
'192',
'--hide-extension',
`${appName}.app`,
'--app-drop-link',
'423',
'192',
];
const tempDir = await mkdtemp(join(os.tmpdir(), 'electron-forge-dmg-'));
const filePath = join(tempDir, `${appName}.app`);
try {
await cp(this.config.file, filePath, {
recursive: true,
});
args.push(outPath, filePath);
execFileSync('create-dmg', args, {
cwd: dir,
env: process.env,
stdio: 'inherit',
shell: true,
});
} finally {
// await rm(filePath, { force: true });
}
return [forgeDefaultOutPath];
}
}

View File

@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"include": ["./src"],
"compilerOptions": {
"composite": true,
"noEmit": false,
"outDir": "lib"
}
}

View File

@@ -0,0 +1,29 @@
import { resolve } from 'node:path';
import { fileURLToPath } from 'url';
import { defineConfig } from 'vite';
const root = fileURLToPath(new URL('.', import.meta.url));
export default defineConfig({
build: {
emptyOutDir: true,
minify: false,
lib: {
entry: {
maker: resolve(root, 'src/maker.ts'),
},
formats: ['cjs'],
},
rollupOptions: {
output: {
exports: 'named',
},
external: [
'@electron-forge/maker-base',
'@electron-forge/shared-types',
/^node:/,
],
},
},
});