mirror of
https://github.com/opisek/obsidian-statusbar-organizer.git
synced 2026-07-22 09:20:28 +00:00
add incremental data.json upgrades
This commit is contained in:
parent
33697e24f9
commit
89bb6c512a
7 changed files with 114 additions and 18 deletions
20
main.ts
20
main.ts
|
|
@ -8,6 +8,7 @@ import { fixOrder } from './src/organizer';
|
|||
import { monitorFullscreen } from 'src/fullscreen';
|
||||
import { registerHotkeys } from 'src/hotkeys';
|
||||
import { showSettings } from './src/menu';
|
||||
import { upgrade } from 'src/upgrade';
|
||||
|
||||
const DEFAULT_SETTINGS: StatusBarOrganizerSettings = {
|
||||
activePreset: "Default",
|
||||
|
|
@ -16,7 +17,8 @@ const DEFAULT_SETTINGS: StatusBarOrganizerSettings = {
|
|||
presets: {
|
||||
"Default": {}
|
||||
},
|
||||
presetsOrder: ["Default"]
|
||||
presetsOrder: ["Default"],
|
||||
version: "CURRENT_VERSION"
|
||||
}
|
||||
|
||||
export default class StatusBarOrganizer extends Plugin {
|
||||
|
|
@ -40,16 +42,12 @@ export default class StatusBarOrganizer extends Plugin {
|
|||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
|
||||
// Backwards compatibility with versions < 2.0.0
|
||||
const oldSettings = this.settings as any;
|
||||
if ("status" in oldSettings) {
|
||||
oldSettings.presets.default = oldSettings.status;
|
||||
delete oldSettings.status;
|
||||
this.settings = oldSettings as StatusBarOrganizerSettings;
|
||||
await this.saveSettings();
|
||||
}
|
||||
const savedData = await this.loadData();
|
||||
if (Object.keys(savedData).length != 0 && !("version" in savedData)) savedData["version"] = "0.0.0";
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, savedData);
|
||||
|
||||
upgrade(this.settings);
|
||||
await this.saveSettings();
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"id": "statusbar-organizer",
|
||||
"name": "Status Bar Organizer",
|
||||
"version": "2.1.0",
|
||||
"version": "2.1.1",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "Lets you rearrange and hide specific status bar elements.",
|
||||
"author": "Kacper Darowski",
|
||||
"authorUrl": "https://opisek.net/",
|
||||
"isDesktopOnly": true
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
{
|
||||
"name": "obsidian-statusbar-organizer",
|
||||
"version": "2.1.0",
|
||||
"version": "2.1.1",
|
||||
"description": "Arrange and hide status bar elements.",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production && node version-bump.mjs",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
},
|
||||
"keywords": [],
|
||||
|
|
|
|||
13
src/types.d.ts
vendored
13
src/types.d.ts
vendored
|
|
@ -25,9 +25,20 @@ interface StatusBarOrganizerSettings {
|
|||
separateFullscreenPreset: boolean,
|
||||
presets: { [key: string]: BarStatus }
|
||||
presetsOrder: string[];
|
||||
version: string;
|
||||
}
|
||||
|
||||
type ElectronWindow = {
|
||||
addListener: (event: string, callback: () => void) => void;
|
||||
isFullScreen: () => boolean;
|
||||
}
|
||||
}
|
||||
|
||||
type Version = {
|
||||
major: number;
|
||||
minor: number;
|
||||
patch: number;
|
||||
}
|
||||
|
||||
type Upgrades = Map<number, Map<number, Map<number, Upgrade>>>;
|
||||
type Upgrade = (settings: StatusBarOrganizerSettings) => void;
|
||||
type VersionUpgrade = { version: Version, upgrade: Upgrade };
|
||||
|
|
|
|||
80
src/upgrade.ts
Normal file
80
src/upgrade.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
let upgrades: Upgrades = new Map();
|
||||
|
||||
function ver(major: number, minor: number, patch: number) {
|
||||
return { major, minor, patch };
|
||||
}
|
||||
function parseVersion(version: string): Version {
|
||||
const [major, minor, patch] = version.split('.').map(x => Number.parseInt(x));
|
||||
return { major, minor, patch };
|
||||
}
|
||||
|
||||
function registerUpdate(version: Version, upgrade: (settings: StatusBarOrganizerSettings) => void) {
|
||||
let majorCollection = upgrades.get(version.major);
|
||||
if (!majorCollection) {
|
||||
majorCollection = new Map();
|
||||
upgrades.set(version.major, majorCollection);
|
||||
}
|
||||
|
||||
let minorCollection = majorCollection.get(version.minor);
|
||||
if (!minorCollection) {
|
||||
minorCollection = new Map();
|
||||
majorCollection.set(version.minor, minorCollection);
|
||||
}
|
||||
|
||||
minorCollection.set(version.patch, upgrade);
|
||||
}
|
||||
|
||||
function filterCollections(collection: Map<number, any>, threshold: number) {
|
||||
return [...collection.entries()].filter(x => x[0] >= threshold).sort((a, b) => a[0] - b[0]);
|
||||
}
|
||||
|
||||
function getUpgradeList(upgrades: Upgrades, version: Version): VersionUpgrade[] {
|
||||
const upgradesList: VersionUpgrade[] = [];
|
||||
|
||||
for (let majorUpgrades of filterCollections(upgrades, version.major)) {
|
||||
for (let minorUpgrades of filterCollections(majorUpgrades[1], version.minor)) {
|
||||
for (let [patch, upgrade] of filterCollections(minorUpgrades[1], version.patch)) {
|
||||
if (majorUpgrades[0] === version.major && minorUpgrades[0] === version.minor && patch === version.patch) {
|
||||
continue;
|
||||
}
|
||||
upgradesList.push({ version: { major: majorUpgrades[0], minor: minorUpgrades[0], patch }, upgrade });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return upgradesList;
|
||||
}
|
||||
|
||||
export function upgrade(settings: StatusBarOrganizerSettings) {
|
||||
const oldSettings = settings as any;
|
||||
let version;
|
||||
if ("version" in oldSettings) {
|
||||
version = parseVersion(oldSettings.version);
|
||||
} else {
|
||||
version = ver(0,0,0);
|
||||
}
|
||||
|
||||
getUpgradeList(upgrades, version).forEach(x => x.upgrade(settings));
|
||||
settings.version = "CURRENT_VERSION";
|
||||
}
|
||||
|
||||
// Backwards compatibility with versions < 2.0.0
|
||||
// Add presets to settings
|
||||
registerUpdate(ver(2,0,0), (settings: StatusBarOrganizerSettings) => {
|
||||
const oldSettings = settings as any;
|
||||
if ("status" in oldSettings) {
|
||||
oldSettings.presets.default = oldSettings.status;
|
||||
delete oldSettings.status;
|
||||
settings = oldSettings as StatusBarOrganizerSettings;
|
||||
}
|
||||
});
|
||||
|
||||
// Backwards compatibility with versions < 2.1.1
|
||||
// Remove exists property from elements
|
||||
registerUpdate(ver(2,1,1), (settings: StatusBarOrganizerSettings) => {
|
||||
Object.entries(settings.presets).forEach(([preset, presetSettings]) => {
|
||||
Object.entries(presetSettings).forEach(([element, elementSettings]) => {
|
||||
delete (elementSettings as any)["exists"];
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -12,3 +12,8 @@ writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t"));
|
|||
let versions = JSON.parse(readFileSync("versions.json", "utf8"));
|
||||
versions[targetVersion] = minAppVersion;
|
||||
writeFileSync("versions.json", JSON.stringify(versions, null, "\t"));
|
||||
|
||||
// update main.js with target version
|
||||
let main = readFileSync("main.js", "utf8");
|
||||
main = main.replaceAll("CURRENT_VERSION", targetVersion);
|
||||
writeFileSync("main.js", main);
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
{
|
||||
"1.0.0": "0.15.0"
|
||||
}
|
||||
"1.0.0": "0.15.0",
|
||||
"2.1.0": "0.15.0",
|
||||
"2.1.1": "0.15.0"
|
||||
}
|
||||
Loading…
Reference in a new issue