Skip to content

Commit

Permalink
汐洛VSC扩展add_task_同步更新版本使用格式化时间补全二段版本号 #824
Browse files Browse the repository at this point in the history
  • Loading branch information
Soltus committed Jun 12, 2024
1 parent 45e15b4 commit 614b1d3
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions/Sillot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sillot",
"displayName": "汐洛 Sillot",
"description": "汐洛彖夲肜矩阵(Sillot T☳Converbenk Matrix)为智慧新彖务服务。此插件为汐洛官方插件,提供多功能一体化集成。",
"version": "0.35.3100",
"version": "0.35.20240612140342",
"preview": true,
"repository": "https://github.com/Hi-Windom/Sillot",
"publisher": "Hi-Windom",
Expand Down
179 changes: 97 additions & 82 deletions .vscode/extensions/Sillot/src/task/同步更新版本.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,97 @@
import * as vscode from "vscode";
import fs from "fs-extra";
import path, { resolve } from "path";
import { Log } from "../utils/log";
import { C } from "../extension.const";
import { loadCompletionItemsFromFile } from "../utils/json";

export function add_task_同步更新版本(context: vscode.ExtensionContext) {
const TAG = "汐洛.同步更新版本";
const disposable = vscode.commands.registerCommand(TAG, async () => {
const wname = vscode.workspace.name;
if (wname && vscode.workspace.workspaceFile) {
const pkgMapFile = `${path.dirname(vscode.workspace.workspaceFile.fsPath)}/${C.PackageJsonMapping}`;
Log.d(wname, pkgMapFile);
if (!(await fs.exists(pkgMapFile))) {
vscode.window.showWarningMessage("package.json 映射不存在,请先添加");
return;
}
const pkgMap: { [key: string]: any } = await loadCompletionItemsFromFile(pkgMapFile);
const paths: string[] = pkgMap[wname];

// 创建快速选择框
const quickPick = vscode.window.createQuickPick();
quickPick.title = "选择要更新版本的文件";
quickPick.items = paths.map(path => ({ label: path }));
quickPick.canSelectMany = true; // 允许多选

// 显示快速选择框并等待用户选择
const selectedOptions: string[] = await new Promise(resolve => {
quickPick.onDidAccept(() => {
resolve(quickPick.selectedItems.map(item => item.label));
quickPick.dispose();
});
quickPick.onDidHide(() => {
resolve([]);
quickPick.dispose();
});
quickPick.show();
});

if (selectedOptions.length === 0) {
// 如果用户没有选择任何选项,则直接返回
return;
}

// 获取用户选择的路径的版本号
const versionPromises = selectedOptions.map(async (value: string) => {
if (await fs.exists(value)) {
const pkgContent = fs.readJSONSync(value);
return pkgContent.version;
}
return null; // 如果文件不存在,返回null
});

Promise.all(versionPromises).then(versions => {
// 过滤掉null值
const validVersions = versions.filter(version => version !== null);
vscode.window.showInputBox({ prompt: `输入新版本: (当前版本: ${validVersions.join(", ")})` }).then(async version => {
if (version) {
// 遍历映射并更新版本号
selectedOptions.forEach(async (value: string, index: number) => {
Log.d(TAG, value);
if (await fs.exists(value)) {
const pkgContent = fs.readJSONSync(value);
pkgContent.version = version;
fs.writeFileSync(value, JSON.stringify(pkgContent, null, 2));
Log.d(`${version} ->${value}`);
} else {
vscode.window.showWarningMessage(`已跳过无效映射 ${value}`);
}
});
vscode.window.showInformationMessage("所有 package.json 文件的版本已更新。");
}
});
});
} else {
vscode.window.showWarningMessage("当前不在工作区环境");
}
});

context.subscriptions.push(disposable);
}
import * as vscode from "vscode";
import fs from "fs-extra";
import path, { resolve } from "path";
import { Log } from "../utils/log";
import { C } from "../extension.const";
import { loadCompletionItemsFromFile } from "../utils/json";
import { formatDate } from "sofill/mid";

export function add_task_同步更新版本(context: vscode.ExtensionContext) {
const TAG = "汐洛.同步更新版本";
const disposable = vscode.commands.registerCommand(TAG, async () => {
const wname = vscode.workspace.name;
if (wname && vscode.workspace.workspaceFile) {
const pkgMapFile = `${path.dirname(vscode.workspace.workspaceFile.fsPath)}/${C.PackageJsonMapping}`;
Log.d(wname, pkgMapFile);
if (!(await fs.exists(pkgMapFile))) {
vscode.window.showWarningMessage("package.json 映射不存在,请先添加");
return;
}
const pkgMap: { [key: string]: any } = await loadCompletionItemsFromFile(pkgMapFile);
const paths: string[] = pkgMap[wname];

// 创建快速选择框
const quickPick = vscode.window.createQuickPick();
quickPick.title = "选择要更新版本的文件";
quickPick.items = paths.map(path => ({ label: path }));
quickPick.canSelectMany = true; // 允许多选

// 显示快速选择框并等待用户选择
const selectedOptions: string[] = await new Promise(resolve => {
quickPick.onDidAccept(() => {
resolve(quickPick.selectedItems.map(item => item.label));
quickPick.dispose();
});
quickPick.onDidHide(() => {
resolve([]);
quickPick.dispose();
});
quickPick.show();
});

if (selectedOptions.length === 0) {
// 如果用户没有选择任何选项,则直接返回
return;
}

// 获取用户选择的路径的版本号
const versionPromises = selectedOptions.map(async (value: string) => {
if (await fs.exists(value)) {
const pkgContent = fs.readJSONSync(value);
return pkgContent.version;
}
return null; // 如果文件不存在,返回null
});

Promise.all(versionPromises).then(versions => {
// 过滤掉null值
const validVersions = versions.filter(version => version !== null);
vscode.window
.showInputBox({
title: "请输入新版本号",
value: validVersions[0],
placeHolder: "如果是二段版本号会被使用格式化时间填充为三段版本号",
prompt: `旧版本号: ${validVersions.join(", ")})`,
})
.then(async version => {
if (version) {
// 检查 version 是否只有两段
const versionParts = version.split(".");
if (versionParts.length === 2) {
// 使用当前时间生成第三段版本号
const timestamp = formatDate(new Date(), "yyyyMMddHHmmss");
version = `${version}.${timestamp}`;
}
// 遍历映射并更新版本号
selectedOptions.forEach(async (value: string, index: number) => {
Log.d(TAG, value);
if (await fs.exists(value)) {
const pkgContent = fs.readJSONSync(value);
pkgContent.version = version;
fs.writeFileSync(value, JSON.stringify(pkgContent, null, 2));
Log.d(`${version} ->${value}`);
} else {
vscode.window.showWarningMessage(`已跳过无效映射 ${value}`);
}
});
vscode.window.showInformationMessage("所有 package.json 文件的版本已更新。");
}
});
});
} else {
vscode.window.showWarningMessage("当前不在工作区环境");
}
});

context.subscriptions.push(disposable);
}
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sillot",
"version": "0.35.3100",
"version": "0.35.20240612140342",
"syv": "3.1.0",
"sypv": "[3.0.16, 3.0.17]",
"description": "Build Your Eternal Digital Garden",
Expand Down
2 changes: 1 addition & 1 deletion docs/starlight/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "starlight",
"type": "module",
"version": "0.35.3100",
"version": "0.35.20240612140342",
"scripts": {
"test": "echo 'Test your sister day by day'",
"dev": "astro dev",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Sillot-workspace",
"version": "0.35.3100",
"version": "0.35.20240612140342",
"description": "汐洛 pnpm 工作区",
"packageManager": "[email protected]",
"scripts": {
Expand Down

0 comments on commit 614b1d3

Please sign in to comment.