feat: 完成 v0.6.0 墨呈品牌迁移
This commit is contained in:
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
declare const __APP_ID__: string;
|
||||
declare const __APP_ENGLISH_NAME__: string;
|
||||
declare const __APP_DISPLAY_NAME__: string;
|
||||
declare const __APP_WINDOW_TITLE__: string;
|
||||
declare const __APP_LEGACY_DISPLAY_NAME__: string;
|
||||
@@ -233,7 +233,7 @@ export class DesktopApplicationController {
|
||||
: undefined;
|
||||
const initialBounds = this.#resolveInitialBounds(isPrimary);
|
||||
const window = new BrowserWindow({
|
||||
title: `Markdown PDF 导出器 v${app.getVersion()}`,
|
||||
title: `${__APP_WINDOW_TITLE__} v${app.getVersion()}`,
|
||||
icon: this.#iconPath,
|
||||
...initialBounds,
|
||||
minWidth: Math.min(
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
session
|
||||
} from "electron";
|
||||
import { access, mkdir } from "node:fs/promises";
|
||||
import { existsSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import {
|
||||
@@ -25,6 +26,7 @@ import {
|
||||
findMarkdownFileArgument,
|
||||
isMarkdownFilePath
|
||||
} from "./markdown-file.js";
|
||||
import { migrateLegacyUserData } from "./user-data-migration.js";
|
||||
|
||||
const APP_SCHEME = "mdpdf";
|
||||
const APP_HOST = "bundle";
|
||||
@@ -37,6 +39,28 @@ let pendingExternalMarkdownPath = findMarkdownFileArgument(
|
||||
process.cwd()
|
||||
);
|
||||
|
||||
function configurePackagedUserDataDirectory() {
|
||||
if (!app.isPackaged) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
const appDataDirectory = app.getPath("appData");
|
||||
const targetDirectory = path.join(
|
||||
appDataDirectory,
|
||||
__APP_ENGLISH_NAME__
|
||||
);
|
||||
const targetAlreadyExists = existsSync(targetDirectory);
|
||||
app.setPath("userData", targetDirectory);
|
||||
if (targetAlreadyExists) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return migrateLegacyUserData(
|
||||
[path.join(appDataDirectory, __APP_LEGACY_DISPLAY_NAME__)],
|
||||
targetDirectory
|
||||
).then(() => undefined);
|
||||
}
|
||||
|
||||
const userDataMigration = configurePackagedUserDataDirectory();
|
||||
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
{
|
||||
scheme: APP_SCHEME,
|
||||
@@ -300,9 +324,10 @@ if (!hasSingleInstanceLock) {
|
||||
app
|
||||
.whenReady()
|
||||
.then(async () => {
|
||||
await userDataMigration;
|
||||
Menu.setApplicationMenu(null);
|
||||
if (process.platform === "win32") {
|
||||
app.setAppUserModelId("com.md-to-pdf.desktop");
|
||||
app.setAppUserModelId(__APP_ID__);
|
||||
}
|
||||
const themeDirectory = getDesktopThemeDirectory();
|
||||
await mkdir(themeDirectory, { recursive: true });
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { cp, lstat, mkdir } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
const MIGRATED_ENTRIES = [
|
||||
"window-state.json",
|
||||
"save-location.json",
|
||||
"themes",
|
||||
"Local Storage",
|
||||
"Session Storage",
|
||||
"IndexedDB"
|
||||
] as const;
|
||||
|
||||
async function readOrdinaryStatus(target: string) {
|
||||
try {
|
||||
const status = await lstat(target);
|
||||
return status.isSymbolicLink() ? undefined : status;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
return undefined;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export interface LegacyUserDataMigrationResult {
|
||||
migrated: boolean;
|
||||
sourceDirectory?: string;
|
||||
copiedEntries: string[];
|
||||
}
|
||||
|
||||
export async function migrateLegacyUserData(
|
||||
legacyDirectories: readonly string[],
|
||||
targetDirectory: string
|
||||
): Promise<LegacyUserDataMigrationResult> {
|
||||
if (await readOrdinaryStatus(targetDirectory)) {
|
||||
return { migrated: false, copiedEntries: [] };
|
||||
}
|
||||
|
||||
for (const legacyDirectory of legacyDirectories) {
|
||||
if (path.resolve(legacyDirectory) === path.resolve(targetDirectory)) {
|
||||
continue;
|
||||
}
|
||||
const legacyStatus = await readOrdinaryStatus(legacyDirectory);
|
||||
if (!legacyStatus?.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await mkdir(targetDirectory, { recursive: true });
|
||||
const copiedEntries: string[] = [];
|
||||
for (const entry of MIGRATED_ENTRIES) {
|
||||
const source = path.join(legacyDirectory, entry);
|
||||
if (!(await readOrdinaryStatus(source))) {
|
||||
continue;
|
||||
}
|
||||
await cp(source, path.join(targetDirectory, entry), {
|
||||
recursive: true,
|
||||
errorOnExist: true,
|
||||
force: false
|
||||
});
|
||||
copiedEntries.push(entry);
|
||||
}
|
||||
return {
|
||||
migrated: true,
|
||||
sourceDirectory: legacyDirectory,
|
||||
copiedEntries
|
||||
};
|
||||
}
|
||||
|
||||
return { migrated: false, copiedEntries: [] };
|
||||
}
|
||||
Reference in New Issue
Block a user