Skip to content

Commit

Permalink
feat: configration export & import
Browse files Browse the repository at this point in the history
Signed-off-by: The1111mp <The1111mp@outlook.com>
  • Loading branch information
1111mp committed Mar 2, 2024
1 parent e8b7086 commit acc5b77
Show file tree
Hide file tree
Showing 20 changed files with 606 additions and 42 deletions.
21 changes: 18 additions & 3 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Closer, Themes } from 'types';
import type { UpdateInfo as ElectronUpdateInfo } from 'electron-updater';
import { Closer, Themes } from "types";
import type { UpdateInfo as ElectronUpdateInfo } from "electron-updater";

declare global {
namespace Nvmd {
Expand Down Expand Up @@ -28,7 +28,7 @@ declare global {
mirror: string;
}

type UpdateInfo = ElectronUpdateInfo | 'update-not-available';
type UpdateInfo = ElectronUpdateInfo | "update-not-available";

interface Project {
name: string;
Expand All @@ -38,5 +38,20 @@ declare global {
createAt: string;
updateAt: string;
}

interface ConfigrationExport {
color?: string;
mirrors?: string | null;
path: string;
projects?: boolean;
setting?: boolean;
}

interface Configration {
color?: string;
mirrors?: string;
setting?: Setting;
projects?: Project[];
}
}
}
50 changes: 47 additions & 3 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@
},
"Cancel": {
"message": "Cancel",
"description": "The text of the OK"
"description": "The text of the Cancel"
},
"Continue": {
"message": "Continue",
"description": "The text of the Continue"
},
"Language": {
"message": "Language",
Expand Down Expand Up @@ -399,9 +403,9 @@
"message": "Quit App",
"description": "The text of the Quit-App"
},
"Theme-Color": {
"Theme color": {
"message": "Theme Color",
"description": "The text of the Theme-Color"
"description": "The text of the Theme color"
},
"Color-Tip": {
"message": "Here you can choose your favorite color as the theme color.",
Expand Down Expand Up @@ -458,5 +462,45 @@
"Custom": {
"message": "Custom",
"description": "The text of the Custom"
},
"Configration": {
"message": "Configration",
"description": "The text of the Configration"
},
"Configration-export": {
"message": "Configration export",
"description": "The text of the Configration-export"
},
"Configration-export-tip": {
"message": "Please select the configuration you want to export:",
"description": "Configration-export-tip"
},
"Configration-export-setting": {
"message": "(lanuage, installation directory, mirror url, etc.)",
"description": "Configration-export-setting"
},
"Configration-export-success": {
"message": "The export has been completed, please view it in the $filename$ file",
"description": "Configration-export-success"
},
"Configration-import": {
"message": "Configration import",
"description": "The text of the Configration-import"
},
"Configration-import-tip": {
"message": "Whether to automatically synchronize the nodejs version (.nvmdrc) for project data when importing it?",
"description": "The text of the Configration-import-tip"
},
"Import-only": {
"message": "Import only",
"description": "The text of the Import-only"
},
"Import-and-sync": {
"message": "Import and sync",
"description": "The text of the Import-and-sync"
},
"Configration-import-success": {
"message": "The import has been completed. If you encounter data inconsistency, please restart the application.",
"description": "The text of the Configration-import-success"
}
}
50 changes: 47 additions & 3 deletions _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@
},
"Cancel": {
"message": "取消",
"description": "The text of the OK"
"description": "The text of the Cancel"
},
"Continue": {
"message": "继续",
"description": "The text of the Continue"
},
"Language": {
"message": "语言",
Expand Down Expand Up @@ -399,9 +403,9 @@
"message": "退出应用",
"description": "The text of the Quit-App"
},
"Theme-Color": {
"Theme color": {
"message": "主题颜色",
"description": "The text of the Theme-Color"
"description": "The text of the Theme color"
},
"Color-Tip": {
"message": "在这里你可以选择你喜爱的颜色作为主题的颜色。",
Expand Down Expand Up @@ -458,5 +462,45 @@
"Custom": {
"message": "自定义",
"description": "The text of the Custom"
},
"Configration": {
"message": "配置",
"description": "The text of the Configration"
},
"Configration-export": {
"message": "配置导出",
"description": "The text of the Configration-export"
},
"Configration-export-tip": {
"message": "请选择您要导出的配置:",
"description": "Configration-export-tip"
},
"Configration-export-setting": {
"message": "(语言、安装目录、镜像地址等。)",
"description": "Configration-export-setting"
},
"Configration-export-success": {
"message": "导出已完成,请在 $filename$ 文件中查看",
"description": "Configration-export-setting"
},
"Configration-import": {
"message": "配置导入",
"description": "The text of the Configration-import"
},
"Configration-import-tip": {
"message": "导入项目数据时是否自动同步 nodejs 版本(.nvmdrc)?",
"description": "The text of the Configration-import-tip"
},
"Import-only": {
"message": "仅导入",
"description": "The text of the Import-only"
},
"Import-and-sync": {
"message": "导入且同步",
"description": "The text of the Import-and-sync"
},
"Configration-import-success": {
"message": "导入已完成。 如果遇到数据不一致,请重启应用。",
"description": "The text of the Configration-import-success"
}
}
7 changes: 4 additions & 3 deletions electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ export default defineConfig(({ mode }) => {
main: {
clean: true,
entry: { main: isTest ? "src/main/main.test.ts" : "src/main/main.ts" },
target: "node18",
format: "esm",
// The requested module 'electron-updater' is a CommonJS module, which may not support all module.exports as named exports.
noExternal: isProd ? [/(.*)/] : ["electron-updater"],
minify: isProd,
watch: !isProd
watch: !isProd,
sourcemap: !isProd
},
preload: {
clean: true,
entry: { preload: isTest ? "src/preload/preload.test.ts" : "src/preload/index.ts" },
format: "cjs",
minify: isProd,
watch: !isProd
watch: !isProd,
sourcemap: !isProd
},
renderer: {
resolve: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@electron-toolkit/utils": "^3.0.0",
"@hookform/resolvers": "^3.3.4",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-icons": "^1.3.0",
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 44 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ import { resolveHtmlPath } from "./utils/resolvePath";
import { allNodeVersions, allInstalledNodeVersions } from "./deps/all-node-versions";
import getNode from "./deps/get-node";
import { updateSchema } from "./utils/migration";
import { configrationExport, configrationImport } from "./utils/configration";
import { getCurrentVersion, setCurrentVersion, uninstallVersion } from "./utils/version";
import { setSetting, getSetting } from "./utils/setting";
import { getProjects, getVersion, syncProjectVersion, updateProjects } from "./utils/projects";
import {
getProjects,
getVersion,
syncProjectVersion,
updateProjects,
updateProjectsAndSync
} from "./utils/projects";
import { gt } from "semver";
import loadLocale from "./locale";
import { Closer, Themes } from "../types";
Expand Down Expand Up @@ -508,4 +515,40 @@ Promise.resolve().then(() => {
ipcMain.handle("sync-project-version", (_event, path: string, version: string) => {
return syncProjectVersion(path, version);
});

ipcMain.handle("configration-export", async (_event, args: Nvmd.ConfigrationExport) => {
const { color, setting: exportSetting, projects, path, mirrors } = args;
let output: Nvmd.Configration = {};

if (color) output.color = color;

if (exportSetting) output.setting = setting;
if (mirrors) output.mirrors = mirrors;

if (projects) output.projects = await getProjects();

return configrationExport(path, output);
});

ipcMain.handle(
"configration-import",
async (_event, { sync, title }: { sync: boolean; title: string }) => {
const { canceled, filePaths } = await dialog.showOpenDialog(mainWindow!, {
title,
filters: [{ extensions: ["json"], name: "" }],
properties: ["openFile", "createDirectory", "showHiddenFiles"]
});

if (canceled) return { canceled };
const [path] = filePaths;
const { color, mirrors, setting: importSetting, projects } = await configrationImport(path);

if (projects) {
await updateProjectsAndSync(projects, sync);
projects && mainWindow?.webContents.send("call-projects-update", projects);
}

return { canceled, color, mirrors, setting: importSetting };
}
);
});
17 changes: 17 additions & 0 deletions src/main/utils/configration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { join } from "node:path";
import { readJson, writeJson } from "fs-extra";

export async function configrationExport(path: string, output: Nvmd.Configration) {
const filename = `configration_${Date.now()}.json`;
await writeJson(join(path, `configration_${Date.now()}.json`), output);
return filename;
}

export async function configrationImport(path: string) {
try {
const input = (await readJson(path)) as Nvmd.Configration;
return input;
} catch {
return {};
}
}
37 changes: 22 additions & 15 deletions src/main/utils/projects.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { join } from 'node:path';
import {
pathExists,
readFile,
readJson,
writeJson,
writeFile,
remove,
} from 'fs-extra';
import { PROJECTS_JSONFILE, NVMDRC_NAME } from '../constants';
import { join } from "node:path";
import { pathExists, readFile, readJson, writeJson, writeFile, remove } from "fs-extra";
import { PROJECTS_JSONFILE, NVMDRC_NAME } from "../constants";

let cacheProjects: Nvmd.Project[];

export async function getProjects(
load: boolean = false,
): Promise<Nvmd.Project[]> {
export async function getProjects(load: boolean = false): Promise<Nvmd.Project[]> {
if (cacheProjects !== void 0 && !load) {
return cacheProjects;
}
Expand All @@ -37,13 +28,29 @@ export async function updateProjects(projects: Nvmd.Project[], path?: string) {
return;
}

export async function updateProjectsAndSync(projects: Nvmd.Project[], sync: boolean = false) {
const syncProject = async ({ path, version }: Nvmd.Project, index: number) => {
if (!(await pathExists(path))) {
projects[index].active = false;
return;
}

sync && (await writeFile(join(path, NVMDRC_NAME), version));
return;
};

await Promise.all(projects.map((project, index) => syncProject(project, index)));
cacheProjects = projects;
await writeJson(PROJECTS_JSONFILE, projects);
}

export async function getVersion(path: string): Promise<string> {
const target = join(path, NVMDRC_NAME);
if (!(await pathExists(target))) return '';
if (!(await pathExists(target))) return "";

const version = (await readFile(target)).toString();

return version || '';
return version || "";
}

export async function syncProjectVersion(path: string, version: string) {
Expand Down
Loading

0 comments on commit acc5b77

Please sign in to comment.